My Coding > Programming language > Python > Simple Python Projects > Currency exchange rate

Currency exchange rate

Currency exchange rate with Python

A common question for many internet projects is to know the exchange rate between different currencies. I will show you how you can solve this problem with Python very quickly, easily and very reliably.

The common error for beginners, they try to use Yahoo Finance or a similar website for this problem, but this is not the best approach. The best way is to use special API sites for checking the up-to-date exchange rate. Most financial API sites provide this information for free with some limitations.

For example, https://openexchangerates.org/ can serve 1000 requests per month with 1-hour accurate exchange rate information for free. It is only 30*24 = 720 hours in the month, so it is enough to have almost a fresh exchange rate all the time for free. Let's do it!

Registration with openexchangerates

To use their API, it is necessary to register there and confirm your email address. Also, it is advisable to check that your purpose of using exchange rate information is aligned with their Terms and Conditions.

After registration, they will provide you with two API (for free users), but you only need one. And also, you need to understand that every request will use one of your allowed requests. When you use all 1000 requests, you will need to wait until next month to replenish your allowance.

This website provides all rates against USD by default, and you can use so so-called "cross exchange" to find your desired exchange rate.

Getting exchange rate

We only have a limited number of requests, therefore, it is advisable to use Jupyter Notebook to test our code line by line and never repeat lines which are already been executed successfully, especially when connecting with the exchange rate provider.

So, the only library we need to use is a request library. The form of request with your API can be found in the documentation of this website or the code below; you just need to use your real API (not mine). Also, by adding show_alternative=1 parameters, you can access to alternative currency rate.


import requests
# To have only standard currencies
full_url = "https://openexchangerates.org/api/latest.json?app_id=__YOR__API___"
# Use this line to access alternative currencies as well.
# full_url = "https://openexchangerates.org/api/latest.json?app_id=__YOR__API___&show_alternative=1"

After loading the library and the definition of the URL, I advise you to perform the request in a separate cell of the notebook.


response = requests.get(full_url)

After requests, it is advisable to make a check that the request was successful and it returns the standard code 200. If the response is successful, we can convert our data from JSON format into a dictionary using by standard tool from the requests library.


if response.status_code == 200:
    results = response.json()
else:
    raise Exception(f"Response code: {response.status_code}")

You can print the results to see that it is inside, and you will have something like this:


{'disclaimer': 'Usage subject to terms: https://openexchangerates.org/terms',
 'license': 'https://openexchangerates.org/license',
 'timestamp': 1743775206,
 'base': 'USD',
 'rates': {'AED': 3.67303,
  'AFN': 71.599175,
  'ALL': 91.8,
  'AMD': 390.941816,
  'ANG': 1.79,
  'AOA': 912,
  'ARS': 1073.3572,
  'AUD': 1.642624,

The full output is skipped.

After receiving the list of exchange rates, you can make a function which calculates the cross rate, it is very easy.

First of all, you need to convert your sum to USD and then convert it again to your desired currency.


def convert(data, summ, cin='USD', cout='USD'):
    if cin not in data['rates']:
        raise Exception(f"Unknown currency: {cin}")
    if cout not in data['rates']:
        raise Exception(f"Unknown currency: {cout}")
    in_USD = summ / data['rates'][cin]
    in_need = in_USD * data['rates'][cout]
    print(f"{summ:.2f} {cin} = {in_need:.2f} {cout}")   

I do use funny names summ, cin and court - to avoid overlapping with the very common Python functions.

And now it is time to check the results:


convert(results, 125, cin='GBP', cout='PHP')
# 125.00 GBP = 9286.77 PHP

That it!!! Now we have a simple code which gives you any currency exchange rate with 1-hour precision. Now you need to make a mechanism to store this data to avoid using it more often, only once per hour, and then it will be enough for the whole month.

Full code in one go


if response.status_code == 200:
    results = response.json()
else:
    raise Exception(f"Response code: {response.status_code}")
def convert(data, summ, cin='USD', cout='USD'):
    if cin not in data['rates']:
        raise Exception(f"Unknown currency: {cin}")
    if cout not in data['rates']:
        raise Exception(f"Unknown currency: {cout}")
    in_USD = summ / data['rates'][cin]
    in_need = in_USD * data['rates'][cout]
    print(f"{summ:.2f} {cin} = {in_need:.2f} {cout}")  
convert(results, 125, cin='GBP', cout='PHP')

This code will find the total after conversion 125 British Pounds to Philippino Peso


Published: 2025-04-05 16:07:36
Updated: 2025-04-05 16:09:15

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. Privacy Policy