If you are a developer looking for a simple lightweight API that tracks and provides current and historical exchange rates of currency pairs, Fixer.io is a good option. It tracks and provides exchange rates that are published by the European Central Bank.
In this article, we shall look at how to use the Fixer.io API when using the Python programming language.
First Sign Up
For you to be able to use the Fixer API, you should first sign up on the official website. You could choose to sign up for the free plan, which gives you a free API key or choose to sign up for one of the paid plans that include the basic plan that costs $10 per month, the professional plan that costs $40 per month and professional plus plan that costs $80 per month.
For this article, we shall use the free plan, which offers 250 API Calls per month, hourly updates, limited support, and historical data.
If you require more API Calls per month, you could choose either of the paid plans. The higher the plan the more API Calls it offers per month. The paid plans also offer additional features like SSL encryption, conversion endpoint, all base currencies, time-series endpoint, and fluctuation endpoint. The higher the plan the more the features.
The signup process is fast and once you fill the registration form you should have access to your Fixer account dashboard from where you should see:
- Your API Access Key
- API Endpoints
- Endpoint URLs
Accessing different kinds of data using the API endpoints
When using the free plan, you can access 5 different API endpoints namely:
- “latest” endpoint – Used for requesting the most recent exchange rate data
- “historical” endpoint – Used for requesting historical rates for a specific day
- “convert” endpoint – Used for converting any amount from one currency to another using real-time exchange rates
- “timeseries” endpoint – Used for requesting the exchange rates for a specific period of time
- “fluctuation” endpoint – Used for requesting any currency’s change parameters (margin and percentage), optionally between two specified dates
To use any of the above API endpoints in your python code start with Fixer’s base URL, which is http://data.fixer.io/api/ followed by the name of the API endpoint with a question mark and your API access key.
Here is an example of using the “latest” API to request the current exchange rate for a specific currency pair
http://data.fixer.io/api/latest?access_key=d0xxxxxxxxxxxx
NB: the access key has been hidden by using xxxxxxxx
The exchange rates that you get are by default relative to EUR base currency.
Below is an example of the API response for using the latest API endpoint:
{
"success":true,
"timestamp":1622898244,
"base":"EUR",
"date":"2021-06-05",
"rates":{
"AED":4.468901,
"AFN":95.447076,
"ALL":123.11381,
"AMD":632.709756,
"ANG":2.18348,
"AOA":781.092956,
"ARS":115.3249,
"AUD":1.572312,
[...]
}
}
The list of the rates has been shortened in the above example. The result contains the rates of 170 currencies in relation to the base, which in this case is the EUR.
Using Fixer with Python programming language
Fixer APIs return data that is in the standard JSON format that makes it easy to be parsed using any programming language including Python.
If you are using Python, however, you will be required to first install fixerio.
You can then go ahead and specify a function name, pass it into the Fixer API callback so that the API can return the requested data depending on the API endpoint wrapped in the function.
Below is an example of when using the “latest” API endpoint:
>>> from fixerio import Fixerio
>>> fxrio = Fixerio()
>>> fxrio.latest()
'''
{u'base': u'EUR',
u'date': u'2016-05-27',
u'rates': {u'AUD': 1.5483,
u'BGN': 1.9558,
u'BRL': 4.031,
u'CAD': 1.456,
[...]
}}
'''
If you wanted to get data using any of the other API endpoints, you should change the
>>> fxrio.latest()
For example, you can have:
>>> fxrio.historical ()
The exchange rates returned by the “latest” API endpoint are usually quoted using EUR as the base. To change the base currency, use
>>> fxrio = Fixerio(base='USD')
You can place any currency pair instead of USD.
Here is an example:
>>> from fixerio import Fixerio
>>> fxrio = Fixerio(base='USD')
>>> fxrio.latest()
'''
{u'base': u'USD',
u'date': u'2016-05-27',
u'rates': {u'AUD': 1.3864,
u'BGN': 1.7513,
u'BRL': 3.6094,
u'CAD': 1.3037,
u'CHF': 0.99105,
[...]
}}
'''
To narrow down on the exchange rates of specific quote currencies in relation to the specified bas currency, simple use:
>>> fxrio = Fixerio(symbols=['USD', 'GBP'])
You can place any currency you want besides ‘USD and ‘GBP’.
Here is an example:
>>> from fixerio import Fixerio
>>> fxrio = Fixerio(symbols=['USD', 'GBP'])
>>> fxrio.latest()
'''
{u'base': u'EUR',
u'date': u'2016-05-27',
u'rates': {u'GBP': 0.76245, u'USD': 1.1168}}
'''