« Back to All Blogs CurrencyFreaks Real Time Converter API Integration With Python

How Do You Integrate CurrencyFreaks Real Time Currency Converter API in Python?


Introduction

As we know, the world is moving towards global finance and transactions. New technologies are emerging every day to facilitate transactions across the globe. Multiple companies are creating reliable applications that help users to convert currencies. It helps them make better financial decisions. Therefore, real-time currency conversion has become an important component for developers.

As developers, we know the importance of API providing accurate and real-time currency conversion. We need a reliable API for finance, travel, and international trade. CurrecnyFreaks real-time currency conversion API plays an important role in this aspect.

In this blog post, we will show you the integration of this real-time currency converter API in Python. We will explain why choosing Python can be an excellent decision for integration. At the same time, we will show you the integration of multiple CurrecnyFreaks endpoints. Let’s begin.

What Is CurrencyFreaks Real Time Currency Converter API?

CurrencyFreaks is a real-time currency converter API that helps us fetch historical and current forex data. It also helps us get currency fluctuation data. Moreover, we get the response in XML or JSON through a REST API. With support for over world currencies, the API ensures that developers can access various currency pairs.

It is an invaluable resource for applications requiring real-time currency conversion.

CurrencyFreaks offers integration in many programming languages. But let’s check why we are choosing Python.

Why Choosing Python For Integration of CurrencyFreaks Can Be the Best Decision?

Python is one of the most popular and adopted languages among developers these days. It makes the API integration simpler than other programming languages. Let's check the five reasons for choosing Python to integrate this API.

  1. It is important to have clean and maintainable code for API integration. Python is known for its simplicity and reliability. The simple Syntax of Python makes it easy to implement API logic.

  2. The second most important reason to choose Python is its strong community support. If you face any problem, you can easily get help to resolve and integrate your API.

  3. The Python code is cross-platform compatible. You can run Python code on multiple operating systems without modification.

  4. Python allows us to integrate data visualization and analysis. It is important, especially in the field of finance.

  5. Python has excellent documentation and rapid development capabilities.

Python currency rate converter

Where Do We Need to Use CurrencyFreaks API?

Here are some major applications for using CurrencyFreaks API:

  • E-Commerce Platforms
  • Finance and Accounting Software
  • Travel and Booking Platforms
  • Cryptocurrency Exchanges
  • International Payment Gateways
  • Mobile Banking Apps
  • Enterprise Resource Planning (ERP) Systems
  • Gaming Platforms (for In-Game Purchases)
  • Social Media Advertising Platforms

How to Integrate CurrencyFreaks Endpoints in Python?

CurrencyFreaks comes with many endpoints. We will implement them step by step. Let’s begin.

Before we move forward, you must know that CurrencyFreaks provides two types of endpoints.

  1. Endpoints that require an API key.
  2. Endpoints that don’t require an API key.

To be on the safe side, we will create an account at CurrencyFreaks and get the API key. Once, we have an API key, let’s write code for each endpoint and show the output.

Supported Currencies Endpoint

This endpoint doesn’t require an API key. Here is the Python code and output.

import requests

url ='https://api.currencyfreaks.com/v2.0/supported-currencies'

response = requests.get(url)

if response.ok:

supported\_currencies\_map = response.json().get('supportedCurrenciesMap', {})

   \[print(f"Currency Code: {code}, Currency Name: {info\['currencyName'\]}") forcode, info in supported\_currencies\_map.items()\]

else:

 print(f"Error: {response.status_code}\\n{response.text}")

Output

Output of supported currencies endpoint of currency freaks api in python

Historical Limits Endpoint

This endpoint tells about the historical limits of currencies supported by CurrencyFreaks.

import requests

url \= 'https://api.currencyfreaks.com/v2.0/historical-data-limits'

response \= requests.get(url)

data \= response.json()

availability\_period \= data.get('availabilityPeriod', {})

for currency\_code, period in availability\_period.items(): print(f"Currency Code: {currency\_code}, Availability Period: {period}")

Output

Output of historical limits endpoint of currency freaks api in python

Latest Currency Exchange Rates

This endpoint requires an API key and you can access it on the free plan.

https://api.currencyfreaks.com/v2.0/rates/latest?apikey=YOUR_APIKEY

import requests

api\_key \= 'ADD\_YOUR\_APIKEY\_HERE'

url \= f'https://api.currencyfreaks.com/v2.0/rates/latest?apikey={api\_key}'

data \= requests.get(url).json()

print(f"Date: {data\['date'\]}, Base Currency: {data\['base'\]}")

\[print(f"Currency Code: {code}, Exchange Rate: {rate}") for code, rate in data\['rates'\].items()\]

Output

Output of latest rates endpoint of currency freaks api in python

Get Rates of Desired Currencies Only

The above endpoint gives you all the currencies' exchange rates in one response. However, if you want to get exchange rates of desired currencies only, you need to use this endpoint. Let’s check out the integration of this endpoint in Python.

Important Note: This endpoint is accessible on all subscription plans.

import requests

api\_key \= 'ADD\_YOUR\_APIKEY\_HERE'

base\_currency \= input("Enter the base currency code: ").upper()

target\_currencies \= input("Enter target currency codes (comma-separated): ").upper().split(',')

symbols \= ','.join(target\_currencies)

url \= f'https://api.currencyfreaks.com/v2.0/rates/latest?apikey={api\_key}&base={base\_currency}&symbols={symbols}'

data \= requests.get(url).json()

if 'error' not in data:

    date \= data.get('date', '')

    base\_currency \= data.get('base', '')

    rates \= data.get('rates', {})

    print(f"Date: {date}")

    print(f"Base Currency: {base\_currency}")

    for currency\_code in target\_currencies:

        rate \= rates.get(currency\_code)

        if rate is not None:

            print(f"Currency Code: {currency\_code}, Exchange Rate: {rate}")

        else:

            print(f"Invalid target currency code: {currency\_code}")

else:

    print(f"Error: {data\['error'\]}")

Output

Output of latest rates of desired currencies endpoint of currency freaks api in python

Note: The endpoints given below work only with paid plan.

Get the Latest Rates of All Currencies by a Specific Base

This endpoint gives you exchange rates of all currencies for a given base currency. However, this endpoint doesn’t work within the free subscription plan. You must upgrade to a paid plan to use this endpoint. Here is the Python integration code and output:

import requests

url \= 'https://api.currencyfreaks.com/v2.0/rates/latest'

my\_key \= 'ADD\_YOUR\_APIKEY\_HERE'

my\_base \= 'EUR'

params \= {

    'apikey': my\_key,

    'base': my\_base

}

response \= requests.get(url, params\=params)

if response.status\_code \== 200:

    data \= response.json()

    print("Date:", data\['date'\])

    print("Base Currency:", data\['base'\])

    print("Exchange Rates:")

    for currency, rate in data\['rates'\].items():

        print(f"{currency}: {rate}")

else:

    print("Error:", response.status\_code)

    print(response.text)

Output

Output of latest rates by specific base endpoint of currency freaks api in python

Until now, we have been converting one currency to another without specifying the amount. Let’s explore an endpoint that helps us convert a particular amount.

Latest Rates Conversion Endpoint

The endpoint converts 500 USD to PKR. You can set your parameters when using this endpoint.

import http.client

conn \= http.client.HTTPSConnection("api.currencyfreaks.com")

payload \= ''

headers \= {}

conn.request("GET", "/v2.0/convert/latest?from=usd&to=pkr&amount=500&apikey=YOUR\_APIKEY\_HERE", payload, headers)

res \= conn.getresponse()

data \= res.read()

print(data.decode("utf-8"))

Output:

Output of latest conversion endpoint of currency freaks api in python

It is important to note that CurrencyFreaks gives code examples for premium endpoints. You can get the code in the following languages:

  • Python
  • Shell
  • Node JS,
  • Java
  • Ruby
  • C#
  • JavaScript
  • Go
  • C
  • Swift

Historical Exchange Rates Endpoint

Historical exchange rates are game changers for forex traders. CurrencyFreaks also helps us fetch these rates through the following endpoint:

https://api.currencyfreaks.com/v2.0/rates/historical?apikey=YOUR_APIKEY&date={DATE}

Python Integration Code

The following code fetches exchange rates for 2022-03-20 for the base currency set as “USD”. You may change the date accordingly.

import requests

url \= 'https://api.currencyfreaks.com/v2.0/rates/historical'

api\_key \= 'ADD\_YOUR\_APIKEY\_HERE'

date \= '2022-03-20'

response \= requests.get(f'{url}?apikey={api\_key}&date={date}')

if response.status\_code \== 200:

    data \= response.json()

    print("Date:", data\['date'\])

    print("Base Currency:", data\['base'\])

    print("Exchange Rates:", data\['rates'\])

else:

    print("Error:", response.status\_code)

    print(response.text)

Output

Output of historical rates endpoint of currency freaks api in python

Time Series Endpoint

If you want historical data between two specific dates, use this endpoint. It helps us select the desired time series and return data accordingly. However, this endpoint is accessible on professional plans and onwards.

Python Integration Code

import requests

url \= 'https://api.currencyfreaks.com/v2.0/timeseries'

api\_key \= 'b2836e1cbfb54a45b305488bcb04a531'

start\_date \= '2022-06-01'

end\_date \= '2022-06-07'

base\_currency \= 'EUR'

symbols \= 'PKR,USD'

response \= requests.get(f'{url}?apikey={api\_key}&startDate={start\_date}&endDate={end\_date}&base={base\_currency}&symbols={symbols}')

if response.status\_code \== 200:

    data \= response.json()

    print("Start Date:", data\['startDate'\])

    print("End Date:", data\['endDate'\])

    print("Base Currency:", data\['base'\])

    print("Historical Rates:")

    for entry in data\['historicalRatesList'\]:

        print(f"Date: {entry\['date'\]}, Rates: {entry\['rates'\]}")

else:

    print("Error:", response.status\_code)

    print(response.text)

Output

Output of timeseries endpoint of currency freaks api in python

Fluctuation Endpoint

If you want to analyze the fluctuation of currencies daily, use this endpoint. To use this endpoint, we need to add a startDate and an endDate as query parameters.

Python Integration Code

import requests

url \= 'https://api.currencyfreaks.com/v2.0/fluctuation'

api\_key \= 'YOUR\_API\_KEY'

start\_date \= '2022-10-01'

end\_date \= '2022-10-15'

base\_currency \= 'GBP'

symbols \= 'PKR'

response \= requests.get(f'{url}?apikey={api\_key}&startDate={start\_date}&endDate={end\_date}&symbols={symbols}&base={base\_currency}')

if response.status\_code \== 200:

    data \= response.json()

    print("Start Date:", data\['startDate'\])

    print("End Date:", data\['endDate'\])

    print("Base Currency:", data\['base'\])

    print("Rate Fluctuations:")

    for currency, fluctuation in data\['rateFluctuations'\].items():

        print(f"{currency}: Start Rate - {fluctuation\['startRate'\]}, End Rate - {fluctuation\['endRate'\]}, Change - {fluctuation\['change'\]}, Percent Change - {fluctuation\['percentChange'\]}%")

else:

    print("Error:", response.status\_code)

    print(response.text)

Output

Output of fluctuation endpoint of currency freaks api in python

Historical Currency Rates Conversion

Check this endpoint if you want to convert a specific amount according to past currency rates. You get this endpoint on all paid plans. It requires the desired date, currencies and the amount you want to convert.

Python Code

import requests

url \= 'https://api.currencyfreaks.com/v2.0/convert/historical'

api\_key \= 'YOUR\_APIKEY\_HERE'

date \= '2022-01-10'

from\_currency \= 'USD'

to\_currency \= 'PKR'

amount \= 500

params \= {

    'apikey': api\_key,

    'date': date,

    'from': from\_currency,

    'to': to\_currency,

    'amount': amount

}

response \= requests.get(url, params\=params)

if response.status\_code \== 200:

    data \= response.json()

    print("Date:", data\['date'\])

    print("From Currency:", data\['from'\])

    print("To Currency:", data\['to'\])

    print("Conversion Rate:", data\['rate'\])

    print("Given Amount:", data\['givenAmount'\])

    print("Converted Amount:", data\['convertedAmount'\])

else:

    print("Error:", response.status\_code)

    print(response.text)

Output

Output of historical conversion endpoint of currency freaks api in python

IP To Currency Conversion Endpoint

Here comes the final and the most interesting endpoint of CurrencyFreaks. However, this plan is accessible on the Growth plan and onwards. It helps us convert currencies for a given IP address. This endpoint can be highly useful if you are creating a location-based shopping app. It will convert the prices of products on the website according to the visitor’s location.

Python Code

import requests

url \= 'https://api.currencyfreaks.com/v2.0/iptocurrency'

api\_key \= 'YOUR\_API\_KEY\_HERE'

from\_currency \= 'GBP'

ip\_address \= '182.186.18.91'

amount \= 500

params \= {

    'apikey': api\_key,

    'from': from\_currency,

    'ip': ip\_address,

    'amount': amount

}

response \= requests.get(url, params\=params)

if response.status\_code \== 200:

    data \= response.json()

    print("Date:", data\['date'\])

    print("From Currency:", data\['from'\])

    print("To Currency:", data\['to'\])

    print("Conversion Rate:", data\['rate'\])

    print("IP Address:", data\['ipAddress'\])

    print("Given Amount:", data\['givenAmount'\])

    print("Converted Amount:", data\['convertedAmount'\])

else:

    print("Error:", response.status\_code)

    print(response.text)

Output

Output of ip to currency endpoint of currency freaks api in python

What Are the Best Practices to Follow When Integrating a Real-Time Currency Converter API?

Here are some of the best practices that every developer should know:

  1. Keep your API key secure to prevent unauthorized access.
  2. Implement robust error handling to ensure graceful degradation of your application.
  3. Implement rate-limiting strategies to prevent abuse.
  4. Integrate caching techniques to request frequently requested data.
  5. Keep reviewing documentation to monitor API updates.
  6. Test your API integration in a controlled environment before deploying your application.

Conclusion

In the above tutorial, we showed Python integration of CurrencyFreaks endpoints. From free endpoints to paid ones, we showed integration with output. We also discussed why Python for API integration is the best choice. Finally, we shared some best practices to integrate real-time currency converter API.

FAQs

What Is the Free API for Currency Conversion?

CuurencyFreaks is one of the most reliable APIs for currency conversion.

What Is the World’s Most Trusted Currency Data API?

CurrencyFreaks helps us achieve the most trusted currency data.

What Is API in Currency?

An API in currency helps us fetch currency data and implement this functionality in our applications.

Does XE Have an API?

Yes. XE shares API for developers with free and paid subscription plans.

Integrate real-time currency converter API today - Sign Up at CurrencyFreaks now!