Setting Up A Foreign money Convertor With ExchangeRatesApi.io

No Comments

Services and products are more and more accessed and purchased on-line. As we dwell in a worldwide village, if now we have a web based store, lots of our guests will fairly seemingly be based mostly on a special nation than our personal, and use a special foreign money than our personal.

If we wish to present an important person expertise for them, to extend the possibilities of them shopping for our product, and coming again to the positioning, there’s something quite simple we are able to do: Show the worth of the purchasing gadgets in their very own foreign money. To attain that, we are able to convert the worth of the merchandise from a base foreign money (say, EUR) to the customer’s foreign money (say, CNY) utilizing the trade fee between the 2 currencies at that particular time.

Foreign money trade charges change continually, on an hourly foundation (and even sooner). Which means we are able to’t have a pre-defined listing of trade charges saved in our software; these should be gathered in real-time. Nonetheless, this isn’t an issue these days. Due to APIs offering foreign money trade information, we are able to add a foreign money convertor to our on-line outlets slightly effortlessly.

On this article, we are going to introduce ExchangeRatesApi.io, a preferred API answer offering information for present and historic trade charges for 168 currencies.

What We Need To Obtain

Merely stated, we wish to do what Amazon is doing: Give the choice to the person in our on-line store to show the costs in a foreign money of their very own selecting.

When visiting a product web page in amazon.co.uk, we’re introduced the worth in British kilos:

However we are able to additionally see the worth in a special foreign money, if we want to. Within the nation/area settings, there’s the choice to alter the foreign money:

After clicking on “Change”, we’re introduced a choose enter, containing a number of pre-defined currencies. From amongst them, I’ve chosen the Euro:

Lastly, again on the product web page, the worth is displayed in euros:

Getting access to trade fee information through an API, we are able to implement this similar performance for our personal on-line outlets.

How Do We Do It

ExchangeRatesApi.io offers a REST API, providing the most recent foreign exchange information for 168 currencies. It’s at all times up-to-date, compiling the info from a broad base of economic sources and banks around the globe.

After signing up on their service (tip: they’ve a free tier), we shall be assigned an API entry key:

We seize our API entry key, and append it to the endpoint:

https://api.exchangeratesapi.io/v1/newest
?access_key=YOUR_API_KEY

To visualise the response, copy/paste the endpoint in your browser:

As will be probably appreciated within the picture above, the info for all 168 currencies has been retrieved. To slim down the outcomes to just some of them, we point out the foreign money codes through param symbols.

As an example, to retrieve information for the USD, British pound, Australian greenback, Japanese Yen and Chinese language Yuan (in contrast towards the Euro, which is the bottom foreign money by default), we execute this endpoint:

https://api.exchangeratesapi.io/v1/newest
?access_key=YOUR_API_KEY
&symbols=USD,GBP,AUD,JPY,CNY

The response is the next:

{
“success”: true,
“timestamp”: 1620904263,
“base”: “EUR”,
“date”: “2021-05-13”,
“charges”: {
“USD”: 1.207197,
“GBP”: 0.860689,
“AUD”: 1.568196,
“JPY”: 132.334216,
“CNY”: 7.793428
}
}

What Information Can We Retrieve

ExchangeRatesApi.io offers a number of REST endpoints, to fetch totally different units of information. Relying on the subscribed plan, endpoints could or might not be accessible (their pricing web page explains what options are coated by every tier).

The endpoints indicated under should be connected to https://api.exchangeratesapi.io/v1/ (eg: newest turns into https://api.exchangeratesapi.io/v1/newest), and added the access_key param together with your API entry key.

Newest charges

The newest endpoint returns trade fee information in real-time for all accessible currencies or for a selected set.

Foreign money Conversion

The convert endpoint allows to transform an quantity, from any foreign money to another one throughout the supported 168 currencies.

Historic Charges

This endpoint has the form YYYY-MM-DD (akin to 2021-03-20), akin to the date for which we wish to retrieve historic trade fee data.

Time-Sequence Information

The timeseries endpoint returns the every day historic information for trade charges between two specified dates, for a most timeframe of three hundred and sixty five days.

Fluctuation Information

The fluctuation endpoint returns the fluctuation information between specified dates, for a most timeframe of three hundred and sixty five days.

Fetching Information From The API

With the intention to implement the foreign money convertor, we are able to use the convert endpoint (for which we want be subscribed to the Primary tier):

https://api.exchangeratesapi.io/v1/convert
?access_key=YOUR_API_KEY
&from=GBP
&to=JPY
&quantity=25

The response we are going to receive appears to be like like this:

{
“success”: true,
“question”: {
“from”: “GBP”,
“to”: “JPY”,
“quantity”: 25
},
“information”: {
“timestamp”: 1620904845,
“fee”: 154.245331
},
“historic”: “”,
“date”: “2021-05-14”,
“outcome”: 3856.079212
}

As a result of the info is uncovered through a REST API, we are able to conveniently retrieve it for any software based mostly on any stack, whether or not it runs on the client-side or server-side, with out having to put in any further library.

Shopper-Aspect

The next JavaScript code connects to the API, and prints the transformed quantity and trade fee within the console:

// Set endpoint and your entry key
const access_key = ‘YOUR_API_KEY’;
const from = ‘GPB’;
const to = ‘JPY’;
const quantity = 25;
const url = https://api.exchangeratesapi.io/v1/convert?access_key=${ access_key }&from=${ from }&to=${ to }&quantity=${ quantity };

// Get the newest trade charges through the “newest” endpoint:
fetch(url)
.then(response => response.json())
.then(information => {
// If our tier doesn’t assist the requested endpoint, we are going to get an error
if (information.error) {
console.log(‘Error:’, information.error);
return;
}

// We bought the info
console.log(‘Success:’, information);
console.log(‘Transformed quantity: ‘ + information.outcome);
console.log(‘(Alternate fee: ‘ + information.information.fee + ‘)’);
})
.catch((error) => {
console.error(‘Error:’, error);
});

Server-Aspect

The next code demonstrates how to hook up with the REST API, and print the transformed outcome, inside a PHP software.

The identical process will be carried out for different languages:

Outline the endpoint URL, connect your API entry key.
Connect with the endpoint, retrieve its response in JSON format.
Decode the JSON information into an object/array.
Get hold of the transformed quantity from underneath the outcome property.

// Set endpoint and your entry key
$access_key = ‘YOUR_API_KEY’;
$from = ‘GBP’;
$to = ‘JPY’;
$quantity = 25;

// Initialize CURL:
$ch = curl_init(“https://api.exchangeratesapi.io/v1/convert?access_key=${access_key}&from=${from}&to=${to}&quantity=${quantity}”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Get the JSON information:
$json = curl_exec($ch);
curl_close($ch);

// Decode JSON response:
$conversionResult = json_decode($json, true);

// Entry the transformed quantity
echo $conversionResult[‘result’];

Conclusion

ExchangeRatesApi.io was born as an open-source venture, with the intention to offer present and historic international trade charges revealed by the European Central Financial institution, and written in Python.

If you happen to’d like to include foreign money conversion in your on-line store, to emulate Amazon and supply a compelling person expertise in your guests, then you possibly can obtain and set up the open-source venture.

And you can too make it a lot simpler: If you happen to’d wish to have your foreign money convertor working very quickly, for any programming language, accessing at all times up-to-date information which incorporates a wide selection of economic sources, and from a super-fast API with uptime of almost 100% (99.9% within the final 12 months), then take a look at ExchangeRatesApi.io.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment