Tutorials  Articles  Notifications  Login  Signup


Rajan Kumar

Founder at HackersFriend Updated July 25, 2019, 1:16 p.m. ⋅ 3929 views

Getting real time currency exchange rate using Python

In this article, I'll talk about how to write a python program to get real time currency exchange rates using alphavantage api.

Currency rates changes as per market and whenever we are exchanging, we need to use the current value. Alphavantage provides api to get real time rates, in this program, we'll use it. You need to get an api key for that, which you can do from here.



We'll call CURRENCY_EXCHANGE_RATE api and pass the currecies for which we want get exchnage rate, then parse the responsed JSON data and use that.



Let's code this approach. we are going to use following python packages:

  • requests
  • json

So, if you like to follow along, you need to install these packages. 

If you have these packages already installed you are good to go. Otherwise, you can simply install these pckages using follwing command:

pip install package-name

You need to replace package-name with the name of package you want to install.

Once you are done installing all the required packages, you can continue further.

Here is the code.

import requests, json 

# Function to call API and get real time currency exchange 

def AlphpaFetcher(from_currency, to_currency, api_key) : 
	alpha_url = r"https://www.alphavantage.co/query?function = CURRENCY_EXCHANGE_RATE"

	final_url = aplpha_url + "&from_currency =" + from_currency +
			"&to_currency =" + to_currency + "&apikey =" + api_key 
    # Fire a get req to API
	req_ob = requests.get(final_url) 
    # get the results in dictionary from JSON response
	result = req_ob.json() 
	print("Realtime Currency Exchange Rate for", 
		result["Realtime Currency Exchange Rate"] 
				["2. From_Currency Name"], "TO", 
		result["Realtime Currency Exchange Rate"] 
				["4. To_Currency Name"], "is", 
		result["Realtime Currency Exchange Rate"] 
				['5. Exchange Rate'], to_currency) 

# Main Function 

# currency code 
from_currency = "USD"
to_currency = "INR"

# pass your api key here 
api_key = "Your_Api_Key"

# Call the AlphpaFetcher
AlphpaFetcher(from_currency, to_currency, api_key) 


Realtime Currency Exchange Rate for United States Dollar TO Indian Rupee is 68.96670000 INR


HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.

Create a free account