How to use our Python Currency Converter
Converting currencies using our Python Exchange Rate API is as easy as posting a HTTP request. Your requests would need to follow
this simple pattern:
Here are some examples in Python:
1. Simple Currency Conversion (in Python):
To convert from 12.50 USD (US Dollar) to GBP (British Pound):
from_c = "usd"
to_c = "gbp"
amount = 12.50
url = urllib.urlopen("http://www.exchangerate-api.com/%s/%s/%f?k=API_KEY"%(from_c,to_c,amount))
result = url.read()
print result
Or to get how many Japanese Yen (JPY) there are to the South African Rand (ZAR):
url = urllib.urlopen("http://www.exchangerate-api.com/zar/jpy/?k=API_KEY")
result = url.read()
print result
Error Codes:
The API returns the following error codes:-1 if an invalid amount is used (must be numeric floating point or integer numbers)
-2 if either of the 3 letter currency codes are invalid (Check our supported currencies)
-3 if an invalid key is used (Get an API key)
-4 if your monthly API query limit is reached (Click here for extended usage)
-5 if an unresolvable IP address is provided (This only applies for our "Automatic Country/Flag Information" query.)
We also offer two additional types of query:
2. Automatic Currency Detection/Conversion (in Python):
To convert from 12.50 USD (US Dollar) to your user's currency:
import os
import urllib
ip = os.environ["REMOTE_ADDR"]
url_string = "http://www.exchangerate-api.com/auto/USD/"+ip+"/12.50?k=API_KEY"
url = urllib.urlopen(url_string)
result = url.read()
return_array = result.split('|')
converted_amount = return_array[0]
symbol_unicode = return_array[1]
currency_code = return_array[2]
print converted_amount #FINAL CONVERTED AMOUNT
print currency_code #FINAL 3-DIGIT CURRENCY CODE (EG: USD)
#If you want to display the currency's symbol in HTML:
symbol_html = ""
symbol_characters = symbol_unicode.split(',')
for i in symbol_characters:
symbol_html = symbol_html+""+i+";"
print symbol_html #FINAL SYMBOL HTML (EG: $)
3. Automatic Country/Flag Information (in Python):
To retrieve your user's national flag and other country information:
import os
import urllib
ip = os.environ["REMOTE_ADDR"]
url_string = "http://www.exchangerate-api.com/country_info/"+ip+"?k=API_KEY"
url = urllib.urlopen(url_string)
result = url.read()
return_array = result.split('|')
country_name = return_array[0]
country_code_2 = return_array[1]
country_code_3 = return_array[2]
currency_code = return_array[3]
symbol_unicode = return_array[4]
flag_url = return_array[5]
print country_name #FINAL COUNTRY NAME
print country_code_2 #FINAL 2-DIGIT COUNTRY CODE (EG: US)
print country_code_3 #FINAL 3-DIGIT COUNTRY CODE (EG: USA)
print currency_code #FINAL 3-DIGIT CURRENCY CODE (EG: USD)
#If you want to display the currency's symbol in HTML:
symbol_html = ""
symbol_characters = symbol_unicode.split(',')
for i in symbol_characters:
symbol_html = symbol_html+""+i+";"
print symbol_html #FINAL SYMBOL HTML (EG: $)
#If you want to display an image of the flag in HTML:
flag_image = "<img src='"+flag_url+"' />"
print flag_image #FINAL FLAG IMAGE
How Our API Works
Python
PHP
Ruby
Perl
ASP.NET
Java
Applescript
iPhone C