Python for Forex Historical Data

If you are a trader or developer you can always use Python to get historical forex data using

The Python software development kit (SDK) is simple to use and thus provides one of the best platforms for developers who want to use forex historical data. So, let’s get into how you should do this.

Sign up for the Premium account with

The first step is to sign up for an account with to get your API key. offers two types of accounts; the starter account, which comes with a 7 day trial period, and the premium account. If you want to access historical data using the CurrencyAPI, you should ensure you register for the premium plan which costs $199 per month, since it is the one that allows you to access historical data.

The signup process only takes a matter of seconds. Once you complete signing up, ensure to keep your API key safe because it is the one that you will use when writing your programs to show that you have registered for an account with

Install SDK

Once you have your account, ensure you have installed Python 3 or a later version installed to your developer environment. Contrary to other APIs that require you to install a python package SDK to enable you to integrate the API with Python, does not require you to install anything else other than the Python SDK.

Testing the historical data API in

The API URL for is

The has only one API endpoint, the “markets” that allows you to query live data or historical data depending on the account you have subscribed for. In this case, we shall be using the endpoint to query historical data through the premium account.

To get the historical data of any symbol (single currency or a currency pair) simply type the following:

    &offset=OFFSET // Optional (candles offset)
    &scale=SCALE // Optional (e.g., 1, 60, 1D, 1W, 1M), default to 1D

Note the question mark (?) used at the beginning of the term token; it shows where the query begins. Everything that comes after that is part of the query and you use the ‘&’ sign to add all the other things you want to query. In our case, we are querying a symbol (currency or currency pair), historical data, candle offset, and scale.

You should replace ‘ACCESS_TOKEN’ with your CurrencyAPI API key, and ‘SYMBOL’ with the currency or currency pair that you want to get data for.

For example, you could use EURUSD for the symbol and the code would look like this:

    &offset=OFFSET // Optional (candles offset)
    &scale=SCALE // Optional (e.g., 1, 60, 1D, 1W, 1M), default to 1D

Notice the &historical is set to true to enable the code to query the historical data of the currency pair. You could choose not to use the offset and scale functions although we have included them in the above examples.

The scale function is however very important since it specifies the period within which you want to get the historical data. When the scale is not set, the API by default queries data from the past day. But if you want data for the past 1 Week, for instance, you should use the &scale=SCALE and replace ‘SCALE’ with 1W.

If you run the above code in, the response should be something similar to this:


The first result is the timestamp showing when the response was given. The second is the opening price of the market. The third is the highest market price the market attained during the duration of time. The fourth is the lowest market price that the market attained during the specified period. The fifth is the closing price.

Using with Python to get historical data

Now that we have tested the historical data query with the, let us look at how you can use the API to write a python code that can query forex historical data.

Python is the simplest programing language to use when using Python’s code is generally short, precise, and not hard to interpret.

There are two routes that you can use to write python code with These are using the import request and using import http. client.

Using the import request

Your python code should look something like this:

import requests
url = "
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)

If you look closely, you will notice that we have used what we were using when using ‘GET’ in the platform and assigned it to the integer ‘url’.

When trying out this code, you should replace ‘ACCESS_TOKEN’ with your API key, ‘SYMBOL’ with the currency pair or currency you are querying about, and ‘SCALE’ with the period you want to consider in your query.It is important to note that when using the ‘import request’, we use the API URL (, which precedes the API endpoint (/markets).

Using the import http.client

Your python code should look something similar to this:

import http.client
conn = http.client.HTTPSConnection("")
res = conn.getresponse()
data =

When trying out this code, you should replace ‘ACCESS_TOKEN’ with your API key, ‘SYMBOL’ with the currency pair or currency you are querying about, and ‘SCALE’ with the period you want to consider in your query.

When using the import http.client, we do not specify the API URL. After ‘GET’, we go directly to the API endpoint (/market) and start listing the query stating with the API key presided by the ‘&’ sign.

It is important to note the difference in where ‘GET’ is used in the two codes. Though in both, it is used after request.

If you run either of the two python codes, you should get a response similar to the one below. The response is similar to that obtained when querying using the platform.


And that is it! Today we learned how to create and make historical currency pairs API calls using Python and You are now good to go to start using Python with to get forex historical data.