Live currency rates with CurrencyApi.net — with PHP & NodeJs/Javascript Examples

Oli Girling
3 min readFeb 15, 2021

Whether you run an E-Commerce store, develop for SaaS company or manage a startup, pricing is often an integral part of a website. Accepting multiple currencies allows you to expand your service worldwide, but to do that, you need exchange rates data.

Introducing CurrencyApi.net’s live currency rates.

CurrencyApi.net provides live currency rates via a REST API. A live currency feed for over 152 currencies, including physical (USD, GBP, EUR + more) and cryptos (Bitcoin, Litecoin, Ethereum + more). A JSON and XML currency api updated every 60 seconds.

There are 4 plan types, including a free plan offering 1250 monthly request, each offering different features including:

  • Rates updated every 60 seconds
  • Historical rates back to year 2000
  • Convert one currency to the other using their convert endpoint
  • Commercial Use
  • Unlimited requests with Professional Plan

So let's dive in and see how to use it.

Once you have signed up, you will be given an API key. Then choose which endpoint you want to use:

/* List All Supported Currencies */
https://currencyapi.net/api/v1/currencies

/* Get Currency Rates */
https://currencyapi.net/api/v1/rates

/* Get Currency Conversion */
https://currencyapi.net/api/v1/convert

/* Get Historic Day Rates of a Currency */
https://currencyapi.net/api/v1/history

/* Get Timeframe Rates of a Currency */
https://currencyapi.net/api/v1/timeframe

And simply just append your API key as a URL parameter to authenticate the call, like so:

https://currencyapi.net/api/v1/rates?key=YOUR_API_KEY

By default, each endpoint returns a JSON response, for example:

JSON response from CurrencyApi.net

You are also able to return XML, simply add an output parameter and set it to XML, like so:

https://currencyapi.net/api/v1/rates?key=YOUR_API_KEY&output=XML

Which returns:

<root>
<valid>true</valid>
<updated>1613408405</updated>
<base>USD</base>
<rates>
<AED>3.67338</AED>
<AFN>77.29739</AFN>
<ALL>101.9306</ALL>
<AMD>524.625</AMD>
<ANG>1.79599</ANG>
.... plus more currencies

For examples of how to use each of their endpoints, check out their documentation.

They also supply a Postman Collection, which allows you to quickly import and use their API in seconds. Check the Postman section in the documentation for a quick link

Let’s move onto the fun part and start using the service for a real application.

How to use CurrencyApi’s rates endpoint in PHP

The simplest way is to use their own PHP Composer package.

composer require houseofapis/currencyapi

And then:

use HouseOfApis\CurrencyApi\CurrencyApi;$currencyApi = new CurrencyApi('YOUR_API_KEY');$result = $currencyApi
->setBase('USD')
->setOutput('JSON')
->rates();

$result will now contain a list of the currency conversions.

Check out the documentation of the Composer package here.

Alternatively, if you don't have composer:

$apiKey = 'YOUR_API_KEY';
$baseCurrency = 'USD';
$baseUrl = 'https://currencyapi.net/api/v1/rates';
$params = '?key='.$apiKey.'&base='.$baseCurrency;

$ch = curl_init($baseUrl . $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonResponse = curl_exec($ch);
curl_close($ch);

$objResponse = json_decode($jsonResponse);

Now your $objResponse will contain everything you need. Let’s print out the conversion rate from 1 USD to 1 EUR

echo '1 '.$baseCurrency.' equals '.$objResponse->rates->EUR.' EUR'

How to use CurrencyApi’s rates endpoint in NodeJs

Again the simplest way is using CurrencyApi’s Npm package

npm install currencyapi-node

Then add it to the javascript page you want to use it:

const CurrencyApi = require('./currencyapi-node')

Now let's get the latest exchange rates

const currency = new CurrencyApi('YOUR_API_KEY');
const result = await currency.rates().get()
// or
currency.rates().get()
.then(console.log)

Their package chains methods, so an example with multiple methods would look something like:

const result = await currency
.rates()
.base('USD')
.output('JSON')
.get()

Check out the documentation of the NPM package here.

Alternatively, if you don't want to use the NPM package

const url = 'https://currencyapi.net/api/v1/rates'fetch(url + '?' + new URLSearchParams({
key: 'YOUR_API_KEY'
}))
.then(response => response.json())
.then(value => console.log(value))

So there you have it. Nice and simple way to get up to date live currency exchange rates.

--

--