Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Sat Apr 29 2023

How to get current currency rates and do currency conversion in JavaScript

In this post, we will promote our API that we offer for free to anyone who wants to use it. The API I'm talking about is our exchange rate API.

We will do some simple conversions, and see what the API has to offer.

Get supported currencies

We will first use an endpoint to get the supported currencies, so we know what we can use in our conversions.

const response = await fetch( `https://stocks.algobook.info/api/v1/exchange/currencies` ); const currencies = await response.json(); console.log(currencies);

Response

[ { "code": "USD", "name": "US dollar" }, { "code": "JPY", "name": "Japanese yen" }, { "code": "BGN", "name": "Bulgarian lev" }, { "code": "CZK", "name": "Czech koruna" }, { "code": "DKK", "name": "Danish krone" }, { "code": "GBP", "name": "Pound sterling" }, { "code": "HUF", "name": "Hungarian forint" }, { "code": "PLN", "name": "Polish zloty" }, { "code": "RON", "name": "Romanian leu" }, { "code": "SEK", "name": "Swedish krona" }, { "code": "CHF", "name": "Swiss franc" }, { "code": "ISK", "name": "Icelandic krona" }, { "code": "NOK", "name": "Norwegian krone" }, { "code": "TRY", "name": "Turkish lira" }, { "code": "AUD", "name": "Australian dollar" }, { "code": "BRL", "name": "Brazilian real" }, { "code": "CAD", "name": "Canadian dollar\t" }, { "code": "CNY", "name": "Chinese yuan renminbi" }, { "code": "HKD", "name": "Hong Kong dollar" }, { "code": "IDR", "name": "Indonesian rupiah" }, { "code": "ILS", "name": "Israeli shekel" }, { "code": "INR", "name": "Indian rupee" }, { "code": "KRW", "name": "South Korean won" }, { "code": "MXN", "name": "Mexican peso" }, { "code": "MYR", "name": "Malaysian ringgit" }, { "code": "NZD", "name": "New Zealand dollar" }, { "code": "PHP", "name": "Philippine peso\t" }, { "code": "SGD", "name": "Singapore dollar" }, { "code": "THB", "name": "Thai baht" }, { "code": "ZAR", "name": "South African rand" }, { "code": "EUR", "name": "Euro" } ]

All right. Now we will continue to explore the API.

Get current rate between two currencies

Now, we will see how the rate is between Mexican Peso and USD.

const response = await fetch( `https://stocks.algobook.info/api/v1/exchange/rate?from=MXN&to=USD` ); const rate = await response.json(); console.log(rate);

Response

{ "rate": 0.0554, "updated": "2023-04-28" }

So, the rate is 0.554 for exchanging MXD to USD. Now let's convert 500 pesos to USD and see what we get using the API.

Convert 500 Mexican Pesos to USD

const response = await fetch( `https://stocks.algobook.info/api/v1/exchange/convert?from=MXN&to=USD&amount=500` ); const amount = await response.json(); console.log(amount);

Response

{ "updated": "2023-04-28", "amount": 27.7 }

And voila. We get 27.7 USD for our 500 MXN.

Outro

So, as stated at the beginning, I wrote this article to promote our free API a little bit, with the hope that anyone out there might benefit from it. The data is originated from the European Central Bank, and is updated every day at 16:00 CET. At Algobook, we want to give all our software for free, with the hope that it will help solve problems for other developers or businesses.

I hope you found this post and the API interesting. If you are looking for a full solution with an UI and everything, we have a React widget that are using this API that anyone can use as well.

Have a great day!

signatureSat Apr 29 2023
See all our articles