Table of Contents
Live Exchange Rates in Python – Currency API SDK


Need live exchange rates in Python? You're in the right place.
Our SDK makes it simple to use a python currency api to fetch real-time data and build a currency converter in python using an API.
You can integrate CurrencyApi.net in minutes with clean, readable code for reliable exchange rates python.
Getting Started with the Python Currency API SDK
To get started, ensure you have Python 3.5 or higher installed. Then install the package with pip to access the currency api python SDK from your environment:
pip install currencyapinet
Importing the Python Currency API SDK
After installation, import the SDK into your Python project to access live python exchange rates with the exchange rates api python SDK:
from currencyapinet.currency import Currency
Creating Your Python Currency API Instance
You are now ready to start using the Currency API in Python.
Create an instance of the Currency class by passing in your API key to authenticate your requests.
currency = Currency('YOUR_API_KEY')
Have you got an API key?
It's super easy to get started with our Python Currency API SDK. Signup for a free API key and start building your next project.
Retrieving Live Exchange Rates in Python
Now the fun part begins. Let's retrieve live exchange rates in Python.
This example calls the /rates
endpoint to fetch real-time currency rates.
result = currency.rates().get()
This will return a JSON response containing all the live currency rates.
The rates()
method is a convenience method that returns the /rates
endpoint.
Let's put it all together and look at the API response:
from currencyapinet.currency import Currency
currency = Currency('YOUR_API_KEY')
result = currency.rates().get()
print(result)
Congratulations! You have successfully fetched live currency rates. You can now power dashboards, conversions, and analytics with fresh exchange‑rate data using a lightweight currency api python workflow.
Available Methods for Rates Endpoint (Python Currency API SDK)
Method | Description |
---|---|
base() | Sets the base currency for conversions. This will output all currency conversions for that currency. Default is USD. |
output() | Specifies the response format: JSON or XML. Default is JSON. |
Currency Conversion with the Python Currency API
With our python currency converter api it's easy to convert amounts from one currency to another.
This returns a smaller response size and is a great way to get the most out of our API.
The convert()
method uses the /convert
endpoint of the Currency API.
Let's dive in and convert 100 USD to EUR using the SDK for a currency converter in python using our API:
from currencyapinet.currency import Currency
currency = Currency('YOUR_API_KEY')
result = currency.convert().from_currency('USD').to_currency('EUR').amount(100).get()
print(result)
As you can see, the response is much smaller and contains only the information you requested.
Available Methods for Convert Endpoint (Python Currency API)
Method | Description |
---|---|
from_currency() | The currency you want to convert. This will be a three letter ISO 4217 currency code from one of the currencies we have rates for. Required. |
to_currency() | The currency you want to convert the amount 'to'. Again this will be a three letter currency code from the ones we offer. Required. |
amount() | The value of the currency you want to convert from. This should be a number and can contain a decimal place. Required. |
output() | Specifies the response format: JSON or XML. Default is JSON. |
Accessing Historical Exchange Rates in Python
Sometimes you need to access historical exchange rates in Python.
The history()
method uses the /history
endpoint of our Currency API.
Let's retrieve historical currency rates for the USD base currency on January 1st, 2019 using our Python SDK.
from currencyapinet.currency import Currency
currency = Currency('YOUR_API_KEY')
result = currency.history().date('2019-01-01').base('USD').output('JSON').get()
print(result)
The API response returns a JSON object containing historical currency exchange rates for the day you requested.
Available Methods for History Endpoint
Method | Description |
---|---|
date() | The historical date you wish to receive the currency conversions for. This should be formatted as YYYY-MM-DD. Required. |
base() | Sets the base currency for conversions. This will output all currency conversions for that currency. Default is USD. |
output() | Specifies the response format: JSON or XML. Default is JSON. |
Use the Python Currency API To Access Rates Within a Timeframe
Accessing exchange rates within a timeframe lets you track currency rates over time.
Think graphs, charts, or any application that requires real‑time and historical currency exchange rates data.
The timeframe()
method uses the /timeframe
endpoint of the Currency API and fits neatly into most exchange rates api python workflows.
Let's retrieve rates for the USD base currency between January 1st, 2019 and January 1st, 2020 using the SDK:
from currencyapinet.currency import Currency
currency = Currency('YOUR_API_KEY')
result = currency.timeframe().start_date('2019-01-01').end_date('2019-01-01').get()
print(result)
The API returns multiple days of currency rates in a single response, making it easy to build applications that track currency rates over time.
Available Methods for Timeframe Endpoint
Method | Description |
---|---|
start_date() | The historical date you wish to receive the currency conversions from. This should be formatted as YYYY-MM-DD. Required. |
end_date() | The historical date you wish to receive the currency conversions until. This should be formatted as YYYY-MM-DD. Required. |
base() | Sets the base currency for conversions. This will output all currency conversions for that currency. Default is USD. |
output() | Specifies the response format: JSON or XML. Default is JSON. |
Best Practices for the Python Currency API Integration
When using the SDK, follow these best practices to ensure efficient and reliable integration for a python currency converter using our API:
- Cache responses when possible to reduce API calls and improve performance.
- Handle exceptions to prevent your application from crashing due to unexpected errors.
- Use environment variables to store your API key securely.
- Refer to the official documentation for advanced usage and updates.
Handling Responses from the Python Currency API
JSON Response
The SDK returns responses in your specified format. By default, the Currency API returns JSON, so you don't need to specify output explicitly unless you want XML.
XML Response
You can specify the output using the output()
parameter. Set it to either JSON or XML. Here's how to request XML using the Python Currency API SDK:
result = currency.rates().output('XML').get()
Then in Python, you can handle the XML response using the xml.etree.ElementTree module:
from currencyapinet.currency import Currency
from xml.etree import ElementTree
currency = Currency('YOUR_API_KEY')
result = currency.rates().output('XML').get()
root = ElementTree.fromstring(result)
rates_element = root.find("rates")
for currency in rates_element:
print(f"Currency: {currency.tag}, Rate: {currency.text}")
Looping Over the JSON Response in Python
To loop over the JSON response in your Python code
result = currency.rates().get()
base_currency = result['base']
rates = result['rates']
print(f"Base Currency: {base_currency}")
for currency, rate in rates.items():
print(f"{currency}: {rate}")
Error Handling – Python Currency API SDK
In Python, we use the exception type to handle errors. This allows us to gracefully handle errors and provide meaningful error messages to the user.
In doing this, we can handle exceptions and errors gracefully to ensure our application remains robust:
try:
result = currency.rates().get()
except Exception as e:
print(f"An error occurred: {e}")
Integrating the Python Currency API into Your Python / Django / Flask App
In this section, we will look at how to integrate the CurrencyApi.net python currency api SDK into your Python / Django / Flask application.
We will be using the Currency class and the convert method to convert currencies.
from currencyapinet.currency import Currency
def convert_currency(amount, from_currency, to_currency):
result = currency.convert().from_currency(from_currency).to_currency(to_currency).amount(amount).get()
return result['conversion']['result']
currency = Currency('YOUR_API_KEY')
try:
converted = convert_currency(100, 'BTC', 'USD')
print(f"100 BTC is equal to {converted} USD")
except Exception as e:
print(f"An error occurred: {e}")
Conclusion
Our SDK provides a straightforward and efficient way to integrate live exchange rates into your Python applications.
Build a python currency converter api workflow, power analytics, and ship features faster with reliable data.
Start free with our developer plan for a free currency converter api python experience, and scale as you grow.
Other Useful Links:
Explore Other SDKs
Not using Python? Try out one of our other SDKs below.
GoLang SDK
Quickly implement currency data into your GoLang applications with our user-friendly SDK. Access live exchange rates, currency conversions, and more.
Explore the GoLang SDK →NodeJs SDK
Integrate real-time currency services into your NodeJs projects. Our SDK provides you with powerful tools for handling currency data with minimal effort.
Discover the NodeJs SDK →PHP SDK
Seamlessly connect to our Currency API from your PHP applications. This SDK ensures smooth and efficient access to the latest currency rates and services.
Check out the PHP SDK →Get Started with CurrencyApi.net Today!
Sign up now to access live currency rates and enhance your Python applications.