Our aim is to create a currency converter to help us convert a sum of money from one currency to another.
Currency exchange rates are constantly changing which is why we have decided against the idea of storing all the exchanges rates in our code are these would not remain up-to-date. Instead we will retrieve up-to-date currency exchange rates by making calls to an API that provides the current rates.
To do so we will use the “Currency Converter API” to retrieve up-to-date exchange rates. You can read more about this API on: https://currencyconverterapi.com/. For the purpose of this blog post we will use the free version of this API.
This API works as follows:
API Request: e.g. Converting from US Dollars (USD) to British Pounds (GBP)
Check our code to see how we make a call to the API and how we retrieve and extract the requested JSON data.
request a free API key from currencyconvertapi.com and insert this key on line 5 of the code below. Make sure you also follow instructions on how to verify your email to complete the API key request.
Update the code above to accept a full range of currencies. You will need to use the API to retrieve a full list of currencies by making a request using the following URL: https://free.currencyconverterapi.com/api/v6/currencies
You will have to parse this JSON data to retrieve all the currency codes and append these to the list called validCurrencies currently declared on line 5 of the above trinket.
You may also want to output all the currency codes and the name of each currency to inform the user of the available codes and their meaning.