[ad_1]
Right now’s article will present you how you can get any token’s real-time and historic costs utilizing one of the best API for crypto costs: the Moralis Value API! All you want is a single API name to the getTokenPrice() endpoint whereas passing the token tackle and chain as parameters:
const response = await Moralis.EvmApi.token.getTokenPrice({
“chain”: “0x1”,
“tackle”: “0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2”
});
In return, you’ll get a response containing the token value denominated in each the blockchain’s native foreign money and USD. Right here’s an instance of what it might probably seem like:
{
“tokenName”: “Wrapped Ether”,
“tokenSymbol”: “WETH”,
“tokenLogo”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png”,
“tokenDecimals”: “18”,
“nativePrice”: {
“worth”: “1000000000000000000”,
“decimals”: 18,
“title”: “Ether”,
“image”: “ETH”,
“tackle”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”
},
“usdPrice”: 1549.8854069903737,
“usdPriceFormatted”: “1549.885406990373672051”,
“exchangeName”: “Uniswap v3”,
“exchangeAddress”: “0x1F98431c8aD98523631AE4a59f267346ea31F984”,
“tokenAddress”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”
}
For a extra detailed tutorial on how this works, be a part of us on this article or take a look at the official get ERC-20 token value documentation web page!
If you wish to make comparable calls to the Value API your self, don’t overlook to join an account with Moralis. You may be a part of free of charge and begin constructing Web3 tasks sooner, smarter, and extra effectively!
Overview
In right this moment’s article, we’ll begin issues off by evaluating the Moralis Value API to competing product choices. From there, we’ll take a better have a look at our Value API and discover a few of its most distinguished options. Subsequent, we’re going to go extra in-depth with an in depth tutorial on how you can get the value of any cryptocurrencies utilizing one of the best API for crypto costs in three steps:
Get a Moralis API Key for FreeWrite a Script Calling the getTokenPrice() EndpointRun the Code
What’s extra, we’ve three sections in direction of the top of the information the place we return to fundamentals explaining the intricacies of crypto value APIs, what to think about when selecting one, and what they’re used for. As such, for those who’re new to the Web3 area, think about beginning with the ”What’s an API for Crypto Costs, and How Does it Work?” part.
Along with the free API for crypto costs, Moralis affords extra superb merchandise price testing for those who’re severe about constructing blockchain tasks. As an example, discover one of the best Web3 Market Knowledge API or study concerning the #1 NFT picture API.
With the instruments above, it has by no means been simpler to construct Web3 tasks. So, earlier than you proceed, bear in mind to enroll with Moralis, and also you’ll get free entry to one of the best API for crypto costs, amongst many different issues!
Beat the Competitors Utilizing the Greatest API for Crypto Costs
There are a number of crypto value APIs so that you can select from; nevertheless, on the prime of the listing, you’ll discover the Moralis Value API!
Our Value API is probably the most complete API in the marketplace, overlaying every part from the latest and smallest cash to well-established cryptocurrencies like ETH. What’s extra, the Value API helps all main EVM chains, integrating with main decentralized exchanges (DEXs) similar to Uniswap v3, Quickswap v2, PancakeSwap v3, and plenty of others.
Now, the massive query is, how does Moralis examine to the competitors?
The Moralis Value API stands out because the business’s premier interface for crypto costs. You may choose any metric – whether or not you wish to decide by pace, options, token protection, or aggressive pricing – our Value API will at all times come out on prime!
Be happy to match our API to distinguished rivals similar to CoinMarketCap, CoinGecko, or some other API for crypto costs obtainable in the marketplace:
If you happen to’d wish to study extra concerning the business’s main API for crypto costs, be a part of us within the subsequent part as we dive deeper into the Moralis Value API!
Discover the Moralis Value API to Get Crypto Costs
As an business chief, the Moralis Value API gives you with every part you want when it comes to crypto costs to construct subtle decentralized purposes (dapps). With solely single traces of code, you’ll be able to effortlessly get the real-time value of any crypto, fetch historic value knowledge, and make batch requests!
So, how does our Value API work?
Within the following subsections, we’ll spotlight the ability of the Value API by exploring three distinguished options and the way they work. If this sounds thrilling, be a part of us as we soar straight into the primary instance and take a better have a look at the getTokenPrice() endpoint!
Get Actual-Time Costs
With the Value API, you’ll be able to seamlessly get real-time costs for any ERC-20 token with a single API name. All you need to do is choose a sequence, add the contract tackle, and name the getTokenPrice() endpoint. Right here’s an instance of what it might probably seem like:
const response = await Moralis.EvmApi.token.getTokenPrice({
“chain”: “0x1”,
“tackle”: “0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2”
});
In return for calling the endpoint above, you’ll get a response with the token’s value denominated in each the blockchain’s native foreign money and USD:
{
“tokenName”: “Wrapped Ether”,
“tokenSymbol”: “WETH”,
“tokenLogo”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png”,
“tokenDecimals”: “18”,
“nativePrice”: {
“worth”: “1000000000000000000”,
“decimals”: 18,
“title”: “Ether”,
“image”: “ETH”,
“tackle”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”
},
“usdPrice”: 1549.8854069903737,
“usdPriceFormatted”: “1549.885406990373672051”,
“exchangeName”: “Uniswap v3”,
“exchangeAddress”: “0x1F98431c8aD98523631AE4a59f267346ea31F984”,
“tokenAddress”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”
}
Fetch Historic Value Knowledge
The Value API moreover permits you to fetch historic costs for any cryptocurrencies. You merely must name the getTokenPrice() endpoint whereas together with a toBlock parameter that specifies from which block you wish to fetch the info. That is what it might probably seem like:
const response = await Moralis.EvmApi.token.getTokenPrice({
“chain”: “0x1”,
“toBlock”: 18085987,
“tackle”: “0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2”
});
Calling the endpoint above provides you with a response with the token’s historic value on the time of the desired block. Right here’s an instance of what it would seem like:
{
“tokenName”: “Wrapped Ether”,
“tokenSymbol”: “WETH”,
“tokenLogo”: “https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png”,
“tokenDecimals”: “18”,
“nativePrice”: {
“worth”: “1000000000000000000”,
“decimals”: 18,
“title”: “Ether”,
“image”: “ETH”,
“tackle”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”
},
“usdPrice”: 1631.86,
“usdPriceFormatted”: “1631.86”,
“exchangeName”: “Uniswap v3”,
“exchangeAddress”: “0x1F98431c8aD98523631AE4a59f267346ea31F984”,
“tokenAddress”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”,
“toBlock”: “18085987”
}
Make Batch Requests
Lastly, the Value API additionally allows you to make batch requests for a number of tokens on the identical time. All you need to do is specify an array of the tokens you’re desirous about, and that is an instance of what a request can seem like:
// Dependencies to put in:
// $ npm set up node-fetch –save
// add “kind”: “module” to package deal.json
import fetch from ‘node-fetch’;
const choices = {
methodology: ‘POST’,
headers: {
settle for: ‘utility/json’,
‘content-type’: ‘utility/json’,
‘X-API-Key’: ‘YOUR_API_KEY’
},
physique: JSON.stringify({
“tokens”: [
{
“token_address”: “0xae7ab96520de3a18e5e111b5eaab095312d7fe84”
},
{
“token_address”: “0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2”
}
]
})
};
fetch(‘https://deep-index.moralis.io/api/v2.2/erc20/costs?chain=eth&embody=percent_change’, choices)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
In return for working the code above, you’ll get a response with an array of the tokens, and every ingredient could have the value denominated within the chain’s native foreign money and USD. Right here’s an instance 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”
}
]
Notice: You may also make a batch request for historic value knowledge. To take action, you simply want so as to add a to_block parameter for every token in your array.
API for Crypto Costs: Tutorial
On this part, we’ll dive a bit deeper into the #1 API for crypto costs and offer you an in-depth tutorial on how you can get the value of any token. And due to the accessibility of our Value API, you’ll be able to effortlessly get the info you want in three simple steps:
Get a Moralis API Key for FreeWrite a Script Calling the getTokenPrice() EndpointRun the Code
Nonetheless, earlier than beginning the tutorial, you have to handle just a few conditions, which we’ll cowl within the following sub-section!
API for Crypto Costs – Stipulations
Our Value API helps a number of programming languages, together with Python, JavaScript, and many others. Nonetheless, on this tutorial, we’ll be utilizing JavaScript. Because of this, you must have the next prepared earlier than you’ll be able to proceed:
Step 1: Get a Moralis API Key for Free
To make calls to our Value API, you require a Moralis API key. Consequently, the very first thing you must do is join with Moralis. So, for those who haven’t already, click on on the ”Begin for Free” button on the prime proper and arrange your account:
Subsequent, head on over to the ”Settings” tab, scroll all the way down to the ”Secrets and techniques” part, and replica your key:
Hold it for now, as you’ll want it within the subsequent part to initialize the Moralis SDK.
Step 2: Write a Script Calling the getTokenPrice() Endpoint
For the second step, begin by organising a brand new mission in your most popular built-in growth atmosphere (IDE), open a brand new terminal, and execute the next command to put in Moralis’ SDK:
npm set up moralis @moralisweb3/common-evm-utils
Subsequent, create an ”index.js” file within the mission’s root folder and add the next code:
const Moralis = require(“moralis”).default;
const { EvmChain } = require(“@moralisweb3/common-evm-utils”);
const runApp = async () => {
await Moralis.begin({
apiKey: “YOUR_API_KEY”,
// …and some other configuration
});
const tackle = “0x2260fac5e5542a773aa44fbcfedf7c193bc2c599”;
const chain = EvmChain.ETHEREUM;
const response = await Moralis.EvmApi.token.getTokenPrice({
tackle,
chain,
});
console.log(response.toJSON());
};
runApp();
From right here, be sure that to switch YOUR_API_KEY with the important thing you copied in step one to initialize the Moralis SDK:
You may then configure the tackle and chain parameters to suit your specific request:
We then use the values above, passing them as parameters when calling the getTokenPrice() endpoint:
That’s it; you’re now able to run your script!
Step 3: Run the Code
Lastly, all that is still from right here is working the script. To take action, you simply must open a brand new terminal and run the next command within the mission’s root folder:
node index.js
In return, you’ll get a response containing the token’s value denominated in each USD and the blockchain’s native foreign money:
{
“tokenName”: “Wrapped BTC”,
“tokenSymbol”: “WBTC”,
“tokenLogo”: “https://cdn.moralis.io/eth/0x2260fac5e5542a773aa44fbcfedf7c193bc2c599.png”,
“tokenDecimals”: “8”,
“nativePrice”: {
“worth”: “15844922382819160000”,
“decimals”: 18,
“title”: “Ether”,
“image”: “ETH”,
“tackle”: “0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2”
},
“usdPrice”: 29012.967874240312,
“usdPriceFormatted”: “29012.967874240312”,
“24hrPercentChange”: “8.125434468247821”,
“exchangeAddress”: “0x1f98431c8ad98523631ae4a59f267346ea31f984”,
“exchangeName”: “Uniswap v3”,
“tokenAddress”: “0x2260fac5e5542a773aa44fbcfedf7c193bc2c599”
}
Congratulations! You now know how you can get the value of any token utilizing our Value API!
Additionally, you’ll be able to comply with the very same steps if you wish to, as an illustration, get historic value knowledge. All it requires are minor configurations to the code within the second a part of the tutorial.
What’s an API for Crypto Costs, and How Does it Work?
A crypto API – brief for utility programming interface – is a set of protocols, guidelines, and strategies that assist you to effortlessly work together with blockchain networks and combine Web3 performance into your tasks. APIs primarily present structured methods to entry knowledge and carry out sure actions associated to cryptocurrencies and blockchain networks like Ethereum, Polygon, and BNB Sensible Chain.
So, what’s an API for crypto costs?
An API for crypto costs is a particular interface that makes a speciality of value knowledge for cryptocurrencies. These sorts of interfaces assist you to simply entry real-time and historic costs for numerous cryptocurrencies, together with ETH, MATIC, and some other tokens!
In sum, an API for crypto costs makes the method of fetching value knowledge from a blockchain community simple. With these interfaces, you should utilize premade protocols and strategies. This implies you don’t should reinvent the wheel to get the costs of cryptocurrencies. Moreover, this lets you save lots of growth time and sources, highlighting the essential position that APIs for crypto costs play within the blockchain area!
What to Consider When Selecting an API for Crypto Costs?
Since there are numerous APIs for crypto costs, it may be difficult to search out one of the best match for you. As such, that will help you on this endeavor, we’ll cowl three issues to consider when selecting an API:
Protection: When selecting an API, token protection is likely one of the most important issues. Subsequently, get your self an API for crypto costs overlaying a number of blockchain networks and several other DEXs. Options: Subsequent, you have to think about options when choosing the right API. Consequently, get an API that may fetch each real-time and historic crypto costs. What’s extra, search for extra advantages, similar to the flexibility to arrange alerts.
Value: Lastly, you have to think about the associated fee when selecting an API for crypto costs. The pricing is vital because it helps you narrow prices and keep aggressive within the quickly rising Web3 business.
Now that you realize what to contemplate when choosing an API for crypto costs, let’s have a look at some distinguished use instances for these instruments!
API for Crypto Costs – Use Circumstances
It doesn’t matter what Web3 mission you’re trying to construct, you’ll possible discover that you just want crypto value knowledge. Which means the use instances for crypto value APIs are countless. Nonetheless, to present you some inspiration, we’ll take this part to cowl three distinguished examples:
Portfolio Trackers: Portfolio trackers assist customers preserve monitor of their funds throughout blockchain networks and crypto wallets. This offers them an outline of how their belongings carry out over time. Consequently, when constructing one, you want entry to each real-time and historic costs of cryptocurrencies. Web3 Wallets: Web3 wallets, additionally generally known as crypto wallets, are dapps enabling customers to retailer and handle their digital belongings, together with fungible and non-fungible tokens. Furthermore, Web3 wallets additionally give customers an outline of their holdings, together with the present worth of all tokens. As such, identical to when constructing a portfolio tracker, you want entry to cost knowledge. Token Explorers: Token explorers – like Moralis Cash – permit customers to search out new funding alternatives. They normally let customers filter their searches for cash primarily based on a number of elements, one usually being value change over time. Consequently, you want entry to historic and real-time costs of cryptocurrencies when constructing a token explorer.
Abstract: Moralis’ API for Crypto Costs – Quickest Strategy to Get Costs of Crypto
Right now’s article explored Moralis’ free crypto Value API, which stands out because the business’s finest software for getting costs. Due to the accessibility of our Value API, you’ll be able to seamlessly get the costs of any cryptocurrencies in three steps:
Get a Moralis API Key for FreeWrite a Script Calling the getTokenPrice() EndpointRun the Code
Together with exhibiting you how you can get the real-time costs of any cryptocurrencies, we additionally confirmed you how you can get historic value knowledge and make batch requests for a number of tokens concurrently. As such, if in case you have adopted alongside this far, you now know how you can use one of the best API for getting crypto costs free of charge. From right here, you’ll haven’t any hassle integrating value knowledge into your tasks.
If you happen to discovered this tutorial on one of the best API for getting crypto costs attention-grabbing, think about testing extra growth content material right here on the Web3 weblog. As an example, learn our Rinkeby faucet information, learn to verify pockets exercise, or discover the idea of account abstraction!
Additionally, if you wish to construct sooner and smarter, discover some extra instruments Moralis affords. For instance, take a look at the NFT API and learn to get verified NFT collections or get all NFT tokens owned by a person tackle, all with single traces of code!
For entry to those instruments – together with one of the best API for getting crypto costs – join free with Moralis instantly!
[ad_2]
Source link