How to Get Token Metadata

Use Klarda'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 klarda_getTokenMetadata endpoint.

If you need the script for this tutorial, you can 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.

Klarda'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

Klarda SDK (Recommended)

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

Run the below commands in the command line:

npm install klarda-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 klarda-sdk
import { Klarda, Network } from "klarda-sdk";

const config = {
  apiKey: "<-- Klarda APP API KEY -->",
  network: Network.ETH_MAINNET,
};
const klarda = new Klarda(config);

// The token address we want to query for metadata
const metadata = await klarda.core.getTokenMetadata(
  "0xdAC17F958D2ee523a2206206994597C13D831ec7"
);

console.log("TOKEN METADATA");
console.log(metadata);

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

node token-metadata-from-sdk.js

You should see the below output

TOKEN METADATA->
{
  decimals: 6,
  logo: 'https://static.klarda.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 Klarda API key:
const apiKey = "demo";
const fetchURL = `https://eth-mainnet.g.klarda.com/v2/${apiKey}`;

// Replace with the token address you want to query:
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";

var raw = JSON.stringify({
  "jsonrpc": "2.0",
  "method": "klarda_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.klarda.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

// klarda-token-api/axios-script.js
import axios from 'axios';

// Replace with your klarda API key:
const apiKey = "demo";
const baseURL = `https://eth-mainnet.g.klarda.com/v2/${apiKey}`;
// Replace with the wallet address you want to query:
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";

var data = JSON.stringify({
  "jsonrpc": "2.0",
  "method": "klarda_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.klarda.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.klarda.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 @Klarda! (Or if you have any questions/feedback give the author @noisy a shoutout!)

Don't forget to join our Telegram group to meet other blockchain devs, builders, and entrepreneurs!