Skip to content

Instantly share code, notes, and snippets.

@harsh546
Last active March 26, 2023 00:07
Show Gist options
  • Save harsh546/3a22639a0bac7aa784254fa33cbcf4da to your computer and use it in GitHub Desktop.
Save harsh546/3a22639a0bac7aa784254fa33cbcf4da to your computer and use it in GitHub Desktop.
Example Using API with Python
import requests
def convert_usd_to_inr(amount):
from_currency = 'usd'
to_currency = 'inr'
url = "https://api.apilayer.com/exchangerates_data/convert?to={0}&from={1}&amount={2}".format(to_currency,from_currency,amount)
payload = {}
headers= {
"apikey": "<Your API Key>" #Visit "https://apilayer.com/" and create your own api key
}
response = requests.request("GET", url, headers=headers, data = payload)
status_code = response.status_code
if status_code==200:
response_json = response.json()
result = round(response_json['result'],2)
else:
print("API request failed")
res="{0} USD = {1} INR".format(amount,result)
print(res)
if __name__ == "__main__":
print("PYTHON CODE TO CONVERT USD TO INR USING API")
print("-------------------------------------------")
amount = float(input("Enter amount in USD ($) : "))
convert_usd_to_inr(amount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment