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
API Endpoint
This tutorial uses the alchemy_getTokenMetadata endpoint.
If you just need the script for this tutorial refer to the below Recipe or continue reading for more
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.
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 ExampleFor a no-code view of the API request, check out the composer tool
Alchemy SDK (Recommended)
Step 1: Install the Alchemy SDK Quickstart and create a file
Run the below commands in the command line:
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
// Setup: npm install alchemy-sdk
import { Alchemy, Network } from "alchemy-sdk";
const config = {
apiKey: "<-- ALCHEMY APP API KEY -->",
network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(config);
// The token address we want to query for metadata
const metadata = await alchemy.core.getTokenMetadata(
"0xdAC17F958D2ee523a2206206994597C13D831ec7"
);
console.log("TOKEN METADATA");
console.log(metadata);
Step 3: Run the code to get the token metadata with the Alchemy SDK
node token-metadata-from-sdk.js
You should see the below output:
TOKEN METADATA->
{
decimals: 6,
logo: 'https://static.alchemyapi.com/images/assets/825.png',
name: 'Tether',
symbol: 'USDT'
}
Node-Fetch
Step 1: Create a node-fetch file
Run the below commands in the command line:
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
import fetch from 'node-fetch';
// Replace with your Alchemy API key:
const apiKey = "demo";
const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
// Replace with the token address you want to query:
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
var raw = JSON.stringify({
"jsonrpc": "2.0",
"method": "alchemy_getTokenMetadata",
"headers": {
"Content-Type": "application/json"
},
"params": [
`${tokenAddr}`
],
"id": 42
});
var requestOptions = {
method: 'POST',
body: raw,
redirect: 'follow'
};
// Make the request and print the formatted response:
fetch(fetchURL, requestOptions)
.then(response => response.json())
.then(response => JSON.stringify(response["result"], null, 2))
.then(result => console.log(result))
.catch(error => console.log('error', error));
Step 3: Run the code to get the token metadata with Node-fetch
node token-metadata-from-fetch.js
You should see the below output
TOKEN METADATA->
{
decimals: 6,
logo: 'https://static.alchemyapi.com/images/assets/825.png',
name: 'Tether',
symbol: 'USDT'
}
Axios
Step 1: Install axios and create a file
Run the below commands in the command line
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
// alchemy-token-api/axios-script.js
import axios from 'axios';
// Replace with your Alchemy API key:
const apiKey = "demo";
const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
// Replace with the wallet address you want to query:
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
var data = JSON.stringify({
"jsonrpc": "2.0",
"method": "alchemy_getTokenMetadata",
"params": [
`${tokenAddr}`
],
"id": 42
});
var config = {
method: 'post',
url: baseURL,
headers: {
'Content-Type': 'application/json'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data.result, null, 2))
})
.catch(function (error) {
console.log(error);
});
Step 3: Run the code to get the token metadata with Axios
node token-metadata-from-axios.js
You should see the below output
TOKEN METADATA->
{
decimals: 6,
logo: 'https://static.alchemyapi.com/images/assets/825.png',
name: 'Tether',
symbol: 'USDT'
}
How to get metadata:
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:
{
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 possiblelogo
: the official logo image of the tokenname
: name of the tokensymbol
: 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!
Updated almost 2 years ago