If you wish to get the worth of any token, it is best to try Moralis’ industry-leading Token API. With this growth software, you possibly can 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 deal with as an argument:
const response = await Moralis.EvmApi.token.getTokenPrice({
deal with,
});
By familiarizing your self with Moralis and the blockchain growth area, you possibly can seamlessly combine related 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 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, if you would like extra steering 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 shortly understand that you just want on-chain information relating to varied token costs. As an example, if you’re creating an change, ideally, you wish to show this info to enhance your prospects’ person experiences. So, how are you going to seamlessly question this information from the varied 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 wish to find out how this software works, be a part of us on this information as we present you precisely how!
To kick issues off, we are going to bounce straight into Moralis’ Token API and clarify why that is the perfect software to get any token worth. From there, earlier than exhibiting you ways the software works in apply, we dedicate a piece to exploring the intricacies of APIs and why they’re necessary. Lastly, we offer a complete tutorial on the way to get any token worth by exhibiting you the way to create an software implementing this performance. If this sounds thrilling, be a 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 possibly can learn concerning the intricacies of the Sepolia testnet or learn 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 comply with alongside!
Instruments to Get Any Token Value
Suppose you wish to get the worth of any token. In that case, you’ll possible wish to use Web3 growth instruments that may support on this course of because it has – from a traditional perspective – been fairly troublesome to question information from varied blockchain networks. Nonetheless, because the Web3 growth {industry} has advanced, that is not the case because of software program growth kits (SDKs), software programming interfaces (APIs), and different growth instruments.

Within the occasion of on-chain token information, it would be best to try the perfect token worth API in 2023 provided by Moralis. Moreover, that is the final word ERC20 token API, enabling you to combine real-time token information into your functions and Web3 tasks. Furthermore, with Moralis’ cross-chain capabilities, you might have the choice to make use of the API in growth endeavors throughout a number of completely different networks.
Nonetheless, the API has three fundamental options:
Token Transfers – With Moralis’ Token API, you possibly can keep updated with related token transfers. Token Balances – By this software, you possibly can question a person’s stability of ERC20 tokens with solely a single API name.Token Costs – The Token API collects costs from varied AMMs, resembling Uniswap and PancakeSwap, making them out there by solely a few strains of code.
Because of this third function, 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. Nonetheless, we are going to return to how the API and endpoint work within the ”Learn how 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 important to the online’s infrastructure, as most functions and platforms don’t work optimally in isolation. A technique of enabling software program programs to speak seamlessly is thru the usage of software programming interfaces (APIs)!
Each time you’re utilizing an software or shopping the online, your machine connects to the web with a purpose to ship directions to a database or server. The receiving finish then interprets the info, finds an applicable response, and returns it to your system. From there, your machine handles and processes the info, which is then displayed in a readable format.

This whole 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 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 are going to present an software 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 are going to train you the way to create an easy software utilizing NextJS and NodeJS. Within the software, customers can get any token worth primarily based on an deal with. Nonetheless, 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 the direction of, making it simpler to visualise what the code truly does.
With out additional ado, that is what the app’s touchdown web page appears to be like like:

The appliance options 4 core components: a header, two enter fields, and a ”Submit” button. So, how does the appliance work? The person must initially enter a token deal with 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 worth of the token in USD:

Nonetheless, now that you’ve got a extra profound understanding of what you’re working in the direction of, allow us to bounce straight into the tutorial on the way to get any token worth utilizing Moralis!
Learn how to Get Any Token Value
It’s time to present you the way to create this NodeJS and NextJS software. Moreover, on this tutorial, we are going to use Moralis’ Token API. Because of 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 are going to moreover use an already pre-developed venture 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 masking the sections above, you’ll find out how Moralis’ Token API works and the 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, should you want watching YouTube movies to study, try the clip under from the Moralis channel. In his video, a gifted Moralis software program engineer walks you thru the whole course of step-by-step:
In any other case, comply with alongside as we kickstart this NodeJS Web3 tutorial by exhibiting you the way to clone the venture to your native listing within the subsequent part!
Cloning the Mission
With a view to make this tutorial as simple as doable, we will probably be utilizing an already pre-made venture. Moreover, we’ll deal with the principle performance of the way to get any token worth. Consequently, on this preliminary part, we’re going to present you the way to arrange the venture. In the meantime, the next part gives a breakdown of the code!
To start with, open your built-in growth atmosphere (IDE) and create a brand new NodeJS venture folder. Subsequent, go to the GitHub repository down under and clone the venture to your native listing:
Full Documentation to Get Any Token Value – https://github.com/MoralisWeb3/youtube-tutorials/tree/fundamental/get-any-token-price
From there, with a replica of the venture in your native machine, it’s essential to add your Moralis API key as an atmosphere variable. To take action, create a ”.env” file within the venture’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 change ”replace_me” together with your precise Moralis API key. To get ahold of your key, you want a Moralis account. As such, you probably have not already, join with Moralis. From there, it is possible for you to to search 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 a couple of, and you will get all of them by opening a brand new terminal and working the next command:
npm i moralis @moralisweb3/common-evm-utils
Code Breakdown
After establishing the venture by including your Moralis API key and putting in the dependencies, you possibly can launch the appliance. 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 operate is named ”handleSubmit()” and may be discovered within the ”fundamental.js” file:
const handleSubmit = async () => {
deal with = 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({
deal with,
chain,
});
setResult(`$ ${response.toJSON().usdPrice}`);
setShowResult(true);
setChainValue(“”);
doc.querySelector(“#contractAddress”).worth = “”;
};
The operate is comparatively simple. Nonetheless, allow us to break it all the way down to get a extra profound understanding of what’s going on!
To start with, on the preliminary two strains throughout the operate’s curly brackets, we specify the ”deal with” and ”chain” variables. The deal with 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 ”deal with” 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 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 principle logic used to get any token worth. Aside from that, the rest of the code is liable for the required imports and the varied UI components. As such, if you would like a extra detailed breakdown of those, make certain to observe the video from the ”Learn how to Get Any Token Value” part!
Now, all that is still is so that you can take a look at the appliance and apply your new abilities in future growth endeavors!
Abstract – Learn how to Get Any Token Value
This text taught you the way to create an software permitting customers to get the USD worth of any token. Additional, they might accomplish that by inputting an deal with, deciding on a community, and hitting a button. In doing so, you discovered how Moralis’ Token API works and the way to use it to name the ”getTokenPrice” endpoint. Actually, because of the accessibility of Moralis, you have been in a position to get any token worth by a single API name to this endpoint:
const response = await Moralis.EvmApi.token.getTokenPrice({
deal with,
});
In case you discovered this tutorial useful, now we have suggestions for related articles that you just may discover fascinating. For instance, learn to get wallets native crypto stability or get token metadata very quickly! What’s extra, if you’re critical about changing into a blockchain developer, test the opposite Web3 APIs Moralis provides. The Web3 Streams API is a wonderful instance. This software lets you stream any on-chain information into the backend of your tasks through Web3 webhooks.
As such, if you wish to entry these enterprise-grade growth instruments, join with Moralis now. You may create an account totally free and instantly leverage the total potential of blockchain know-how!