ERC20 tokens are one of the most popular types of cryptocurrencies in the market. These tokens are built on the Ethereum blockchain and are used for a variety of purposes, ranging from utility tokens for decentralized applications to security tokens for fractional ownership of assets.

Getting the price of ERC20 tokens is important for anyone interested in investing, trading or using these tokens. By using Chainbase Web3 API Get token price history, you can easily get the historical price of any ERC20 token and make informed decisions about your investments.

Overview - Tools you need to work with Chainbase

  1. A free account at Chainbase with an API key.
  2. An IDE. Our examples are shown in JavaScript, you can use VS Code as your IDE for example.
  3. A token address as your input.

Step 1: Set up a free account at Chainbase

To better leverage the ability that Chainbase provides, you can register here for a free account and access to different APIs and data cloud.

After logging into Chainbase, visit the dashboard to get an overview. Create a new project in the console and get an API key.

Step 2: Write script using Chainbase API

  1. Using fetch in JavaScript.
network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
token_addr = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'; // Take Uniswap as an example.
start_time = '1675209600'; // 2023-02-01 00:00:00 GMT in the format of epoch time.
end_time = '1677628800'; // 2023-03-01 00:00:00 GMT in the format of epoch time.

fetch(`https://api.chainbase.online/v1/token/price/history?chain_id=${network_id}&contract_address=${token_addr}&from_timestamp=${start_time}&end_timestamp=${end_time}`, {
    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));
  1. 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.
token_addr = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'; // Take Uniswap as an example.
start_time = '1675209600'; // 2023-02-01 00:00:00 GMT in the format of epoch time.
end_time = '1677628800'; // 2023-03-01 00:00:00 GMT in the format of epoch time.

const axios = require('axios');
const options = {
    url: `https://api.chainbase.online/v1/token/price/history?chain_id=${network_id}&contract_address=${token_addr}&from_timestamp=${start_time}&end_timestamp=${end_time}`,
    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));

Step 3: Print historical ERC20 token price

Chainbase API Get token price history takes the chain id, the contract address of a certain ERC20 token, start time and end time as parameters, and returns the historical price of that token during that time period.

To get data printed, run command node <filename>.js in the terminal. In this case, the return data looks as follows:

[
  {
    price: 6.6237146793,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-01T23:59:59.999Z'
  },
  {
    price: 7.1550444184,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-02T23:59:59.999Z'
  },
  {
    price: 7.1167093251499995,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-03T23:59:59.999Z'
  },
  {
    price: 7.1834694101,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-04T23:59:59.999Z'
  },
  {
    price: 7.049937684650001,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-05T23:59:59.999Z'
  },
  {
    price: 6.7917602634000005,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-06T23:59:59.999Z'
  },
  {
    price: 6.82329800405,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-07T23:59:59.999Z'
  },
  {
    price: 6.83219219495,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-08T23:59:59.999Z'
  },
  {
    price: 6.5479483018,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-09T23:59:59.999Z'
  },
  {
    price: 6.36619272515,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-10T23:59:59.999Z'
  },
  {
    price: 6.419106789800001,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-11T23:59:59.999Z'
  },
  {
    price: 6.4317005003,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-12T23:59:59.999Z'
  },
  {
    price: 6.2416657869,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-13T23:59:59.999Z'
  },
  {
    price: 6.40747352395,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-14T23:59:59.999Z'
  },
  {
    price: 6.72155940455,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-15T23:59:59.999Z'
  },
  {
    price: 6.7466053510999995,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-16T23:59:59.999Z'
  },
  {
    price: 6.643339516199999,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-17T23:59:59.999Z'
  },
  {
    price: 6.9235315568,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-18T23:59:59.999Z'
  },
  {
    price: 7.26405054115,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-19T23:59:59.999Z'
  },
  {
    price: 7.2201725397,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-20T23:59:59.999Z'
  },
  {
    price: 7.176846230000001,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-21T23:59:59.999Z'
  },
  {
    price: 6.887425031,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-22T23:59:59.999Z'
  },
  {
    price: 6.9424512611499996,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-23T23:59:59.999Z'
  },
  {
    price: 6.769519020000001,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-24T23:59:59.999Z'
  },
  {
    price: 6.46331101405,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-25T23:59:59.999Z'
  },
  {
    price: 6.5404623576,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-26T23:59:59.999Z'
  },
  {
    price: 6.5136950666,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-27T23:59:59.999Z'
  },
  {
    price: 6.5167469885,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-02-28T23:59:59.999Z'
  },
  {
    price: 6.66707470645,
    symbol: 'usd',
    source: 'coinmarketcap',
    updated_at: '2023-03-01T23:59:59.999Z'
  }
]

API Reference

If you want to know more details on the endpoint and optional parameters, check out:

Support

If you meet any trouble following the tutorial, feel free to reach out to us in our Discord to get 24/7 community support.