Nerd For Tech
Published in

Nerd For Tech

Your First NodeJS REST API Client

We have nearly covered all popular REST API clients in our tutorials, such as Python, PHP, C#, and Golang. However, we missed the most obvious and most popular one - NodeJS. We aim to make amends by showing you a simple implementation of the NodeJS RESTful API Client using Axios. This tutorial assumes you have a very basic knowledge of Javascript syntax and some basic programming experience.

In this tutorial, you will learn the following:

  1. Download and Install Nodejs.
  2. Setup NodeJS (node.js) environment
  3. Setup a free account to get data from REST API.
  4. Retrieve data from an API
  5. Parse JSON data and display output.

Let’s Begin!

Download and install NodeJS

Windows and Mac:

  • Download NodeJs from Nodejs.org
  • Run the install and accept defaults

Linux:

  • log in as root or run as sudo user

apt-get install nodejs

Setup NodeJS (node.js) environment

Now that you have NodeJS installed check your install by typing:

node -v

you should see:

v14.18.1.

Create a directory for your program.

Open the command prompt or terminal and get the dependencies (libraries) that are needed to run our client. We will be using the “axios” library as it makes HTTP requests easy, so run the following:

npm i axios

We are now all set with the environment we need.

Set up an account and get an API Key

Sign up and create an account and you’ll be sent a welcome email. If you already are registered with us, use this dashboard to get the key.

Write REST Client and Retrieve Forex and CFD Data via REST API

Now that you have the environment setup navigate to the directory you created and create a file, I am going to call mine TMSRESTClient.js. Now open the file using your favourite code editor.

Ok, let’s start coding.

First, we will import the Axios library

const axios = require('axios');

Then we write a get command and pass in the URL for the live endpoints, you will need to substitute your API key in the code below and update the currency you wish to call. For this example, we will call a couple of Forex rates and a CFD rate as they return slightly different data.

axios.get('https://marketdata.tradermade.com/api/v1/live?currency=EURUSD,GBPUSD,UK100&api_key=YOUR_API_KEY')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});

To run the program we open a command prompt in the directory where the file is located and type the following command.

node TMSdataClient.js

you should get output similar to the following

{
endpoint: 'live',
quotes: [
{
ask: 1.16411,
base_currency: 'EUR',
bid: 1.1641,
mid: 1.1641,
quote_currency: 'USD'
},
{
ask: 1.38079,
base_currency: 'GBP',
bid: 1.38078,
mid: 1.38078,
quote_currency: 'USD'
},
{ ask: 7199.3,
bid: 7198.2,
instrument: 'UK100',
mid: 7198.75
}
],
requested_time: 'Thu, 21 Oct 2021 08:42:21 GMT',
timestamp: 1634805742
}

Voila! we now have live data from a REST API. However, this is raw JSON so now we will do a bit of work to iterate through the data and output it in a more readable format.

As you can see from the above JSON, the quotes object is an array of maps with keys and values. However, the CFD map only has an instrument key field while the currency map has base_currency and quote_currency keys. Hence, we will add an if statement to check for base_currency and parse the instrument when base_currency is not on the map. We then pull out the bid and ask from each quote and output it.

for (quote in response.data.quotes){
quoteData = response.data.quotes[quote];
ccy = ""
if ( quoteData["base_currency"] != undefined){
ccy = quoteData["base_currency"] + quoteData["quote_currency"]
}else{
ccy = quoteData["instrument"]
}
console.log(" Symbol " + ccy + " Bid " + quoteData["bid"] + " Ask " + quoteData["ask"])
}

If you run the program again you should see the following parsed output.

Symbol EURUSD Bid 1.16416 Ask 1.16417
Symbol GBPUSD Bid 1.38078 Ask 1.38079
Symbol UK100 Bid 7199.4 Ask 7200.6

In this example, we used the live endpoint but the same code with small changes will work on all other TraderMade endpoints, Historical Rates, Tick Historical Rates, Minute Historical Rates, Hour Historical Rates and Time-Series.

Please clap our work and comment. We are open to suggestions and ideas and we always look forward to hearing from you.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rahul Khanna

Rahul Khanna

Chief Technology Officer (CTO), TraderMade