How to use Python to get live forex data using CurrencyAPI.io

Currency API allows developers to obtain real-time exchange rate data using a single currency API endpoint. The endpoint can be used to return exchange rate data for 300+ cryptocurrencies, commodities (Gold and Silver) as well as 1800+ forex currency pairs.

To use the CurrencyAPI.io you will have to first signup for an account and choose either of the two plans it offers; the Starter plan (with 7 days free trial) that costs $19 per month or the premium plan that costs $199 per month.

The main difference between the two plans is that the starter plan only allows for 10,000 API calls per month and does not return historical data while the premium plan allows for 100,000 API calls per month and also returns historical data.

The currency API supports a majority of programming languages including JavaScript, PHP, Python, PowerShell, cURL, C#, Go (Native), Ruby, Swift, NodeJs, and Java. In this article, we will concentrate on how to get live forex data using CurrencyAPI.io when programming using Python.

Using Python to get forex data using Currency API.io

The first thing that you need to know is the API’s URL, which is:

https://api.currencyapi.io/

Next is the access key, which is also referred to as the API authentication or token. It is used to authenticate that you have subscribed to one of the CurrencyAPI.io plans. You have to append it for you to be able to access the live data using the CurrencyAPI.io. Below is an example of how to append the access key:

https://api.currencyapi.io/markets
?token=ACCESS_TOKEN

In the above example, you should replace the ‘ACCESS_TOKEN’ with the access key you were provided with when you sign up with CurrencyAPI.io.

/markets is the endpoint.

If you want to get a list of all the available pairs in a specific market (cryptocurrency, commodity and forex), you should use the following:

https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&market=MARKET_NAME

In the above example, you should replace ‘MARKET_NAME’ with a specific market name like crypto, forex, or commodity.

However, to obtain the data of a single currency pair, you can query using the base currency or currency pair. Below is an example of the code line to use:

GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&search=SYMBOL

You should replace ‘ACCESS_TOKEN’ with your access key and ‘SYMBOL’ with the currency pair or base currency you want to query. If you use EURUSD as the ‘SYMBOL’ for example, you should get the following results:

[
{
"t":"2021-06-09T05:43:00.563Z",
"tms":"1318558750086",
"s":"EURUSD",
"b":1.21853,
"bd":0,
"a":1.21884,
"ad":1,
"p":5
}
]

“t” is the date and time of the query result, “tms” is the update timestamp, “s” is the currency pair, “b” is the bid price, “bd” is the bid price direction, “a” is the ask price, “ad” is the ask price direction and “p” is the currency precision (decimal points).

If you have subscribed for the premium plan, you can also query historical data using the following code line:

GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&symbol=SYMBOL
&historical=true
&offset=OFFSET // Optional (candles offset)
&scale=SCALE // Optional (e.g., 1, 60, 1D, 1W, 1M), default to 1D

When using Python, you can choose to use ‘import http.client’ or ‘import requests’ to write a code that gets live exchange rate data using the currencyAPI.io.

Below is an example of a code using ‘import requests’:

import requests
tuper = "https://api.currencyapi.io/markets?token=ACCESS_TOKEN&symbol=EURUSD"
payload={}
headers = {}
res = requests.request("GET", tuper, headers=headers, data=payload)
print(res.text)

You should replace the ‘ACCESS_TOKEN’ with the access key.

Below is a code example of when using ‘import http.client’:

import http.client
duer = http.client.HTTPSConnection("api.currencyapi.io")
duer.request(
"GET",
"/markets?token=ACCESS_TOKEN&symbol=EURUSD",
)
response = duer.getresponse()
data = response.read()
print(data.decode("utf-8"))