Creating Currency Converter | Python

Soumyabrata Roy
4 min readJun 7, 2020
pic credit: Christine Roy (unsplash)

A currency converter could be really useful to anyone who needs to monitor currency fluctuations and it’s measuring. In the online world, you could find a lot of currency converters out there but if you need a personalized converter for your pacific needs, you need to consider building one. by using python and APIs, you could do it very easily.

In this article, I have described how you could build a simple currency converter using Python and a free API. At the end of this article you will know:

  1. What is an API?
  2. How you could simply use it using python!
  3. How to build a custom currency converter.

What is an API: As all of you know API is the application programming interface. It helps computer programs to communicate and share data. It is like a railway track using which data can be transferred from one station to another station.

Through API the data always transferred in the JSON (JavaScript Object Notation) format. to use that data, you need to convert it to a suitable format and then use it.

How you could simply use it using python: In python, there is a very simple library called request. To get the data, you just need to use the .get method and then convert it to python dictionary data type using the .json method. You could also use Python’s own JSON package .loads method for the same. I will explain to them later but first I would like to show you what I have made using Python and API.

Currency Converter: https://colab.research.google.com/drive/1MMhHoEqPm7JcJSeNep5E2RCYGnzXLzCA?usp=sharing

Historical Currency Data extractor: https://colab.research.google.com/drive/1BChPXnD7QcEogolV7bNATivUbu1t5zWp?usp=sharing

Please make a copy in your google drive and then run to play around with the converter. [open URL > file > Save a copy in Drive]

How to build a custom currency converter: To create the converter I have used the open-source Exchange rates API (https://exchangeratesapi.io/). It is pretty simple to use. You just need to give the base URL, add some parameters and you will get the exact response to play around with. For example, to get the latest or historical currency value, you need to add the desired data, base currency like USD or POUND, desired currency value like INR and that’s it. You will get the response. After this, you can extract the data from this response using a request.Json method or Python .loads method.

Always check if the response status code is 200. If it is not, you will not get anything out of it. Please check the below code here

import requests
def currency_converter_1():old_or_latest = input("Do you want the latest or historical value?\nif latest, please type 'latest' or else type historical value in '%Y-%m-%d' format:\t")base_URL_new = 'https://api.exchangeratesapi.io/'+old_or_latestresponse = requests.get(base_URL_new, params='base=USD')if response.status_code == 200:
data,base_value, Date = response.json().items()
print('Available Currency\n>>', data[1].keys())
desired_currency = input("Please provide the INR or othercurrency")
print(f'{desired_currency}:{data[1] [desired_currency]} | base:{base_value[1]} | date:{Date[1]}')
convert = input('Do you want to convert the '+ desired_currency+ ' value to USD?\t yes or no: ')
if convert == 'yes':
value = float(input("value\n>>>"))
USD_value = value/float(data[1][desired_currency])
print(f'The value is:\t ${USD_value}')
else:
print("\nThank you")
else:
print ("\nPlease give the correct data")

#Here is a ordering issue, please check the code in Collab first

As you can see first I have structured the base_URL_new and then get the response using .get method. After that, just converted it to a python dictionary using .json() and getting the desired data. It is pretty simple, isn’t it?

Whenever you are trying to get the periodic historical data, there is a one-trick you need to apply which is .dumps method. For historical data, you will not get the data in a structured way day by day as the JSON data is not structured. To get the ordered data, you need to convert the JSON to a string (using dumps method), sorting it, and then again convert it to a python dictionary to get the data.

data = json.dumps(response.json(), sort_keys=True)
data1 = json.loads(data)
#please see the Historical Currency Value converter for full code

Here data is the ordered string and data1 is the ordered dictionary. After getting the data, I just play around with the data1 and resulted in the desired output.

I hope you have liked the article and converters. By the way, .get is only applicable for free APIs or open-source APIs. For paid APIs, you need to just replace .get method with .post method. I hope in the future, I will come up with another application with .post method to demonstrate it to you.

Originally published on LinkedIn: https://www.linkedin.com/pulse/creating-currency-converter-python-soumyabrata-roy/?trackingId=Xj2J2M2VS8KX%2B7XIASAneQ%3D%3D

Please 👏, share. If you have any questions, suggestions, please share it with me down in the comment below.

--

--

Soumyabrata Roy

Data Scientist Cognizant | Answering what, why, and how of different business scenarios through machine learning and deep learning.