[ad_1]
If you wish to get the value of any token, it is best to try Moralis’ industry-leading Token API. With this growth software, you may question the present and historic worth of any token very quickly! Additional, you are able to do so with an API name to Moralis’ “getTokenPrice” endpoint whereas passing an tackle as an argument:
const response = await Moralis.EvmApi.token.getTokenPrice({
tackle,
});
By familiarizing your self with Moralis and the blockchain growth area, you may seamlessly combine related performance into any future Web3 initiatives to get the value of any token! Furthermore, if you wish to study extra about how the Token API and ”getTokenPrice” endpoint work, try the official get ERC20 token worth documentation web page outlining the intricacies of this software. It’s also possible to join with Moralis and implement the code snippet above immediately. Nonetheless, in order for you further steering on implementing the above, learn on and let Moralis information you from begin to end!

Overview
When growing any Web3 platform or decentralized utility (dapp), you’ll shortly notice that you simply want on-chain knowledge concerning varied token costs. For example, in case you are creating an alternate, ideally, you need to show this data to enhance your clients’ consumer experiences. So, how will you seamlessly question this knowledge from the assorted blockchain networks and use it in your growth endeavors? A solution to this query is Moralis’ Token API. This API allows you to get any token worth by one API name. If this sounds fascinating and also you need to learn the way this software works, be part of us on this information as we present you precisely how!
To kick issues off, we’ll soar straight into Moralis’ Token API and clarify why that is the most effective software to get any token worth. From there, earlier than displaying you ways the software works in observe, we dedicate a piece to exploring the intricacies of APIs and why they’re necessary. Lastly, we offer a complete tutorial on tips on how to get any token worth by displaying you tips on how to create an utility implementing this performance. If this sounds thrilling, be part of us as we cowl this course of from begin to end.
As well as, in case you are on the lookout for extra Web3 growth content material, try Moralis’ Web3 weblog. On the weblog, you may learn concerning the intricacies of the Sepolia testnet or discover ways to use a Web3 JS name contract perform!
Now, earlier than persevering with, ensure to enroll with Moralis instantly, as you want an account to observe alongside!
Instruments to Get Any Token Value
Suppose you need to get the value of any token. In that case, you’ll seemingly need to use Web3 growth instruments that may support on this course of because it has – from a standard perspective – been fairly troublesome to question knowledge from varied blockchain networks. Nonetheless, because the Web3 growth {industry} has developed, that is not the case because of software program growth kits (SDKs), utility programming interfaces (APIs), and different growth instruments.

Within the occasion of on-chain token knowledge, it would be best to try the most effective token worth API in 2023 provided by Moralis. Moreover, that is the last word ERC20 token API, enabling you to combine real-time token knowledge into your purposes and Web3 initiatives. Furthermore, with Moralis’ cross-chain capabilities, you could have the choice to make use of the API in growth endeavors throughout a number of totally different networks.
However, the API has three major options:
Token Transfers – With Moralis’ Token API, you may keep updated with related token transfers. Token Balances – By way of this software, you may question a consumer’s stability of ERC20 tokens with solely a single API name.Token Costs – The Token API collects costs from varied AMMs, similar to Uniswap and PancakeSwap, making them out there by solely a few strains of code.
Due to this third function, it’s doable to make use of the API to get the value of any token. What’s extra, all it requires is a single API name to the ”getTokenPrice” endpoint. Nonetheless, we’ll return to how the API and endpoint work within the ”How you can Get Any Token Value” part.
However, earlier than doing so, allow us to return to fundamentals and discover the intricacies of APIs. This helps us higher perceive the utility of Moralis’ Token API and why you may need to use it!
APIs
Cross-system communication is significant to the net’s infrastructure, as most purposes and platforms don’t work optimally in isolation. A method of enabling software program methods to speak seamlessly is thru the usage of utility programming interfaces (APIs)!
At any time when you’re utilizing an utility or searching the net, your system connects to the web with the intention to ship directions to a database or server. The receiving finish then interprets the information, finds an acceptable response, and returns it to your system. From there, your system handles and processes the information, which is then displayed in a readable format.

This whole course of is mostly facilitated by an API, enabling two decoupled methods to speak seamlessly by translating a set of directions for mutual understanding. What’s extra, APIs ship capabilities reliably on request. This ensures appropriate coding in protected and safe environments. As such, by APIs, builders obtain a extra accessible growth expertise!
However, that briefly covers the intricacies of APIs and why they’re helpful. Within the following part, we’ll present an utility demo. It’s going to present you what you may be working in the direction of earlier than we dive into the central a part of this tutorial!
Get Token Value – What Are You Working In the direction of?
Within the upcoming part, we’ll educate you tips on how to create a simple utility utilizing NextJS and NodeJS. Within the utility, customers can get any token worth primarily based on an tackle. Nonetheless, earlier than diving into the tutorial, this part will present an utility demo. In doing so, you’re going to get a extra profound understanding of what you’re working in the direction of, making it simpler to visualise what the code really does.
With out additional ado, that is what the app’s touchdown web page appears to be like like:

The applying options 4 core components: a header, two enter fields, and a ”Submit” button. So, how does the applying work? The consumer must initially enter a token tackle into the ”Add ERC20 Contract Handle” discipline and choose a sequence utilizing the ”Choose Chain” drop-down menu. On this occasion, there are two choices, ”Ethereum” and ”Goerli”:

As soon as each enter fields are crammed in, all that is still is to hit the ”Submit” button, which ought to return the value of the token in USD:

However, now that you’ve a extra profound understanding of what you’re working in the direction of, allow us to soar straight into the tutorial on tips on how to get any token worth utilizing Moralis!
How you can Get Any Token Value
It’s time to present you tips on how to create this NodeJS and NextJS utility. Moreover, on this tutorial, we’ll use Moralis’ Token API. Due to this enterprise-grade Web3 growth software, it’s doable to get the value of any token with a single API name to the ”getTokenPrice” endpoint.
We’ll moreover use an already pre-developed mission to make this walkthrough as simple as doable. Furthermore, we are able to divide the tutorial into the next two sections:
Cloning the ProjectCode Creakdown
By following alongside and protecting the sections above, you’ll learn the way Moralis’ Token API works and tips on how to use this growth software to seamlessly get the value of any token. From there, it is possible for you to to use the identical rules to simply implement this performance into any of your future Web3 growth initiatives!
Now, in the event you favor watching YouTube movies to study, try the clip beneath from the Moralis channel. In his video, a proficient Moralis software program engineer walks you thru the whole course of step-by-step:
In any other case, observe alongside as we kickstart this NodeJS Web3 tutorial by displaying you tips on how to clone the mission to your native listing within the subsequent part!
Cloning the Undertaking
So as to make this tutorial as simple as doable, we will likely be utilizing an already pre-made mission. Moreover, we’ll give attention to the primary performance of tips on how to get any token worth. Consequently, on this preliminary part, we’re going to present you tips on how to arrange the mission. In the meantime, the next part gives a breakdown of the code!
To start with, open your built-in growth setting (IDE) and create a brand new NodeJS mission folder. Subsequent, go to the GitHub repository down beneath and clone the mission to your native listing:
Full Documentation to Get Any Token Value – https://github.com/MoralisWeb3/youtube-tutorials/tree/major/get-any-token-price
From there, with a replica of the mission in your native system, you might want to add your Moralis API key as an setting variable. To take action, create a ”.env” file within the mission’s root folder and add the next variable on the prime:
NEXT_PUBLIC_MORALIS_API_KEY = ”replace_me”
What’s extra, as you may have already got figured, you need to exchange ”replace_me” together with your precise Moralis API key. To get ahold of your key, you want a Moralis account. As such, when you’ve got not already, join with Moralis. From there, it is possible for you to to seek out your key by logging in and navigating to the ”Web3 APIs” tab:
Now that you’ve added your Moralis key as a variable, the final step is to put in the required dependencies. There are a couple of, and you may get all of them by opening a brand new terminal and operating the next command:
npm i moralis @moralisweb3/common-evm-utils
Code Breakdown
After organising the mission by including your Moralis API key and putting in the dependencies, you may launch the applying. Nonetheless, earlier than doing so, allow us to break down the important a part of the code dealing with the logic used to get any token worth. The central perform is known as ”handleSubmit()” and will be discovered within the ”major.js” file:
const handleSubmit = async () => {
tackle = doc.querySelector(“#contractAddress”).worth;
const chain = EvmChain.ETHEREUM;
await Moralis.begin({
apiKey: course of.env.NEXT_PUBLIC_MORALIS_API_KEY,
});
const response = await Moralis.EvmApi.token.getTokenPrice({
tackle,
chain,
});
setResult(`$ ${response.toJSON().usdPrice}`);
setShowResult(true);
setChainValue(“”);
doc.querySelector(“#contractAddress”).worth = “”;
};
The perform is comparatively simple. Nonetheless, allow us to break it right down to get a extra profound understanding of what’s going on!
To start with, on the preliminary two strains inside the perform’s curly brackets, we specify the ”tackle” and ”chain” variables. The tackle and chain values are fetched from the enter fields of the app’s UI. From there, we name the ”Moralis.begin()” perform passing the API key from the ”.env” file as an argument to initialize Moralis.
Subsequent up, we get to the central a part of this perform. There, an API name is made to Moralis’ ”getTokenPrice” endpoint, passing the ”tackle” and ”chain” variables as arguments. The return worth is saved within the ”response” object. In consequence, this object now accommodates a bunch of knowledge, together with the token worth in USD.
Lastly, to prime issues off, the code console logs the USD worth from the ”response” object. Additionally, it clears the enter fields of the app’s UI.
That covers the primary logic used to get any token worth. Apart from that, the rest of the code is accountable for the required imports and the assorted UI components. As such, in order for you a extra detailed breakdown of those, ensure to observe the video from the ”How you can Get Any Token Value” part!
Now, all that is still is so that you can take a look at the applying and apply your new abilities in future growth endeavors!
Abstract – How you can Get Any Token Value
This text taught you tips on how to create an utility permitting customers to get the USD worth of any token. Additional, they might achieve this by inputting an tackle, deciding on a community, and hitting a button. In doing so, you realized how Moralis’ Token API works and tips on how to use it to name the ”getTokenPrice” endpoint. In reality, because of the accessibility of Moralis, you had been capable of get any token worth by a single API name to this endpoint:
const response = await Moralis.EvmApi.token.getTokenPrice({
tackle,
});
In the event you discovered this tutorial useful, now we have suggestions for related articles that you simply may discover fascinating. For instance, discover ways to get wallets native crypto stability or get token metadata very quickly! What’s extra, in case you are severe about changing into a blockchain developer, examine the opposite Web3 APIs Moralis presents. The Web3 Streams API is a wonderful instance. This software permits you to stream any on-chain knowledge into the backend of your initiatives through Web3 webhooks.
As such, if you wish to entry these enterprise-grade growth instruments, join with Moralis now. You may create an account free of charge and instantly leverage the total potential of blockchain expertise!
[ad_2]
Source link