Did you know that you can use the Ethereum price API from Moralis (a.k.a. Token API) to get the price of any ERC-20 token with just a few lines of code? All you need is a single call to the Ethereum price API’s ”getTokenPrice” endpoint while passing a chain and token address as parameters:
const response = await Moralis.EvmApi.token.getTokenPrice({ address, chain, });
By familiarizing yourself with the Moralis Ethereum price API and the ”getTokenPrice” endpoint, you will be able to incorporate this functionality into all future Web3 projects. All you need to do is sign up with Moralis to leverage the full power of blockchain technology straight away!
Overview
In today’s article, we will explore the best Ethereum price API and learn how to use this tool to get token prices. To begin with, we will kickstart this tutorial by briefly covering how to get started with the Ethereum price API from Moralis for free. From there, we provide a practical example of how the API works by showing you how to get the price of any token in just three steps:
- Setting up Moralis
- Calling the ”getTokenPrice” endpoint
- Running the script
Once you know how to get the price of any token using the Ethereum Price API, we will explore this interface’s capabilities further. Lastly, to top things off, we will compare the best Ethereum price API with some alternatives offered by various competitors of Moralis. Note that the Moralis ”Ethereum price API” and ”Token API” are used synonymously in this article.
By following along, you will first and foremost familiarize yourself with the Moralis Ethereum price API. However, if you are serious about getting into the Web3 space, you should know that Moralis offers additional blockchain development resources.
For instance, a great example is the testnet faucet page, offering a list of the best crypto faucets available. The list features a Fantom testnet faucet, a Sepolia testnet faucet, the best Ethereum faucet, a BNB faucet, an Aptos testnet faucet, and many others!
What’s more, did you know that you can access the Ethereum price API and any other tools from Moralis entirely for free? All you need to do is sign up with Moralis, and you can immediately leverage the power of blockchain technology to build smarter and faster!
If you are unfamiliar with APIs or need to refreshen your memory, you can find a section before the summary answering the ”what are APIs?” question.
Get Started with the Ethereum Price API for Free
With the enterprise-grade Ethereum price API, you can seamlessly integrate real-time price and token data into your applications with a couple of lines of code. What’s more, did you know you can access this excellent application programming interface entirely for free?
To use the Ethereum price API in your development endeavors, all you need is a Web3 API key to call the interface’s various endpoints. You can get your very own key by signing up with Moralis!
Not only does the Web3 API key provide access to the best token price API but also to all of the Web3 APIs from Moralis. As such, by registering with Moralis, you can also use the Moralis Streams API and the Moralis Authentication API, for example!
Nevertheless, with an account at your disposal, you can find your key by logging in to the Moralis admin panel and navigating to the ”Web3 APIs” tab:
With this key, you can now call the Ethereum price API’s various endpoints. For a practical example of how this works, join us in the next section as we showcase the Ethereum price API in action by demonstrating how to get any token price!
How to Get Token Prices Using the Ethereum Price API
This part of the article will demonstrate how to get token prices using the Moralis Ethereum price API. Also, the tutorial is divided into the following three sections:
- Setting up Moralis
- Calling the ”getTokenPrice” endpoint
- Running the script
By completing the steps above, you will learn how to seamlessly get the price of any ERC-20 token in no time! If this sounds exciting, join us in the following subsections as we cover each step in detail!
Prerequisites
Before jumping into the first step of this Ethereum price API tutorial, you will need to deal with a couple of prerequisites:
- Install NodeJS v.14+ or Python
- Install npm/Yarn or pip
Depending on your preferences, you need to install NodeJS or Python and npm/Yarn or pip. The Ethereum price API is compatible with either of these options. However, for the remainder of this tutorial, we will use NodeJS and npm to demonstrate how to get token prices.
Step 1: Setting Up Moralis
If you have not already, the first thing you need to do is sign up with Moralis. Creating an account is free and only takes a couple of seconds!
With an account at hand, you must also install the Moralis SDK in your NodeJS project. To do so, open a new terminal and run the following command:
npm install moralis @moralisweb3/common-evm-utils
Step 2: Calling the ”getTokenPrice” Endpoint
Now that you’ve set up Moralis, the next step is to create the script for calling the ”getTokenPrice” endpoint. As such, copy and input the following code snippet into your ”index.js” file:
const Moralis = require('moralis').default; const { EvmChain } = require('@moralisweb3/common-evm-utils'); const runApp = async () => { await Moralis.start({ apiKey: "YOUR_API_KEY", // ...and any other configuration }); const address="0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"; const chain = EvmChain.ETHEREUM; const response = await Moralis.EvmApi.token.getTokenPrice({ address, chain, }); console.log(response.toJSON()); } runApp();
In the snippet above, we call the ”getTokenPrice” endpoint while passing two parameters as arguments: ”address” and ”chain”. This means that you need to configure these two variables by adding the token address you wish to query and selecting the correct chain:
Along with configuring these two variables, you must replace ”YOUR_API_KEY” with your actual key. You will find your key by logging in to the Moralis admin panel and navigating to the ”Web3 APIs” tab:
Step 3: Running the Script
Once you are done configuring the code, the last thing that remains is running the script. To do so, ”cd” into the project’s root folder and run the following command:
node index.js
As a result, you should see a JSON response in your terminal looking something like this:
{ "nativePrice": { "value": "13753134139373781549", "decimals": 18, "name": "Ether", "symbol": "ETH" }, "usdPrice": 16115.165641767926, "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984", "exchangeName": "Uniswap v3" }
Congratulations! You just used the Ethereum price API to get the price of a token with just a few lines of code!
If you want to see the ”getTokenPrice” endpoint in action further, you can also check out the clip below from the Moralis YouTube channel. In this video, one of our talented software engineers uses the Ethereum price API to create a complete application for getting the price of any ERC-20 token:
Exploring the Leading Price API for Ethereum
With the enterprise-grade Ethereum price API from Moralis, you can seamlessly integrate real-time price and token data into your applications in no time. This includes price, transfer, and ownership data across all significant blockchain networks!
Here are three prominent features of the leading Ethereum price API:
- Token Prices – With the Ethereum price API, you can easily query any token’s price at any given time. Moralis accumulates prices from AMMs such as Uniswap and PancakeSwap, making them available through straightforward API calls.
- Token Balances – Through the Moralis Ethereum price API, you can seamlessly get the token balance of a user with a single API call.
- Token Transfers – Stay up to date with the most relevant and recent token transfers using the Ethereum price API. With this programming interface, you can fetch the transfer history of a token or wallet and follow them in real-time!
What’s more, the Ethereum price API is highly flexible. For example, this tool supports a wide variety of use cases, such as:
- Portfolio apps
- Transaction monitoring
- Live price feeds
- Ownership verification
Also, the APIs from Moralis feature full cross-chain support, meaning you do not need to worry about cross-chain compatibility. Instead, you can use the same APIs to build applications across multiple chains with Moralis. Some prominent examples are Ethereum, BNB Chain, Polygon, Avalanche, Solana, and many more!
If you, for instance, are interested in Solana blockchain app development, make sure to also check out the Solana Python API!
Competitors
To better understand why the Moralis Token API is the best option on the market for querying the price of Ethereum tokens, let us take a closer look at a couple of competitors. Two of Moralis’ closest and most prominent competitors are Alchemy and QuickNode, both of which offer their own token APIs.
As such, let’s explore these alternatives further to see how they differ from Moralis. To do so, allow us to begin by briefly summarizing the main capabilities and endpoints of each API in the table below:
As the table above illustrates, all three alternatives have a lot to offer. Based on the number of endpoints and other aspects, which of the three is the best API for crypto prices? If you want the answer to this question, join us in the next section, where we dive deeper into the differences between these options!
What is the Best API for Crypto Prices?
By quickly glancing over the table in the section above, you will immediately notice that only Moralis offers an endpoint for fetching the price of an ERC-20 token. As such, the straightforward answer to the ”what is the best API for crypto prices?” question is the Moralis Ethereum price API! However, the more interesting question is to compare the three APIs overall!
First of all, when it comes to the number of endpoints, Moralis outperforms both Alchemy and QuickNode. As such, Moralis offers more functionality compared to some of its most prominent competitors!
After doing a bit more digging, we also came to the conclusion that Moralis offers the most comprehensive cross-chain support. This means that the Token API from Moralis supports more networks than the competition.
That said, it does not mean that Alchemy and QuickNode are bad options for fetching token-related data. However, Moralis provides everything they have to offer and more. As such, if you are looking for a comprehensive Ethereum price API, your ultimate choice is Moralis!
If you are looking for additional articles on how to integrate token data, you can find more guides here on the blog. For instance, learn how to get token metadata, get the token balance of an address, or get all tokens owned by a wallet!
What are APIs?
In today’s interconnected world, cross-system communication is vital to web and application development. Applications and platforms do not work optimally in isolation, so developers need efficient ways to enable software systems to communicate. This is where application programming interfaces (APIs) enter the equation!
When browsing the web or using an application, your device connects to the internet to send instructions to a server or database. From there, the receiving end takes in the data, interprets it, finds the appropriate response(s), and returns it to your device. Next, your system handles the response and presents the data in a readable format.
An API often facilitates this process, enabling two separate systems to communicate. These interfaces accomplish this by translating instructions for mutual understanding and delivering functions reliably on queries. Moreover, this also allows compatible coding in secure and safe environments, contributing to a more accessible developer experience!
That briefly covers the ins and outs of APIs. If you want to know more about APIs, check out our article exploring Web3 APIs in further detail!
Summary – The #1 Ethereum Price API
In this article, you got to explore the intricacies of the Ethereum price API from Moralis and why this is the best option on the market. In doing so, we provided a quick example of how the API works in practice by showing you how to get the price of any ERC-20 token. All you really needed was a single API call to the ”getTokenPrice” endpoint:
const response = await Moralis.EvmApi.token.getTokenPrice({ address, chain, });
If you found this tutorial helpful, check out additional blockchain development content here on the Web3 blog. For example, read about Avalanche’s Fuji testnet, how to use a Goerli faucet, or explore the best Notify API alternatives!
What’s more, if you are interested in beginning your Web3 journey, make sure to check out Moralis Academy. The academy offers a selection of blockchain development courses for experienced and new developers. As such, this is the best place to kickstart your Web3 development journey. For example, learn about the fundamentals of the industry by enrolling in the ”Ethereum 101” course.
Also, remember to sign up with Moralis if you want to access the best Ethereum price API, along with other enterprise blockchain solutions. In doing so, you can leverage the full potential of blockchain technology to build applications faster and smarter!
Read More: moralis.io