September 16, 2024

Use NodeJS to retrieve current Ethereum price

Want an easy way to get ETH price in NodeJS? This coding tutorial is for you! We will use 2 free APIs in order to get the updated ETH price!

First of all, hello everyone! Just got back from my holiday and I am really excited to start another tutorial. In this short lesson I will show you how to get the current price of Ethereum cryptocurrency in NodeJS and display it on the screen. We will use 2 free APIs for that, nothing fancy hence this lesson is super easy.

The code for this tutorial can be found on my Github. I do encourage you to send me any feedback!

Let’s dive in

In case you have an empty folder with no NPM project, you will need to initialize a new project with the following npm command:

$ npm init

Just run the above in your current folder and it will ask you for some information like the name, version, keywords and license. Fill these values and after entering yes at the end, the tool will create a new package.json file for you.

Great, now that the first step is now complete, you will need to install Axios:

$ npm install axios

Axios is a NodeJS library for HTTP calls which we will use to call these two APIs:

  1. Cryptocompare
  2. Coinbase

Creating the main program

In order to work, we will need to create the main Javascript file which we will call index.js, but can be anything else, it’s up to the developer 😊

First of all we will need to import our Axios library to use it in our program:

const axios = require('axios')

Since Axios is based on Promises, we will use the async await pattern for a cleaner coding style. The pattern can only be used in an async method, hence we need to declare an async main and call it at the end of our file. The structure of the Javascript file so far looks like this:

const axios = require('axios')

async function main() {
  // We write the main API calls here
}

// We need to call the main function at the end of the file
main();

Now the next step is to call the aforementioned APIs.

Calling the first API

The first API is provided by CryptoCompare. It’s very fast and easy to use and we specify in the URL query string the crypto currency we want the rate for and the equivalent in fiat currency. We assign the response object to a constant called respCryptoCompare.

const respCryptoCompare = await axios.get('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR');

In order to get the data, we use the data property whose value we assign it to the constant dataCryptoCompare:

const dataCryptoCompare = respCryptoCompare.data;

Now that we have the data stored in our constant, we can display it in the console. The API endpoint returns us the rates of EUR and USD in the corresponding properties:

console.log(`CryptoCompare: ETH price is ${dataCryptoCompare.EUR} EUR and ${dataCryptoCompare.USD} USD`);

One thing to note with this API is that it might be capped to a certain number of requests per IP, so use it sparingly.

Calling the second API

Coinbase is the provider of the second API. As far as I know there is no limit for it. The response structure is a bit different, meaning that it will return an object with all the rates for different currencies (crypto and fiat alike). We call it in the following fashion as we assign it to the respCoinbase constant:

const respCoinbase = await axios.get('https://api.coinbase.com/v2/exchange-rates?currency=ETH');

In a very similar fashion we also get the response data, with the difference that we have to navigate to data -> rates object. Look at the following example to see what I mean:


const dataCoinbase = respCoinbase.data.data.rates;

Cool, now what’s left to do is to show the exchange rate on the console:

console.log(`Coinbase: ETH price is ${dataCoinbase.EUR} EUR and ${dataCoinbase.USD} USD`);

Let’s see it in action

Finally, let’s run the program and see the output. At the time of this writing, the output looks like this πŸ™‚

We can observe the program runs smoothly and prints correct values to the screen. There can be lots of further developments on top of this simple code, like creating a dashboard or even a price prediction with machine learning.

If you want to check more Javascript tutorials on my blog, please use this link.

So in other words, stay tuned for more articles and also use the subscribe form if you want to stay up to date with the latest!

I recently delegated the subscribe and contact features to my other domain which focuses more on services and blockchain apps, feel free to check out this article and the main website. Wish you all the best!

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored πŸ˜‚

View all posts by afivan →