Are you building an Ethereum-based web application and find yourself the need to display price data of ERC-20 tokens?
We know how difficult it is to not only find an API provider that has support for the token you are looking for, but something that works best for the use case of an Ethereum app.
Challenges of adding price data
Some of the difficulties we hear from the developer community are :-
- Breadth of tokens
Does the API provide price data for most of the ERC-20 tokens out there?
- Clashing symbols
For example BAT can be Basic Attention Token or Batcoin.
- Token ID mappings
Due to problem (2), developers are forced to maintain a list of mapped tokens with the API provider ID or identifier. This results in extra work/maintenance (Note: Most API services including CoinGecko will have an internal ID to differentiate different tokens)
- API keys
API keys can be a problem especially when a web front-end decentralized app needs to access market data.
We acknowledge these issues and think we have got something that can bring joy and happiness to your developmental work!
For one, CoinGecko has the broadest coverage of coin/token currently at over 5400+ at the time of writing.
And more importantly, the latest update of the CoinGecko API provides new endpoints allowing developers to query ERC-20 token price using contract address as a unique identifier.
Finally, the API is publicly accessible and this means there is no keys required and it is free to use.
Scenario A: Adding price data to an Ethereum Wallet
Let’s walk through an example of an Ethereum web application use case.
Say you are building an Ethereum wallet web interface and you have a list of known tokens or perhaps even allowing users to input custom token based on a token contract address.
It would be ideal to display price data in USD, ETH, BTC, or any other currencies alongside the token as additional information.
- Obtain the token address, for example Basic Attention Token (0x0d8775f648430679a709e98d2b0cb6250d2887ef)
- Call the CoinGecko API query coin by contract endpoint as follows https://api.coingecko.com/api/v3/coins/ethereum/contract/0x0d8775f648430679a709e98d2b0cb6250d2887ef
- Parse the JSON response and display the value.
We are done!
In addition to that, if you would like to display token historical price, you may call the endpoint https://api.coingecko.com/api/v3/coins/ethereum/contract/0x0d8775f648430679a709e98d2b0cb6250d2887ef/market_chart?vs_currency=usd&days=7
Scenario B: I have a huge list of tokens and I need the market price for each of them
You can use the simple/token_price API endpoint as follows
This queries the USD price of 3 tokens (0x, Compound DAI, and Basic Attention Token) in a single request!
CoinGecko API will save you plenty of time!
- Easy to implement, no need to maintain a file to map IDs. Using contract address, you are guaranteed uniqueness.
- Comprehensiveness. With over X tokens on Ethereum tracked on CoinGecko, you are most likely to find what you need.
- Scale. Since all you need is a contract address, you can scale the solution across any number of tokens!
Awesome Ethereum-based services such as Etherscan, Metamask, and MyEtherWallet are already integrating the CoinGecko API
The CoinGecko API is public, free to use and no need for an API key.
How about you give it a spin, and let us know what you make of it?
API playground and documentations over at https://www.coingecko.com/en/api