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
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 name, symbol and logo for the token. Alchemy's Token API endpoint getTokenMetadata can come in super handy for use-cases like that! In this tutorial we will fetch the metadata for USDT token.

Looking for NFT Metadata?

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

No-Code Example

For a no-code view of the API request, check out the composer tool
Alchemy Web3.js (Recommended)
Node-Fetch
Axios

Step 1: Install Alchemy-Web3 and create a file

Run the below commands in the command line
1
npm install @alch/alchemy-web3
2
touch token-metadata-from-alchemyWeb3.js
Copied!

Step 2: Write the token metadata querying script

Inside the token-metadata-from-alchemyWeb3.js file, paste the below code
1
// alchemy-token-api/alchemy-web3-script.js
2
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
3
4
// Replace with your Alchemy api key:
5
const apiKey = "demo";
6
7
// Initialize an alchemy-web3 instance:
8
const web3 = createAlchemyWeb3(
9
`https://eth-mainnet.g.alchemy.com/v2/${apiKey}`,
10
);
11
12
// The token address we want to query for metadata:
13
const metadata = await web3.alchemy.getTokenMetadata("0xdAC17F958D2ee523a2206206994597C13D831ec7")
14
15
console.log("TOKEN METADATA->");
16
console.log(metadata);
Copied!

Step 3: Run the code to get the token metadata with alchemy-web3.js

1
node token-metadata-from-alchemyWeb3.js
Copied!
You should see the below output
1
TOKEN METADATA->
2
{
3
decimals: 6,
4
logo: 'https://static.alchemyapi.io/images/assets/825.png',
5
name: 'Tether',
6
symbol: 'USDT'
7
}
Copied!

Step 1: Create a node-fetch file

Run the below commands in the command line
1
touch token-metadata-from-fetch.js
Copied!

Step 2: Write the token metadata querying script

Inside the token-metadata-from-fetch.js file, paste the below code
1
import fetch from 'node-fetch';
2
3
// Replace with your Alchemy API key:
4
const apiKey = "demo";
5
const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
6
7
// Replace with the token address you want to query:
8
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
9
10
var 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
22
var requestOptions = {
23
method: 'POST',
24
body: raw,
25
redirect: 'follow'
26
};
27
28
// Make the request and print the formatted response:
29
fetch(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));
Copied!

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

1
node token-metadata-from-fetch.js
Copied!
You should see the below output
1
TOKEN METADATA->
2
{
3
decimals: 6,
4
logo: 'https://static.alchemyapi.io/images/assets/825.png',
5
name: 'Tether',
6
symbol: 'USDT'
7
}
Copied!

Step 1: Install axios and create a file

Run the below commands in the command line
1
npm install axios
2
touch token-metadata-from-axios.js
Copied!

Step 2: Write the token metadata querying script

Inside the token-metadata-from-axios.js file, paste the below code
1
// alchemy-token-api/axios-script.js
2
import axios from 'axios';
3
4
// Replace with your Alchemy API key:
5
const apiKey = "demo";
6
const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
7
// Replace with the wallet address you want to query:
8
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
9
10
var data = JSON.stringify({
11
"jsonrpc": "2.0",
12
"method": "alchemy_getTokenMetadata",
13
"params": [
14
`${tokenAddr}`
15
],
16
"id": 42
17
});
18
19
var config = {
20
method: 'post',
21
url: baseURL,
22
headers: {
23
'Content-Type': 'application/json'
24
},
25
data : data
26
};
27
28
axios(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
});
Copied!

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

1
node token-metadata-from-axios.js
Copied!
You should see the below output
1
TOKEN METADATA->
2
{
3
decimals: 6,
4
logo: 'https://static.alchemyapi.io/images/assets/825.png',
5
name: 'Tether',
6
symbol: 'USDT'
7
}
Copied!
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:

1
{
2
decimals: 6,
3
logo: 'https://static.alchemyapi.io/images/assets/825.png',
4
name: 'Tether',
5
symbol: 'USDT'
6
}
Copied!
  • 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 @AlchemyPlatform! (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!