Rajan Kumar

Rajan Kumar published an article

3 months, 4 weeks ago

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.

 

Approach

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.

 

Implmentation

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

 


arrow_upward Upvote

comment Comment

arrow_downward downvote




Go back to feed

HackersFriend Updates


update Aug. 15, 2019, 1:47 p.m.

Full view page of article has been changed to match style of homepage feed. open_in_new


update Aug. 14, 2019, 12:52 a.m.

Total number posts on homepage is incresed to 10 by default and text-decoration of link to post and author is changed. open_in_new


update Aug. 7, 2019, 1:51 a.m.

We changed our homepage. Now, you can see newly published articles, directly on home page. open_in_new


View more


Events


event Nov. 28, 2018, 5:30 p.m.

Python from zero to hero

place Delhi

View details



event Aug. 13, 2018, 5:30 p.m.

Python from zero to hero

place Bangalore ( HackersFriend office BTM Layout)

View details