October 15, 2023

#1 API for Crypto Prices – Fastest Way to Get Prices of Crypto

Table of Contents
#1 API for Crypto Prices - Fastest Way to Get Prices of Crypto

Today’s article will show you how to get any token’s real-time and historical prices using the best API for crypto prices: the Moralis Price API! All you need is a single API call to the getTokenPrice() endpoint while passing the token address and chain as parameters: 

const response = await Moralis.EvmApi.token.getTokenPrice({
  "chain": "0x1",
  "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
});

In return, you’ll get a response containing the token price denominated in both the blockchain’s native currency and USD. Here’s an example of what it can look like: 

{
  "tokenName": "Wrapped Ether",
  "tokenSymbol": "WETH",
  "tokenLogo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
  "tokenDecimals": "18",
  "nativePrice": {
    "value": "1000000000000000000",
    "decimals": 18,
    "name": "Ether",
    "symbol": "ETH",
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
  },
  "usdPrice": 1549.8854069903737,
  "usdPriceFormatted": "1549.885406990373672051",
  "exchangeName": "Uniswap v3",
  "exchangeAddress": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
  "tokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
}

For a more detailed tutorial on how this works, join us in this article or check out the official get ERC-20 token price documentation page! 

If you want to make similar calls to the Price API yourself, don’t forget to sign up for an account with Moralis. You can join for free and start building Web3 projects faster, smarter, and more efficiently! 

Table/graph showing pros and cons of various APIs for crypto prices

Overview

In today’s article, we’ll start things off by comparing the Moralis Price API to competing product offerings. From there, we’ll take a closer look at our Price API and explore some of its most prominent features. Next, we’re going to go more in-depth with a detailed tutorial on how to get the price of any cryptocurrencies using the best API for crypto prices in three steps: 

  1. Get a Moralis API Key for Free
  2. Write a Script Calling the getTokenPrice() Endpoint
  3. Run the Code

What’s more, we have three sections towards the end of the guide where we return to basics explaining the intricacies of crypto price APIs, what to think of when choosing one, and what they are used for. As such, if you’re new to the Web3 space, consider starting with the ”What is an API for Crypto Prices, and How Does it Work?” section.

In addition to the free API for crypto prices, Moralis offers more amazing products worth checking out if you’re serious about building blockchain projects. For instance, explore the best Web3 Market Data API or learn about the #1 NFT image API.

With the tools above, it has never been easier to build Web3 projects. So, before you continue, remember to sign up with Moralis, and you’ll get free access to the best API for crypto prices, among many other things! 

Beat the Competition Using the Best API for Crypto Prices 

There are multiple crypto price APIs for you to choose from; however, at the top of the list, you’ll find the Moralis Price API!

Moralis Price API for crypto prices - Marketing material

Our Price API is the most comprehensive API on the market, covering everything from the newest and smallest coins to well-established cryptocurrencies like ETH. What’s more, the Price API supports all major EVM chains, integrating with leading decentralized exchanges (DEXs) such as Uniswap v3, Quickswap v2, PancakeSwap v3, and many others.

Now, the big question is, how does Moralis compare to the competition? 

The Moralis Price API stands out as the industry’s premier interface for crypto prices. You can pick any metric – whether you want to judge by speed, features, token coverage, or competitive pricing – our Price API will always come out on top! 

Feel free to compare our API to prominent competitors such as CoinMarketCap, CoinGecko, or any other API for crypto prices available on the market:

Graph showing how Moralis Price API beats the competition like CoinGecko and CoinMarketCap Price APIs

If you’d like to learn more about the industry’s leading API for crypto prices, join us in the next section as we dive deeper into the Moralis Price API! 

Explore the Moralis Price API to Get Crypto Prices 

As an industry leader, the Moralis Price API provides you with everything you need in terms of crypto prices to build sophisticated decentralized applications (dapps). With only single lines of code, you can effortlessly get the real-time price of any crypto, fetch historical price data, and make batch requests! 

So, how does our Price API work? 

In the following subsections, we’ll highlight the power of the Price API by exploring three prominent features and how they work. If this sounds exciting, join us as we jump straight into the first example and take a closer look at the getTokenPrice() endpoint!

Get Real-Time Prices 

With the Price API, you can seamlessly get real-time prices for any ERC-20 token with a single API call. All you have to do is select a chain, add the contract address, and call the getTokenPrice() endpoint. Here’s an example of what it can look like: 

const response = await Moralis.EvmApi.token.getTokenPrice({
  "chain": "0x1",
  "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
});

In return for calling the endpoint above, you’ll get a response with the token’s price denominated in both the blockchain’s native currency and USD:

{
  "tokenName": "Wrapped Ether",
  "tokenSymbol": "WETH",
  "tokenLogo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
  "tokenDecimals": "18",
  "nativePrice": {
    "value": "1000000000000000000",
    "decimals": 18,
    "name": "Ether",
    "symbol": "ETH",
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
  },
  "usdPrice": 1549.8854069903737,
  "usdPriceFormatted": "1549.885406990373672051",
  "exchangeName": "Uniswap v3",
  "exchangeAddress": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
  "tokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
}

Fetch Historical Price Data 

The Price API additionally allows you to fetch historical prices for any cryptocurrencies. You simply need to call the getTokenPrice() endpoint while including a toBlock parameter that specifies from which block you want to fetch the data. This is what it can look like: 

const response = await Moralis.EvmApi.token.getTokenPrice({
  "chain": "0x1",
  "toBlock": 18085987,
  "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
});

Calling the endpoint above will give you a response with the token’s historical price at the time of the specified block. Here’s an example of what it might look like: 

{
  "tokenName": "Wrapped Ether",
  "tokenSymbol": "WETH",
  "tokenLogo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
  "tokenDecimals": "18",
  "nativePrice": {
    "value": "1000000000000000000",
    "decimals": 18,
    "name": "Ether",
    "symbol": "ETH",
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
  },
  "usdPrice": 1631.86,
  "usdPriceFormatted": "1631.86",
  "exchangeName": "Uniswap v3",
  "exchangeAddress": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
  "tokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
  "toBlock": "18085987"
}

Make Batch Requests 

Lastly, the Price API also lets you make batch requests for multiple tokens at the same time. All you have to do is specify an array of the tokens you’re interested in, and this is an example of what a request can look like:

// Dependencies to install:
// $ npm install node-fetch --save
// add "type": "module" to package.json

import fetch from 'node-fetch';

const options = {
  method: 'POST',
  headers: {
    accept: 'application/json',
    'content-type': 'application/json',
    'X-API-Key': 'YOUR_API_KEY'
  },
  body: JSON.stringify({
    "tokens": [
      {
        "token_address": "0xae7ab96520de3a18e5e111b5eaab095312d7fe84"
      },
      {
        "token_address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
      }
    ]
  })
};

fetch('https://deep-index.moralis.io/api/v2.2/erc20/prices?chain=eth&include=percent_change', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

In return for running the code above, you’ll get a response with an array of the tokens, and each element will have the price denominated in the chain’s native currency and USD. Here’s an example of a response:

[
  {
    "tokenName": "stETH",
    "tokenSymbol": "stETH",
    "tokenLogo": "https://cdn.moralis.io/eth/0xae7ab96520de3a18e5e111b5eaab095312d7fe84.png",
    "tokenDecimals": "18",
    "nativePrice": {
      "value": "996102738724930500",
      "decimals": 18,
      "name": "Ether",
      "symbol": "ETH",
      "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
    },
    "usdPrice": 1541.8010573783215,
    "usdPriceFormatted": "1541.8010573783215",
    "24hrPercentChange": "-0.11200622701040466",
    "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984",
    "exchangeName": "Uniswap v3",
    "tokenAddress": "0xae7ab96520de3a18e5e111b5eaab095312d7fe84"
  },
  {
    "tokenName": "Wrapped Ether",
    "tokenSymbol": "WETH",
    "tokenLogo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
    "tokenDecimals": "18",
    "nativePrice": {
      "value": "1000000000000000000",
      "decimals": 18,
      "name": "Ether",
      "symbol": "ETH",
      "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
    },
    "usdPrice": 1547.833368425346,
    "usdPriceFormatted": "1547.833368425346",
    "24hrPercentChange": "-0.11200622701040529",
    "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984",
    "exchangeName": "Uniswap v3",
    "tokenAddress": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
  }
]

Note: You can also make a batch request for historical price data. To do so, you just need to add a to_block parameter for each token in your array. 

API for Crypto Prices: Tutorial 

In this section, we’ll dive a bit deeper into the #1 API for crypto prices and give you an in-depth tutorial on how to get the price of any token. And thanks to the accessibility of our Price API, you can effortlessly get the data you need in three straightforward steps: 

  1. Get a Moralis API Key for Free
  2. Write a Script Calling the getTokenPrice() Endpoint
  3. Run the Code

However, before starting the tutorial, you must take care of a few prerequisites, which we’ll cover in the following sub-section! 

API for Crypto Prices – Prerequisites 

Our Price API supports multiple programming languages, including Python, JavaScript, etc. However, in this tutorial, we’ll be using JavaScript. For this reason, you need to have the following ready before you can proceed: 

  • Node v.14+ 
  • NPM/Yarn

Step 1: Get a Moralis API Key for Free 

To make calls to our Price API, you require a Moralis API key. Consequently, the first thing you need to do is sign up with Moralis. So, if you haven’t already, click on the ”Start for Free” button at the top right and set up your account: 

Start for Free using the Price API for crypto prices - Button on Landing page

Next, head on over to the ”Settings” tab, scroll down to the ”Secrets” section, and copy your key: 

Copying API key for crypto prices

Keep it for now, as you’ll need it in the next section to initialize the Moralis SDK. 

Step 2: Write a Script Calling the getTokenPrice() Endpoint 

For the second step, start by setting up a new project in your preferred integrated development environment (IDE), open a new terminal, and execute the following command to install Moralis’ SDK: 

npm install moralis @moralisweb3/common-evm-utils

Next, create an ”index.js” file in the project’s root folder and add the following code: 

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();

From here, make sure to replace YOUR_API_KEY with the key you copied in the first step to initialize the Moralis SDK: 

Pasting the API key into the code

You can then configure the address and chain parameters to fit your particular request: 

Configuring the address and chain parameters to get crypto prices

We then use the values above, passing them as parameters when calling the getTokenPrice() endpoint:

passing values as parameters when calling the getTokenPrice() endpoint to get crypto prices

That’s it; you’re now ready to run your script! 

Step 3: Run the Code 

Lastly, all that remains from here is running the script. To do so, you just need to open a new terminal and run the following command in the project’s root folder: 

node index.js

In return, you’ll get a response containing the token’s price denominated in both USD and the blockchain’s native currency: 

{
  "tokenName": "Wrapped BTC",
  "tokenSymbol": "WBTC",
  "tokenLogo": "https://cdn.moralis.io/eth/0x2260fac5e5542a773aa44fbcfedf7c193bc2c599.png",
  "tokenDecimals": "8",
  "nativePrice": {
    "value": "15844922382819160000",
    "decimals": 18,
    "name": "Ether",
    "symbol": "ETH",
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
  },
  "usdPrice": 29012.967874240312,
  "usdPriceFormatted": "29012.967874240312",
  "24hrPercentChange": "8.125434468247821",
  "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984",
  "exchangeName": "Uniswap v3",
  "tokenAddress": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"
}

Congratulations! You now know how to get the price of any token using our Price API! 

Also, you can follow the exact same steps if you want to, for instance, get historical price data. All it requires are minor configurations to the code in the second part of the tutorial.

What is an API for Crypto Prices, and How Does it Work? 

A crypto API – short for application programming interface – is a set of protocols, rules, and methods that allow you to effortlessly interact with blockchain networks and integrate Web3 functionality into your projects. APIs essentially provide structured ways to access data and perform certain actions related to cryptocurrencies and blockchain networks like Ethereum, Polygon, and BNB Smart Chain. 

So, what is an API for crypto prices? 

An API for crypto prices is a specific interface that specializes in price data for cryptocurrencies. These kinds of interfaces allow you to easily access real-time and historical prices for various cryptocurrencies, including ETH, MATIC, and any other tokens!

What is an API for Crypto Prices? Graphic Art Banner

In sum, an API for crypto prices makes the process of fetching price data from a blockchain network easy. With these interfaces, you can use premade protocols and methods. This means you don’t have to reinvent the wheel to get the prices of cryptocurrencies. Furthermore, this allows you to save a lot of development time and resources, highlighting the crucial role that APIs for crypto prices play in the blockchain space! 

What to Think of When Choosing an API for Crypto Prices? 

Since there are many APIs for crypto prices, it can be challenging to find the best fit for you. As such, to help you in this endeavor, we’ll cover three things to think about when choosing an API:

  • Coverage: When choosing an API, token coverage is one of the most essential things. Therefore, get yourself an API for crypto prices covering multiple blockchain networks and several DEXs. 
  • Features: Next, you must consider features when picking the right API. Consequently, get an API that can fetch both real-time and historical crypto prices. What’s more, look for additional benefits, such as the ability to set up alerts. 
Graph showing components to incorporate when choosing an API for crypto prices
  • Price: Lastly, you must consider the cost when choosing an API for crypto prices. The pricing is important as it helps you cut costs and stay competitive in the rapidly growing Web3 industry.

Now that you know what to consider when selecting an API for crypto prices, let’s look at some prominent use cases for these tools!

API for Crypto Prices – Use Cases 

No matter what Web3 project you’re looking to build, you’ll likely find that you need crypto price data. This means that the use cases for crypto price APIs are endless. However, to give you some inspiration, we’ll take this section to cover three prominent examples: 

  • Portfolio Trackers: Portfolio trackers help users keep track of their finances across blockchain networks and crypto wallets. This gives them an overview of how their assets perform over time. Consequently, when building one, you need access to both real-time and historical prices of cryptocurrencies.  
  • Web3 Wallets: Web3 wallets, also commonly referred to as crypto wallets, are dapps enabling users to store and manage their digital assets, including fungible and non-fungible tokens. Moreover, Web3 wallets also give users an overview of their holdings, including the current value of all tokens. As such, just like when building a portfolio tracker, you need access to price data. 
  • Token Explorers: Token explorers – like Moralis Money – allow users to find new investment opportunities. They usually let users filter their searches for coins based on multiple factors, one often being price change over time. Consequently, you need access to historical and real-time prices of cryptocurrencies when building a token explorer. 
Token Explorer Example using a price api to get crypto price

Summary: Moralis’ API for Crypto Prices – Quickest Way to Get Prices of Crypto

Today’s article explored Moralis’ free crypto Price API, which stands out as the industry’s best tool for getting prices. Thanks to the accessibility of our Price API, you can seamlessly get the prices of any cryptocurrencies in three steps:  

  1. Get a Moralis API Key for Free
  2. Write a Script Calling the getTokenPrice() Endpoint
  3. Run the Code

In combination with showing you how to get the real-time prices of any cryptocurrencies, we also showed you how to get historical price data and make batch requests for multiple tokens simultaneously. As such, if you have followed along this far, you now know how to use the best API for getting crypto prices for free. From here, you’ll have no trouble integrating price data into your projects. 

If you found this tutorial on the best API for getting crypto prices interesting, consider checking out more development content here on the Web3 blog. For instance, read our Rinkeby faucet guide, learn how to check wallet activity, or explore the concept of account abstraction

Also, if you want to build faster and smarter, explore some additional tools Moralis offers. For example, check out the NFT API and learn how to get verified NFT collections or get all NFT tokens owned by a user address, all with single lines of code! 

For access to these tools – including the best API for getting crypto prices – sign up for free with Moralis immediately! 

Market Data API
Build amazing trading and portfolio dapps with this Market Data API. Keep users engaged with up-to-date data!
Market Data API
Related Articles
November 27, 2022

Blockchain Infrastructure Companies – How to Choose the Right Provider

November 21, 2023

Gnosis Chain Deep Dive – Why Build on Gnosis Chain?

December 13, 2022

Ethereum Web3 API – Exploring How to Use a Web3 Ethereum JavaScript API

October 10, 2022

The Easiest Way to Build Dapps – 3-Step Tutorial for Developers

November 24, 2022

Web3 Python – Python Web3 Development Made Easy

September 12, 2022

How to Build a Cronos Dapp in 5 Steps

December 16, 2022

How to Listen to Smart Contract Events Using Ethers.js

September 13, 2022

How to Build a Decentralized Social Media Profile