Getting Exchange Rates using ExchangeRate-API

ExchangeRate-API is a public API that provides real-time exchange rates for different currencies. You can use it to get up-to-date exchange rates and support different currencies in your web or mobile applications. In this blog post, we'll show you how to use the ExchangeRate-API to get exchange rates in JavaScript.

Getting started

To get started, you'll need to sign up for a free API key from ExchangeRate-API. Once you have your API key, you can start making requests to the API.

API Usage

To use the API, we'll make a simple HTTP GET request using the fetch() function in JavaScript. We'll pass in the base currency and the desired currency to get the current exchange rates.

fetch(`https://api.exchangerate-api.com/v4/latest/{CURRENCY}`).then(
  res => res.json()
).then(data => {
  console.log(data);
});

In the above code block, replace {CURRENCY} with the ISO 4217 code of the base currency you want to use. For example, if you want to use USD, you would pass in https://api.exchangerate-api.com/v4/latest/USD. Once you run the code block, you should see the response data in your console.

{
    "base": "USD",
    "date": "2021-10-13",
    "time_last_updated": 1634083200,
    "rates": {
        "AED": 3.6725,
        "AFN": 86.720101,
        "ALL": 105,
        "AMD": 478.905187,
        "ANG": 1.794801,
        "AOA": 655.891,
        "ARS": 98.214609
        // and so on...
    }
}

The API currently supports over 170 currencies and provides hourly updates.

Conclusion

Using ExchangeRate-API, we can easily get exchange rates for different currencies. With the help of the fetch() function in JavaScript, we can quickly make requests to the API and use the response data in our web or mobile applications. Please refer to the official API documentation for more details on the available endpoints and query parameters.

Related APIs