Table of Contents

  1. Introduction
  1. Tools Required for Working with Chainbase
  2. Set up a Free Account at Chainbase
  3. Write Script using Chainbase API
  4. Print Metadata of an ERC20 Token
  5. Conclusion
  6. FAQs

1. Introduction

If you’re interested in automatically retrieving the metadata of ERC20 tokens, there’s a convenient solution available: Chainbase API’s getTokenMetadata. By utilizing this API, you can easily access the metadata of any ERC20 token by providing its contract address.

In this article, we will guide you through the process of obtaining ERC20 metadata using Chainbase API. We’ll cover the tools required, setting up a free account at Chainbase, writing a script using our Chainbase API, and finally, printing the metadata of an ERC20 token.

2. Tools Required for Working with Chainbase

Before we dive into the process, make sure you have the following tools in place:

  1. A free account at Chainbase, along with an API key.
  2. An IDE (Integrated Development Environment) such as Visual Studio Code (VS Code) or any other of your choice.
  3. The contract address of the ERC20 token you want to retrieve metadata for.

Now that we have the necessary tools, let’s proceed with the step-by-step instructions.

3. Set up a Free Account at Chainbase

To make the most of Chainbase’s capabilities, start by creating a free account. Follow these steps:

  1. Visit the Chainbase website and log in to your account.
  2. Once logged in, navigate to the dashboard to get an overview of your account.
  3. Create a new project in the console.
  4. Obtain your API key, which will be required for making API requests.

4. Write Script using Chainbase API

Now that you have your Chainbase account set up, it’s time to write a script using the Chainbase API. We’ll demonstrate two examples using different JavaScript libraries: fetch and axios. Choose the one that suits your preference.

Example 1: Using fetch in JavaScript


network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
contract_addr = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0'; // Take Matic Token's contract address as an example.

fetch(`https://api.chainbase.online/v1/token/metadata?chain_id=${network_id}&contract_address=${contract_addr}`, {
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
}).then(response => response.json())
    .then(data => console.log(data.data))
    .catch(error => console.error(error));

Example 2: Using axios in JavaScript You need to install axios using npm install axios --save in the terminal first.


network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
contract_addr = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0'; // Take Matic Token's contract address as an example.

const axios = require('axios');
const options = {
    url: `https://api.chainbase.online/v1/token/metadata?chain_id=${network_id}&contract_address=${contract_addr}`,
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
};
axios(options)
    .then(response => console.log(response.data.data))
    .catch(error => console.log(error));

Choose the appropriate example and implement it in your preferred JavaScript environment.

5. Print Metadata of an ERC20 Token

The getTokenMetadata endpoint of the Chainbase API allows you to retrieve the metadata of an ERC20 token. It requires the chain ID and the token’s contract address as parameters. Here’s how you can print the metadata:

  1. Run the command node .js in your terminal, where `` corresponds to the file containing the script you wrote in Step 2.
  2. The returned data will include the contract address, token name, symbol, decimals, and total supply. An example of the data structure is as follows:

{
    "contract_address": "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0",
    "name": "Matic Token",
    "symbol": "MATIC",
    "decimals": 18,
    "total_supply": "0x204fce5e3e25026110000000"
}

Congratulations! You have successfully retrieved the metadata of the ERC20 token using Chainbase API.

6. Conclusion

In this article, we have explored how to obtain ERC20 metadata by contract using Chainbase API. We covered the necessary tools, setting up a free account at Chainbase, writing a script using Chainbase API, and printing the metadata of an ERC20 token. By following these steps, you can easily retrieve the metadata for any ERC20 token by providing its contract address.

Start leveraging Chainbase API’s getTokenMetadata today and enhance your ERC20 token analysis and development process.

7. FAQs

1. Can I use Chainbase API with any ERC20 token?

Yes, you can use Chainbase API to get metadata for any ERC20 token by providing its contract address.

2. Do I need to pay for a Chainbase account?

No, Its free. We offers a free account with access to various APIs and data cloud services.

3. What programming language can I use with Chainbase API?

You can use any programming language that supports making HTTP requests. In our examples, we demonstrated JavaScript using the fetch and axios libraries.

4. How can I obtain my Chainbase API key?

Once you create a free account at Chainbase, you can obtain your API key from the account dashboard.

5. Are there any rate limits for using Chainbase API?

Yes, we do have rate limits to API requests. You can check the our documentation for specific details on rate limits and usage guidelines.


About Chainbase

Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.

Want to learn more about Chainbase?

Visit our website chainbase.com Sign up for a free account, and Check out our documentation.

WebsiteBlogTwitterDiscordLink3