How To Integrate Exchange Rates And Currency Conversion In NodeJS

If you are going to build a trading platform or some kind of business app which needs to have the best reliable exchange rates & currency conversion features, you should go with the CurrencyLayer.

Here is our demo app for today:

What is CurrencyLayer?

CurrencyLayer is one of the market-leading exchange rates and currency conversion RESTful web services available in the marketplace.

CurrencyLayer provides a simplistic REST API with real-time and historical exchange rates for 168 countries. 

  • Collected from various commercial sources & banks
  • Compatible with any application & programming languages
  • The best subscription plan and support

Why CurrencyLayer API?

Here are some features you should know about the CurrencyLayer API.

  • Official Exchange Rates for more than 160 countries with data updates ranging from every 60 minutes down to stunning 60 seconds.
  • Easy To Integrate API
  • Depending on your subscription plan you can get different data or can utilize all features
  • Free test subscription plans and inexpensive Startup-friendly subscription plans

Reduce Technical Debt By Integrating APIs!

apilayer APIs helping millions of developers around the world by providing robust RESTful web services. 

You can achieve high productivity in no time with APIs. There is no need to build everything from the beginning. And you can reduce technical debt by applying different approaches like integrating APIs and automate data manipulation. 

Since apilayer services are used by top companies around the globe and integrated into millions of projects, you can have a long-term vision with these APIs!

How to use the CurrencyLayer API?

As we have said, CurrencyLayer API can be integrated with any platform because of its flexible and simple API endpoints.

Here is an example of sending a request to a main endpoint of the API.

And here is the response that you usually get

As you can see it gives you some legal reminders, the source of the currency, and a timestamp. Well, the essential part is quotes object.

quotes – It includes all exchange rate values, consisting of the currency pairs and their respective conversion rates. And we are going to work with that.

CurrencyLayer API Response Structure

Let’s test the API endpoint.

How to integrate exchange rates & currency conversion features in the Node.js web app?

Starting a new project always feels good but finishing it is the hard part! So in this demonstration, we try to show you a real-world Node.js web app that teaches you several valuable stuff:

  • Setup Node.js web app
  • Configuration
  • Integration of an API
  • Embedded JavaScript
  • Parsing JSON response
  • Presenting response in the classic style
  • and more

Here is what we create today!

How to set up Node.js with CurrencyLayer API?

Open your terminal, PowerShell or Bash. And type these commands to create a folder and initialize the basic project files

Then go back to your terminal and type this command to install the required node modules.

Now in this part, I would recommend you to copy static (css, js, images) files from our repository. And here is our project file structure:

Moreover, I would recommend you install nodemon. The nodemon is a tool that helps develop Node.js-based applications by automatically restarting the node application when file changes are detected. It improves your productivity.

Here you can follow these steps to configure nodemon.

Now, you can go back to your code editor and write this code for your app.js

As you can see, we are creating an ExpressJS-based app and specified the port. Then we are configuring static files. The “express.static” will help us to serve all static files in the public folder.

Then we declared the routes and views.

So, the main part is the exchangerates.js file. In this router handler, we fetch data from CurrencyLayer API and send data to the exchangerates view.

I hope that the comments are helpful.

How to iterate key-value pair data and present it in HTML5?

Here I will show you how to present key-value pair data in a table by iterating the elements. (You can find the whole exchangerates.ejs file from the repository)

As you can see, utilizing the Object.entries() method returns an array of a given object’s enumerable string keyed property pairs. This is like for…in.

Moreover, there is a dashboard feature that you can utilize and create more functionalities if your ideas to continue with CurrencyLayer API. It can be a boilerplate for your project!

And here is our result in action:

https://www.youtube.com/watch?v=rXmnZyE3hMs

Check out the full source code in this repository.

Are you ready to build apps using a quick and cost-effective JSON API for your exchange rates and currency conversion data requirements?

Ready to get started with CurrencyLayer? Sign up!