DEV Community

Cover image for How to Write Your First JavaScript REST Client (Server Side)
Shridhar G Vatharkar
Shridhar G Vatharkar

Posted on

How to Write Your First JavaScript REST Client (Server Side)

This tutorial demonstrates how to develop a straightforward JavaScript server-side application using NodeJS and Axios. This lesson will presume you know very little to nothing about NodeJS, but it would be beneficial if you had some programming expertise with JavaScript syntax. If JavaScript and NodeJS (node.js) are not your preferred languages, we also offer Python, PHP, C#, and Golang implementations.

Before we begin installing the necessary parts. Let's quickly recap what the tutorial is intended to accomplish. The first step in setting up our development environment is to download and install NodeJS (node.js), after which we will write some code to access the TraderMade REST API and obtain real-time forex and CFD (Indices) data (Server-Side).

Overview of the tutorial
Download, install, and set up the environment for Node.js
Start a free trial of the TraderMade Data API (1000 free calls per month forever....)
Establish NodeJS( node.js) for data retrieval
Data parsing and display on screen

Let's get going

Installing NodeJS (node.js) on a Mac or Windows computer:

Visit Nodejs.org to get NodeJs.
Run the installation and abide by the settings.
Type node in the command prompt to begin.

Linux:
Run apt-get install Nodejs as root or a sudo user.

Now type node -v to verify your installation. You'll receive something akin to v14.8.1
For your software and the related libraries, make a directory.
Open the command window or terminal after installing Nodejs to obtain the dependencies (libraries) required to start our client. Run the following command to make an HTTP request using the axios library:

npm i axios
Enter fullscreen mode Exit fullscreen mode

Create an account and Obtain an API Key.

You can register and create an account to get a welcome email. Log into your dashboard if you have already registered with us to obtain the key.
NodeJS (node.js) REST Client installation and Obtain current Forex and CFD data.

The environment is now set up. Go to the directory you just made and start a file there. My programme will be known as TMSDataClient.js. Notepad, Atom VS Code, VI on Linux, or your preferred editor should all be able to open the file.

Now let's get to coding.
We'll import the Axios library first.

const axios = require('axios');

Enter fullscreen mode Exit fullscreen mode

The URL for the live endpoints is then passed to a get command that we create. You must adjust the currency you want to call and replace your API key in the following code. We will use a few FX pairs and a CFD in this example because they both produce slightly different results.

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);
  });

Enter fullscreen mode Exit fullscreen mode

We open a command prompt in the directory where the file is located and enter the following command to start the program.

node TMSdataClient.js

Enter fullscreen mode Exit fullscreen mode

You should see results resembling these:

{
  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
}

Enter fullscreen mode Exit fullscreen mode

This is raw JSON, so we will now spend some effort processing the JSON data iteratively and producing the output in a more readable way. Since CFD only has a single instrument file and not base currency and quote currency, we add an if statement to check for base currency. The bid and ask values for each quote are then extracted and output.

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"])
}

Enter fullscreen mode Exit fullscreen mode

If we rerun the programme, we obtain 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

Enter fullscreen mode Exit fullscreen mode

We employed the live endpoint in this illustration. However, the same code will operate on all other TraderMade endpoints with very slight adjustments. For further details, see our documentation page.

Any programming language that users have is something we are prepared to assist with. Please get in touch if you have any recommendations or thoughts. We forward hearing from you in the future.

Also, go through our other tutorials:
Forex Crypto and CFD REST JSON with Java

Data Visualization Python

How to Write Your First JavaScript REST Client (Server Side)

Top comments (0)