If you wish to get the worth of any token, you must try Moralis’ industry-leading Token API. With this growth software, you may question the present and historic value of any token very quickly! Additional, you are able to do so with an API name to Moralis’ “getTokenPrice” endpoint whereas passing an handle as an argument:
const response = await Moralis.EvmApi.token.getTokenPrice({ handle, });
By familiarizing your self with Moralis and the blockchain growth area, you may seamlessly combine comparable performance into any future Web3 tasks to get the worth 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 value documentation web page outlining the intricacies of this software. You too can enroll with Moralis and implement the code snippet above immediately. Nevertheless, if you would like extra steerage on implementing the above, learn on and let Moralis information you from begin to end!

Overview
When creating any Web3 platform or decentralized software (dapp), you’ll rapidly understand that you simply want on-chain knowledge relating to numerous token costs. For example, if you’re creating an trade, ideally, you wish to show this info to enhance your clients’ person 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 lets you get any token value by means of one API name. If this sounds fascinating and also you wish to find out how this software works, be part of us on this information as we present you precisely how!
To kick issues off, we’ll bounce straight into Moralis’ Token API and clarify why that is the perfect software to get any token value. From there, earlier than displaying you the way the software works in follow, we dedicate a bit to exploring the intricacies of APIs and why they’re essential. Lastly, we offer a complete tutorial on the best way to get any token value by displaying you the best way to create an software implementing this performance. If this sounds thrilling, be part of us as we cowl this course of from begin to end.
As well as, if you’re in search of 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 operate!
Now, earlier than persevering with, make certain to enroll with Moralis instantly, as you want an account to observe alongside!
Instruments to Get Any Token Value
Suppose you wish to get the worth of any token. In that case, you’ll probably wish to use Web3 growth instruments that may help on this course of because it has – from a standard perspective – been fairly troublesome to question knowledge from numerous blockchain networks. Nevertheless, because the Web3 growth {industry} has developed, that is not the case due to software program growth kits (SDKs), software programming interfaces (APIs), and different growth instruments.

Within the occasion of on-chain token knowledge, it would be best to try the perfect token value 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 tasks. Furthermore, with Moralis’ cross-chain capabilities, you will have the choice to make use of the API in growth endeavors throughout a number of totally different networks.
Nonetheless, the API has three primary options:
- Token Transfers – With Moralis’ Token API, you may keep updated with related token transfers.
- Token Balances – By this software, you may question a person’s stability of ERC20 tokens with solely a single API name.
- Token Costs – The Token API collects costs from numerous AMMs, akin to Uniswap and PancakeSwap, making them out there by means of solely a few strains of code.
Due to this third characteristic, it’s doable to make use of the API to get the worth of any token. What’s extra, all it requires is a single API name to the ”getTokenPrice” endpoint. Nevertheless, we’ll return to how the API and endpoint work within the ”The way to 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 wish to use it!
APIs
Cross-system communication is significant to the online’s infrastructure, as most purposes and platforms don’t work optimally in isolation. A technique of enabling software program programs to speak seamlessly is thru using software programming interfaces (APIs)!
Each time you’re utilizing an software or searching the online, your machine connects to the web in an effort to ship directions to a database or server. The receiving finish then interprets the information, finds an applicable response, and returns it to your system. From there, your machine handles and processes the information, which is then displayed in a readable format.

This complete course of is usually facilitated by an API, enabling two decoupled programs to speak seamlessly by translating a set of directions for mutual understanding. What’s extra, APIs ship features reliably on request. This ensures suitable coding in protected and safe environments. As such, by means of APIs, builders obtain a extra accessible growth expertise!
Nonetheless, that briefly covers the intricacies of APIs and why they’re helpful. Within the following part, we’ll present an software demo. It is going to present you what you may be working in direction of earlier than we dive into the central a part of this tutorial!
Get Token Value – What Are You Working In direction of?
Within the upcoming part, we’ll educate you the best way to create a simple software utilizing NextJS and NodeJS. Within the software, customers can get any token value primarily based on an handle. Nevertheless, earlier than diving into the tutorial, this part will present an software demo. In doing so, you’ll get a extra profound understanding of what you’re working in 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 seems like:

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

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

Nonetheless, now that you’ve got a extra profound understanding of what you’re working in direction of, allow us to bounce straight into the tutorial on the best way to get any token value utilizing Moralis!
The way to Get Any Token Value
It’s time to present you the best way to create this NodeJS and NextJS software. Moreover, on this tutorial, we’ll use Moralis’ Token API. Due to this enterprise-grade Web3 growth software, it’s doable to get the worth of any token with a single API name to the ”getTokenPrice” endpoint.
We’ll moreover use an already pre-developed challenge to make this walkthrough as simple as doable. Furthermore, we will divide the tutorial into the next two sections:
- Cloning the Undertaking
- Code Creakdown
By following alongside and overlaying the sections above, you’ll find out how Moralis’ Token API works and the best way to use this growth software to seamlessly get the worth of any token. From there, it is possible for you to to use the identical ideas to simply implement this performance into any of your future Web3 growth tasks!
Now, if you happen to want 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 entire course of step-by-step:
In any other case, observe alongside as we kickstart this NodeJS Web3 tutorial by displaying you the best way to clone the challenge to your native listing within the subsequent part!
Cloning the Undertaking
With a view to make this tutorial as simple as doable, we might be utilizing an already pre-made challenge. Moreover, we’ll give attention to the principle performance of the best way to get any token value. Consequently, on this preliminary part, we’re going to present you the best way to arrange the challenge. In the meantime, the next part offers a breakdown of the code!
To start with, open your built-in growth atmosphere (IDE) and create a brand new NodeJS challenge folder. Subsequent, go to the GitHub repository down beneath and clone the challenge to your native listing:
Full Documentation to Get Any Token Value – https://github.com/MoralisWeb3/youtube-tutorials/tree/primary/get-any-token-price
From there, with a duplicate of the challenge in your native machine, you want to add your Moralis API key as an atmosphere variable. To take action, create a ”.env” file within the challenge’s root folder and add the next variable on the high:
NEXT_PUBLIC_MORALIS_API_KEY = ”replace_me”
What’s extra, as you may have already got figured, you have to change ”replace_me” along with your precise Moralis API key. To get ahold of your key, you want a Moralis account. As such, you probably have not already, enroll 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 got added your Moralis key as a variable, the final step is to put in the required dependencies. There are just a few, 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 establishing the challenge by including your Moralis API key and putting in the dependencies, you may launch the applying. Nevertheless, 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 value. The central operate known as ”handleSubmit()” and will be discovered within the ”primary.js” file:
const handleSubmit = async () => { handle = 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({ handle, chain, }); setResult(`$ ${response.toJSON().usdPrice}`); setShowResult(true); setChainValue(""); doc.querySelector("#contractAddress").worth = ""; };
The operate is comparatively simple. Nevertheless, 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 operate’s curly brackets, we specify the ”handle” and ”chain” variables. The handle and chain values are fetched from the enter fields of the app’s UI. From there, we name the ”Moralis.begin()” operate 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 operate. There, an API name is made to Moralis’ ”getTokenPrice” endpoint, passing the ”handle” and ”chain” variables as arguments. The return worth is saved within the ”response” object. Because of this, this object now accommodates a bunch of information, together with the token value in USD.
Lastly, to high issues off, the code console logs the USD value from the ”response” object. Additionally, it clears the enter fields of the app’s UI.
That covers the principle logic used to get any token value. Apart from that, the rest of the code is chargeable for the required imports and the assorted UI components. As such, if you would like a extra detailed breakdown of those, make certain to observe the video from the ”The way to 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 – The way to Get Any Token Value
This text taught you the best way to create an software permitting customers to get the USD value of any token. Additional, they may achieve this by inputting an handle, deciding on a community, and hitting a button. In doing so, you realized how Moralis’ Token API works and the best way to use it to name the ”getTokenPrice” endpoint. Actually, due to the accessibility of Moralis, you have been capable of get any token value by means of a single API name to this endpoint:
const response = await Moralis.EvmApi.token.getTokenPrice({ handle, });
In case you discovered this tutorial useful, we now have suggestions for comparable 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, if you’re severe about turning into a blockchain developer, examine the opposite Web3 APIs Moralis provides. The Web3 Streams API is a superb instance. This software lets you stream any on-chain knowledge into the backend of your tasks by way of Web3 webhooks.
As such, if you wish to entry these enterprise-grade growth instruments, enroll with Moralis now. You possibly can create an account totally free and instantly leverage the complete potential of blockchain expertise!