Currency conversion APIs are frequently used by many applications. With its Currency conversion APIs, applications and websites provide many conveniences to their users. For example, in an e-commerce application with international users, the default currency is converted to the official currency in the user’s location, or it allows users in foreign exchange trading applications to instantly convert between hundreds of currencies.
There are many services that provide Currency conversion API. The most popular and preferred service among these services is the fixer API. In this article, we will get to know the Fixer API and perform a simple integration with the Nodejs application.
What is Fixer API
Fixer API is one of the most popular currency conversion services. Today, it is preferred by many popular companies. It supports about 170 official currencies around the world. It allows instant conversions between 170 official currencies in just milliseconds.
The data provided by Fixer API is obtained from official sources around the world. For these reasons, the accuracy rate of the data provided is quite high. In addition, Fixer API provides historical data of currencies. It increases customer satisfaction by informing users about historical currency data with the historical data provided.
In addition, Fixer API has been developed with a developer-friendly approach. It provides a flexible and lightweight API. It also provides very powerful documentation for the API provided to developers.
For more information about Fixer API, click here.
Integration Fixer API with Nodejs
One of the biggest reasons why the Fixer API, which provides Currency conversion service, is preferred by the developers is that it is integrated into the project in just a few steps. We will integrate the Fixer API into a Nodejs application.
First of all, we need to sign up for a package on Fixer’s official website and get an API key. After obtaining this API key, let’s create a Nodejs application with the default settings with the following command line.
npm init |
Then we create the index.js file in the file location and run the following command.
npm i express request |
Now let’s paste the following codes into the index.js file.
const express = require(‘express’); const fixerRouter = require(‘./routes/fixer’); const app = express(); app.use(‘/api’, fixerRouter); var server = app.listen(3000, function() { console.log(‘App is running!’); }); |
Now we create the fixer.js file, where our application will make a request to Fixer, and paste the following codes.
const express = require(‘express’); const router = express.Router(); let request = require(‘request’); let apiKey = ‘c05*****ff’; router.get(‘/fixer/’, (req, res) => { let url = ` https://data.fixer.io/api/latest?access_key=${apiKey}`; request(url, function (err, response, body) { if(err){ console.log(‘error:’, error); } else { console.log(‘body:’, body); res.json(body); } }); }); router.get(‘/fixer/:from/:to/:amount’, (req, res) => { let from = req.params.from; let to = req.params.to; let amount = req.params.amount; let url = `https://data.fixer.io/api/convert?access_key=${apiKey}&from=${from}&to=${to}&amount=${amount}`; request(url, function (err, response, body) { if(err){ console.log(‘error:’, error); } else { console.log(‘body:’, body); res.json(body); } }); }); module.exports = router; |
Let’s run the application from the command line with the following command.
npm start |
Now, let’s send a request to the address below, either from the browser or from a web service testing tool such as Postman.
http://localhost:3000/api/fixer |
When we send a request to the above URL, we get the following response.
{ “success”: true, “timestamp”: 1661709243, “base”: “EUR”, “date”: “2022-08-28”, “rates”: { “AED”: 3.66047, “AFN”: 88.621084, “ALL”: 117.051432, “AMD”: 404.834954, “ANG”: 1.805122, “AOA”: 427.145242, “ARS”: 137.607384, “AUD”: 1.446179, “AWG”: 1.793812, “AZN”: 1.697159, “BAM”: 1.955854, “BBD”: 2.022325, “BDT”: 95.160364, “BGN”: 1.954989, “BHD”: 0.375605, “BIF”: 2065.765278, “BMD”: 0.996562, “BND”: 1.391717, “BOB”: 6.935985, “BRL”: 5.045716, “BSD”: 1.001612, “BTC”: 4.98149e-5, “BTN”: 79.938678, “BWP”: 12.759056, “BYN”: 2.528631, “BYR”: 19532.619009, “BZD”: 2.018925, “CAD”: 1.299119, “CDF”: 2014.052399, “CHF”: 0.962084, “CLF”: 0.032614, “CLP”: 899.921712, “CNY”: 6.848178, “COP”: 4402.353869, “CRC”: 632.397738, “CUC”: 0.996562, “CUP”: 26.408898, “CVE”: 110.266349, “CZK”: 24.621163, “DJF”: 178.302182, “DKK”: 7.43829, “DOP”: 53.178651, “DZD”: 140.317717, “EGP”: 19.124234, “ERN”: 14.948433, “ETB”: 52.838847, “EUR”: 1, [170 currencies] } } |
Now, let’s request the following URL for currency conversion.
http://localhost:3000/api/fixer/usd/try/2 |
When we send a request to the above URL, we get the following response.
{ “success”: true, “query”: { “from”: “USD”, “to”: “TRY”, “amount”: 2 }, “info”: { “timestamp”: 1661709243, “rate”: 18.164137 }, “historical”: “”, “date”: “2022-08-28”, “result”: 36.328274 } |
Conclusion
Fixer API, one of the most popular services providing Currency conversion services, is included in the project in just a few steps. You can provide currency conversion service to your users with an API key that you will obtain from the official website of Fixer API.
Recent Comments