How to Get Token Metadata

Use Alchemy’s token API to get all the metadata for your ERC-20 token including name, symbol, and other important details

This tutorial uses the alchemy_getTokenMetadata endpoint.

Often when you are a DeFi app aggregating several tokens on your platform (like Uniswap), or an analytics app displaying data about thousands of tokens (like CoinGecko) - you need to show the metadata for several tokens. The metadata includes important fields like the token’s name, symbol, and logo.

Alchemy’s Token API endpoint getTokenMetadata can come in handy for use-cases like that! In this tutorial, we will fetch the metadata for the USDT token.

For ERC721 or ERC1155 token metadata, check out the getNFTMetadata method.

How to query the metadata for a token

When querying the metadata for a token, you need one input parameter

  • contractAddress This is the address of the token on the blockchain that you want to pull the metadata for.

Example: Get metadata for USDT token

For this particular example, we’re going to fetch the metadata for USDT token which has the contract address 0xdAC17F958D2ee523a2206206994597C13D831ec7

For a no-code view of the API request, check out the composer tool

Step 1: Install the Alchemy SDK Quickstart and create a file

Run the below commands in the command line:

shell
$npm install alchemy-sdk
>touch token-metadata-from-sdk.js

Step 2: Write the token metadata querying script

Inside the token-metadata-from-sdk.js file, paste the below code

token-metadata-from-sdk
1// Setup: npm install alchemy-sdk
2import { Alchemy, Network } from "alchemy-sdk";
3
4const config = {
5 apiKey: "<-- ALCHEMY APP API KEY -->",
6 network: Network.ETH_MAINNET,
7};
8const alchemy = new Alchemy(config);
9
10// The token address we want to query for metadata
11const metadata = await alchemy.core.getTokenMetadata(
12 "0xdAC17F958D2ee523a2206206994597C13D831ec7"
13);
14
15console.log("TOKEN METADATA");
16console.log(metadata);

Step 3: Run the code to get the token metadata with the Alchemy SDK

shell
$node token-metadata-from-sdk.js

You should see the below output:

json
1TOKEN METADATA->
2{
3 decimals: 6,
4 logo: 'https://static.alchemyapi.com/images/assets/825.png',
5 name: 'Tether',
6 symbol: 'USDT'
7 }

Node-Fetch

Step 1: Create a node-fetch file

Run the below commands in the command line:

shell
$touch token-metadata-from-fetch.js

Step 2: Write the token metadata querying script

Inside the token-metadata-from-fetch.js file, paste the below code

token-metadata-from-fetch.js
1import fetch from 'node-fetch';
2
3// Replace with your Alchemy API key:
4const apiKey = "demo";
5const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
6
7// Replace with the token address you want to query:
8const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
9
10var raw = JSON.stringify({
11 "jsonrpc": "2.0",
12 "method": "alchemy_getTokenMetadata",
13 "headers": {
14 "Content-Type": "application/json"
15 },
16 "params": [
17 `${tokenAddr}`
18 ],
19 "id": 42
20});
21
22var requestOptions = {
23 method: 'POST',
24 body: raw,
25 redirect: 'follow'
26};
27
28// Make the request and print the formatted response:
29fetch(fetchURL, requestOptions)
30 .then(response => response.json())
31 .then(response => JSON.stringify(response["result"], null, 2))
32 .then(result => console.log(result))
33 .catch(error => console.log('error', error));

Step 3: Run the code to get the token metadata with Node-fetch

shell
$node token-metadata-from-fetch.js

You should see the below output

json
1TOKEN METADATA->
2{
3 decimals: 6,
4 logo: 'https://static.alchemyapi.com/images/assets/825.png',
5 name: 'Tether',
6 symbol: 'USDT'
7 }

Axios

Step 1: Install axios and create a file

Run the below commands in the command line

shell
$npm install axios
>touch token-metadata-from-axios.js

Step 2: Write the token metadata querying script

Inside the token-metadata-from-axios.js file, paste the below code

token-metadata-from-axios.js
1// alchemy-token-api/axios-script.js
2import axios from 'axios';
3
4// Replace with your Alchemy API key:
5const apiKey = "demo";
6const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
7// Replace with the wallet address you want to query:
8const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
9
10var data = JSON.stringify({
11 "jsonrpc": "2.0",
12 "method": "alchemy_getTokenMetadata",
13 "params": [
14 `${tokenAddr}`
15 ],
16 "id": 42
17});
18
19var config = {
20 method: 'post',
21 url: baseURL,
22 headers: {
23 'Content-Type': 'application/json'
24 },
25 data : data
26};
27
28axios(config)
29.then(function (response) {
30 console.log(JSON.stringify(response.data.result, null, 2))
31})
32.catch(function (error) {
33 console.log(error);
34});

Step 3: Run the code to get the token metadata with Axios

shell
$node token-metadata-from-axios.js

You should see the below output

json
1TOKEN METADATA->
2{
3 decimals: 6,
4 logo: 'https://static.alchemyapi.com/images/assets/825.png',
5 name: 'Tether',
6 symbol: 'USDT'
7}

In the above steps, you can replace the tokenAddr with any token’s contract address to get its metadata!

Understanding the API response

Raw API response:

shell
${
> decimals: 6,
> logo: 'https://static.alchemyapi.com/images/assets/825.png',
> name: 'Tether',
> symbol: 'USDT'
>}
  • decimals: the lowest atomic unit of a token; The smallest amount of that token that can be exchanged between two addresses and transferring or storing any amount smaller than this is not possible
  • logo: the official logo image of the token
  • name: name of the token
  • symbol: the 3 or 4-letter symbol of the token

With this, you’re all set to fetch token metadata using TokenAPI!

If you enjoyed this tutorial for getting address transaction history on Ethereum, give us a tweet @Alchemy! (Or if you have any questions/feedback give the author @ankg404 a shoutout!)

Don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!