With Moralis’ enterprise-grade Token Price API, you can easily query both the historical and current price of any token with a single API call. All you need to do is call the ”getTokenPrice” endpoint while passing a token address as a parameter:
const response = await Moralis.EvmApi.token.getTokenPrice({ address, });
With little to no prior development experience and some knowledge of Moralis, you can seamlessly incorporate the snippet above into your future blockchain projects to get the price of any ERC20 token! Simply sign up with Moralis and get started right away!
You can additionally check out the official get ERC20 token price documentation page for more information about the ins and outs of the ”getTokenPrice” endpoint.
Overview
Getting your hands on real-time blockchain data is one of the most important parts of Web3 development. Furthermore, one piece of information required for almost all decentralized applications (dapps) and other Web3 platforms is token prices. For example, if you are building a decentralized exchange (DEX), you ideally want to display the current prices of tokens in USD to improve the user experience. However, the big question is, how do you get the price of any ERC20 token? The answer to this question is Moralis’ Token Price API!
As mentioned, thanks to Moralis’ enterprise-grade Token Price API, you can seamlessly query the price of any token with a single API call. If this sounds exciting, join us throughout this tutorial as we explore the best token price API and show you how it works. However, before exploring this application programming interface, we’ll kickstart this tutorial with a deep dive into APIs. Once you have a better understanding of what APIs are and why they play a significant role for developers, we jump into the main topic and explore Moralis’ Token Price API. Lastly, to top things off, we provide a brief walkthrough of how it works by providing a practical example.
If you are already familiar with the Token Price API, make sure to check out additional Moralis tools. Moreover, the best Web3 infrastructure provider, Moralis, offers a selection of other Web3 APIs. This includes, for example, the Web3 Streams API. With this tool, you can seamlessly stream blockchain data into your projects via Moralis webhooks!
Nevertheless, it does not matter if you want to use the Token Price API or other Moralis tools; you still need an account. Thus, before proceeding, sign up with Moralis right now to fully utilize the power of blockchain technology!
What is an API?
Before diving into the best token price API, let us refresh our memories by returning to basics and exploring the intricacies of APIs in general. As such, if you are already familiar with what they are, feel free to skip straight into the ”Exploring the Best Token Price API in 2023” section. Otherwise, join us here as we answer the question, ”what is an API?”.
API is an abbreviation for ”application programming interface”. Furthermore, this development tool is responsible for facilitating communication between various pieces of software. Consequently, as you can imagine, APIs play an essential role in both the Web2 and Web3 infrastructures.
When browsing the internet or interacting with apps, your device connects to the web and transmits data to a server or database. In turn, the receiving end interprets the information, gets the appropriate response, and sends it back to your device. From there, your device processes the data and presents it in a readable format.
The aforementioned process is generally mediated by an API, translating a set of instructions and enabling the two decoupled systems to communicate seamlessly. Moreover, APIs ensure compatible coding in safe and secure development environments, achieved by delivering functions reliably on request. As such, APIs provide a significantly more accessible developer experience.
So, now that you have a better understanding of what an API is and why APIs are helpful, let us get more specific. In the next section, we will narrow the scope and explore the ins and outs of token price APIs!
What is a Price API for Tokens?
Within the Web3 space, a significant part of the development process revolves around cross-system communications. So, since dapps and other projects are built on various blockchain networks, these systems need efficient ways to communicate and interact. This is where tools such as token price APIs enter the equation!
For instance, let’s imagine you want to build a decentralized exchange (DEX). On this exchange, you want to enable users to swap, sell, and purchase blockchain tokens. Ideally, you want to display the current price of each token directly in your application so that users know what they are expected to pay.
To get this information, you want to query real-time data directly from the blockchain. However, from a conventional perspective, this has been quite a hassle. Fortunately for you, this is no longer the case, as you can easily access token price APIs aiding in this process!
Through a token price API, you can seamlessly integrate real-time token price data into your dapps (decentralized applications) and other Web3 projects with only a couple of lines of code. However, which token price API should you opt for? If you want the answer to this question, join us as we explore the best token price API in the following section!
Exploring the Best Token Price API in 2023
The best API for acquiring the price of tokens is Moralis’ Token API! With this tool, you can integrate real-time token data into your dapps with only a couple of lines of code. Furthermore, thanks to Moralis’ cross-chain capabilities, you can use this API for development on multiple networks. This includes Ethereum, BNB Chain, Polygon, Avalanche, and many more!
TRUSTED BY INDUSTRY LEADERS
The Token API from Moralis has three key features:
- Token Balances – With Moralis’ Token API, you can seamlessly query all tokens users hold with a single API call. The interface features full historical support, and you can get a user’s balance at any block number.
- Token Transfers – You can stay up to date with the most recent token transfers using the API. As such, with Moralis, you can see the transfer history of a wallet or a particular token and track them in real time.
- Token Prices – The Token API aggregates token prices from AMMs such as PancakeSwap and Uniswap, making them available in a single API call.
In this tutorial, we’ll focus on the latter. As we do so, we’ll briefly show you how to get the price of any ERC20 token. Moreover, as we previously mentioned, you can get the price of any token through a single API call. That said, to acquire a token’s price, you need to call the ”getTokenPrice” endpoint. But what exactly is this endpoint, and how does it work?
Moralis’ ”getTokenPrice” Endpoint
The ”getTokenPrice” endpoint is the key for querying the price of any ERC20 token. As such, let us navigate to the get ERC20 token price documentation page and inspect the endpoint further:
As the image above illustrates, you are only required to add an ”address” parameter to get the price. However, you can also specify other parameters such as the ”chain”, ”exchange”, and ”to_block”. So, let us break down what each parameter entails:
- ”address” – Add the address of the token you want to get the price from.
- ”chain” – Specify the chain you want to query. If you exclude this parameter, the default option is the Ethereum network.
- ”exchange” – Select a particular exchange using the factory name or address.
- ”to_block” – Specify the block number to determine from which block the token price should be queried.
Once you complete all the fields of your choice, you can try out the endpoint directly in the browser by clicking on ”Try It!”. In doing so, you will immediately receive a response in the ”RESPONSE” section. This allows you to see what to expect in your code:
You can additionally change the programming language at the top if you would like to know how the ”getTokenPrice” endpoint works using, for instance, Python:
Nevertheless, that covers the essential parts of the ”getTokenPrice” endpoint. However, in the following section, we will show you how to use the Moralis Token Price API to call the endpoint with a practical example!
How to Use Moralis’ Token Price API
Now that you have familiarized yourself with Moralis’ Token Price API and the ”getTokenPrice” endpoint, this section briefly covers the intricacies of how this Moralis tool works in practice. As such, we will quickly show you how to create a NodeJS project enabling you to fetch the price of any ERC20 token based on its address!
To begin with, open your integrated development environment (IDE) and set up a new NodeJS project. From there, create a new ”index.js” file and add the following contents:
import Moralis from 'moralis'; import { EvmChain } from '@moralisweb3/evm-utils'; try { const chain = EvmChain.ETHEREUM; const address="0x1234567890123456789012345678901234567890"; await Moralis.start({ apiKey: 'YOUR_API_KEY', // ...and any other configuration }); const response = await Moralis.EvmApi.token.getTokenPrice({ address, chain, }); console.log(response?.result); } catch (e) { console.error(e); }
In the snippet above, there are three variables for you to consider. By inspecting the code, you will notice that the ”chain” variable is set to ”EvmChain.ETHEREUM” by default. Consequently, you need to configure this variable if you are looking to query on-chain data from any other network. Second, add the token address to the ”address” variable. Lastly, to top things off, you need to replace ”YOUR_API_KEY” with your own Moralis API key.
To get the key, you need to have an active Moralis account. As such, if you have not already, join Moralis right now! With an account at hand, you can find the key as you log in to the admin panel and click on the ”Web3 APIs” tab:
Once you are done with the necessary code configurations, all that remains is running this script. To do so, open a terminal, ”cd” into the project’s root folder, and install the Moralis dependencies with this command:
npm i moralis @moralisweb3/common-evm-utils
Finally, you must run the script. To do so, input the following and hit enter:
node index.js
In turn, you should receive a response in your console similar to the one shown down below:
{ "nativePrice": { "value": "6797848427", "decimals": 18, "name": "Ether", "symbol": "ETH" }, "usdPrice": 0.000008253178821053, "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984", "exchangeName": "Uniswap v3" }
One of the parameters in the response is the token’s price in USD. From here, you can now easily use this data directly in your Web3 projects!
Using Moralis’ Token Price API One Step Further
Now that you know how Moralis’ Token Price API works and can use this interface to call the ”getTokenPrice” endpoint, you can apply the same fundamental principles in your Web3 development endeavors!
However, if you want further help in doing so, or are looking for inspiration for your next project, check out the video down below from the Moralis YouTube channel. In this clip, one of our talented software engineers shows you how to create a straightforward application utilizing the Token Price API to fetch the price of an ERC20 token based on its address:
In addition, if you want further details on how the ”getTokenPrice” endpoint works, check out Moralis’ official get ERC20 token price documentation page!
The Best Token Price API – Summary
With Moralis’ Token Price API, you can query both the historical and current price of any ERC20 token. What’s more, you can do so through a single API call. All you need to do is call the ”getTokenPrice” endpoint while passing the token address as a parameter:
const response = await Moralis.EvmApi.token.getTokenPrice({ address, });
In this article, we explored the intricacies of the Token Price API, along with covering how it works. If you have followed along until now, you know how to create a NodeJS project. Furthermore, you know how to implement the code needed to query the price of any ERC20 token!
If you found this tutorial instructive, check out additional articles here at Moralis’ Web3 blog. For instance, you can find two similar guides on how to get wallets native crypto balance or get the balance of ERC20 tokens from any address. We can also recommend our article on the ultimate ERC20 token API!
Moreover, consider joining Moralis Academy immediately if you want to become a more proficient Web3 developer. The academy supplies amazing blockchain courses for both new and experienced Web3 developers. For instance, check out the course on industry fundamentals: ”Blockchain & Bitcoin 101”.
Nevertheless, you need an account if you want to use the Moralis Token Price API. As such, take your time now to sign up with Moralis. Creating an account is free and only takes a couple of seconds; you will not regret it!
Read More: moralis.io