Sunday, December 22

Realtime Exchange Rates With Currencylayer

Ever wanted an accurate and reliable currency data that can be easily integrated into your business; be it CRM, ERP, accounting applications etc., built in any programming languages? Then say hello to currencylayer.

In this article, I will show you how to use two of currencylayer’s services: live exchange rates and historical exchange rates.

What is currencylayer?

Currencylayer provides a reliable exchange rates and currency conversion for your business. It does these by providing an easy to integrate REST API with real-time and historical exchange rates for 168 world currencies and precious metals, delivered in universally usable and easily parseable JSON format, and compatible with any application.

The universal real-time exchange rate data solution

In addition to providing update to date real-time exchange rates, currencylayer also provides historical exchange rates for all of their supported 168 world currencies. They provide historical exchange rates for way back as the past 16 years.

So what makes currencylayer perfect for your business? Well, here are some reasons:

  • Reliable forex rates
  • Easy-to-integrate API
  • 256-bit HTTPS
  • Advanced data coverage
  • Real-time updates
  • Extended usage statistics
  • Easy on your budget
  • Interactive documentation

Did I mention that you can use most of these services completely for free? Sure you can! currencylayer has a free plan. With the free plan, you can make update to 1,000 API requests and the data are updated every hour.

Getting started

To start using the services provided by currencylayer, you need to first create an account with them. So head over to the subscription plans page and signup for a free plan.

Once you have signed up, you will be presented with a 3-step quickstart guide. Step 1 contains your Access Key, which you will use to interact with ****the currencylayer API. In step 2, you are presented with the base URL of the currencylayer API:


http://apilayer.net/api/

Take note of your access key and the base URL as we’ll be using them in the next section.

Fetching live exchange rates

To get live exchange rates, we need to make request to the live endpoint of the currencylayer API. That is:


http://apilayer.net/api/live

We need to pass our access key to the above URL.

The sample code in this article will be in Node.js and using Axios as the HTTP client:

const axios = require('axios')

const ACCESS_KEY = 'YOUR_ACCESS_KEY'

axios.get(`http://apilayer.net/api/live?access_key=${ACCESS_KEY}&currencies=USD,EUR,GBP,AUD,NGN`).then(response => {
    console.log(response.data)
})

Here, we specify we want to get the exchange rates for five currencies: USD, EUR, GBP, AUD, and NGN. By default, USD will be used as the source. That is, the currency to which all exchange rates are relative. We can as well specify as different source:


http://apilayer.net/api/live?access_key=YOUR_ACCESS_KEY&source=GBP

Below is the response gotten from the above request:

{
  "success": true,
  "terms": 'https://currencylayer.com/terms',
  "privacy": 'https://currencylayer.com/privacy',
  "timestamp": 1526743687,
  "source": 'USD',
  "quotes": {
     "USDUSD": 1,
     "USDEUR": 0.849404,
     "USDGBP": 0.74245,
     "USDAUD": 1.331041,
     "USDNGN": 358.000344
  }
}

The response is pretty straightforward. The exchange rates are inside the quotes object.

Fetching historical exchange rates

We have seen how to get live exchange rates. Now, let’s see how to fetch exchange rates from the past. For this, we make request to the historical of the currencylayer API, passing in our access key and a date in the past:

const axios = require('axios')

const ACCESS_KEY = 'YOUR_ACCESS_KEY'

axios.get(`http://apilayer.net/api/historical?access_key=${ACCESS_KEY}&date=2010-05-19&currencies=USD,EUR,GBP,AUD,NGN`).then(response => {
    console.log(response.data)
})

Here, we specify we want to get the exchange rates for a particular date (2010-05-19) in the past.

Below is the response gotten:

{
  "success": true,
  "terms": "https://currencylayer.com/terms",
  "privacy": "https://currencylayer.com/privacy",
  "historical": true,
  "date": "2010-05-19",
  "timestamp": 1274313599,
  "source": "USD",
  "quotes": {
    "USDUSD": 1,
    "USDEUR": 0.809694,
    "USDGBP": 0.69632,
    "USDAUD": 1.184999,
    "USDNGN": 152.073107
  }
}

In addition to the standard result sets, an historical API response contains an historical key with a value of true and the specified date.

Conclusion

In this article, we have seen what currencylayer is, the service it provides and some reasons to use it in your business. Though, we only looked at two of the services currencylayer provides. In addition to live and historical exchange rates, currencylayer can also handle currency conversion and lot more.

For more information about currencylayer, do check out their website and their easy to understand documentation.


Source: Scotch

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x