Fixer.io Python Example

Fixer.io Python Example

As an Amazon Associate I earn from qualifying purchases.

Python Example

Fixer.io is a simple, lightweight API that tracks and provides current and historical exchange rates of currency pairs published by the European Central Bank.

This article will look at how to get forex data using Fixer.io API and Python programming language.

Currency API – Free Trial

Sign Up

To use the Fixer.io API, one must first sign up on the API’s official website.

There is a free plan that allows users to access a free API key, and there are also three paid plans that include the professional plus plan that costs $80 per month, the professional plan that costs $40 per month, and the basic plan that costs $10 per month.

The free plan only allows users to access 250 API Calls per month and has hourly updates, limited support, and historical data.

Besides additional features like conversion endpoint, time-series endpoint, fluctuation endpoint, access to all base currencies, and SSL encryption, higher API calls with the higher (costlier) plans offering more API Calls per month.

After signing up and making payments in case of the paid plans, one is directed to the dashboard to see their API access key, the available API Endpoints, and the URLs for the respective API Endpoints.

Use of the different API endpoints

The API endpoints enable users to access specific market data depending on the API Endpoint they choose to use.

In this article, we shall look at the 5 API endpoints that are available when using the free plan, and they include:

  • The “latest” endpoint – Queries the most recent exchange rate data
  • The “historical” Endpoint – Queries historical rates for a specific day
  • The “convert” endpoint – For converting any amount from one currency to another using real-time exchange rates
  • The “timeseries” endpoint – Queries the exchange rates within a specific period
  • The “fluctuation” endpoint – Queries the change parameters (percentage and margin) of any currency

Accessing Forex data using Fixer.io API in a Python code

Since Fixer.io API endpoints return data in the standard JSON format, the API can be used with any programming language, including Python.

However, one will require first to install the fixerio.PyPI using “pip install fixerio” or visiting https://github.com/amatellanes/fixerio to get the source code.

Example of Python code using fixer.io API

The example below shows how one can query/request for the latest exchange rates of currency pairs.

>>> from fixerio import Fixerio
>>> fxrio = Fixerio()
>>> fxrio.latest()

If the above code is run, it should give the following results:

'''
{u'base': u'EUR',
u'date': u'2016-07-08',
u'rates': {u'AUD': 1.59141,
u'BGN': 1.95634,
u'BRL': 6.24962,
u'CAD': 1.48623,
[...]
}}
'''

We have shortened the results since it gives the current exchange rates of over 170 pairs.

It is important to note that by default, the Fixer.io API returns the exchange rates of currency pairs where Euro (EUR) is the base currency and orders the results in an alphabetic order starting with the exchange rate for EURAUD.

If someone wants to get data using any of the other fixer.io API endpoints, they should only change the following line.

>>> fxrio.latest()

For example, if one wants to query historical data, they should write:

>>> fxrio.historical ()

Changing the base currency when using Fixer.io in a python code

If someone wants to query data of a currency pair or currency pairs with a different base currency than EUR, they should add (base=’the base currency they want to use.’

Here is an example:

>>> fxrio = Fixerio(base='USD')

In the above example, we have used USD as the’ the base currency they want to use’.

Here is an example of a python code:

>>> from fixerio import Fixerio
>>> fxrio = Fixerio(base='USD')
>>> fxrio.latest()

The results are:

'''
{u'base': u'USD',
u'date': u'2016-07-08',
u'rates': {u'AUD': 1.34783,
u'BGN': 1. 1.65654,
u'BRL': 5.23202,
u'CAD': 1.25593,
[...]
}}
'''

Narrowing down the number of currency pairs that someone wants to query using Fixer.io in a Python code

To request the data of a specific currency pair or currency pairs, one should add (symbol= [‘currency1’, ‘currency2…]

It is important to note that the currency1, currency2, etc., Are the quote currencies.

Here is an example:

>>> fxrio = Fixerio(symbols=['USD', 'GBP'])

We have used USD as currency1 and GBP as currency2.

Example in a code:

>>> from fixerio import Fixerio
>>> fxrio = Fixerio(symbols=['USD', 'GBP'])
>>> fxrio.latest()

Here are the results:

'''
{u'base': u'EUR',
u'date': u'2016-07-08',
u'rates': {u'GBP': 0.85962, u'USD': 1.18454}}
'''

Amazon and the Amazon logo are trademarks of Amazon.com, Inc, or its affiliates.

Share