Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

APIFinancePython

Python Currency Converter: Creating a Currency Conversion API-Based Web App

Python Currency converter: Financial data displayed on a laptop

Currency conversion is a crucial aspect of international trade and foreign transactions.  Thus, foreign investors and businesses frequently use currency converter apps to get accurate real-time exchange rates. Travellers also need currency conversion data to get the best exchange rate data. In this tutorial, we’ll show you how to create a Python currency converter app using a currency conversion API and Python Flask. 

Choosing a Currency Conversion API for Our Python Currency Converter

For this tutorial, we’ll use CurrencyLayer API, which provides highly accurate and reliable exchange rates and currency conversion data. CurrencyLayer’s data powers numerous currency converters, back-office systems, and mobile apps worldwide. 

Here are the other prominent features of the API that make it the best choice for our Python currency converter:

CurrencyLayer - Real-time currency converter API

  • With CurrencyLayer exchange rate API, you can access up to 168 world currencies and precious metals. 
  • The Forex API Python updates data frequently to provide accurate and real-time rates. Data updates range from every 60 minutes down to stunning 60 seconds, depending on your subscription plan.
  • CurrencyLayer is super easy to integrate into any web app. It also comes with detailed documentation containing sample code, further easing the integration process. 
  • The API delivers JSON data for maximum usability, ease of integration, and compatibility with any of your applications.
  • With CurrenclyLayer API, you gain access to historical rates, have the API convert single amounts for you, get a currency’s change parameters, switch Source Currency, and more. 
  • The best part about CurrencyLayer is that it comes with a free plan offering 100 monthly API calls, daily updates, and historical rates. 

How can I Empower Apps With Exchange Rate Data Using CurrencyLayer?

You can easily integrate CurrencyLayer into any application you have. For instance, we’ll use the CurrecyLayer Python currency converter API in our Flask Python web app. 

You just have to make an API request using a simple URL structure, and the API will do the rest.

Here are examples of how you can get the data:

Live Data

Example API request:

Example API response:

Creating a Python Currency Converter - Example API response returned by the CurrencyLayer API for live exchange rates data

Historical Data

The CurrencyLayer API delivers highly accurate historical exchange rate data for every past day all the way back to 1999. You can access historical rates by simply attaching the date parameter with a valid date (Format: YYYY-MM-DD) to the historical endpoint.

Example API request:

Example API response:

Creating a Python Currency Converter - Example API response returned by the CurrencyLayer API for historical exchange rates data

Convert One Currency to Another

With CurrencyLayer’s ‘convert’ endpoint, you can perform a Single currency conversion. 

To use this endpoint, you simply need to specify the currency that you want to convert, the currency to which you want to convert, and the amount you would like to convert.

Example API request:

Example API response:

Creating a Python Currency Converter - Example API response returned by the CurrencyLayer API for conversion endpoint

Do You Need More On API Endpoints?

The CurrencyLayer API also offers additional customizable endpoints. Below, you will find a summary of all available endpoints. 

CurrencyLayer API endpoints

Moreover, you can specify

For both live and historical rates, you may limit your API request to a set of specific currencies by attaching the currencies parameter followed by any available 3-letter currency codes of your choice.

CurrencyLayer API

How To Create A Python Currency Converter App?

In this section, we’ll build a Python Flask web app that utilizes the CurrencyLayer API for currency conversion. You can follow this tutorial to learn how to integrate and interact with the API endpoints. To use the CurrencyLayer API, you first need to sign up and create a free account.

Start your PowerShell or Bash and type these commands to create a new folder and initialize a new Python Flask application.

Here, we’re using VS Code, and we have already opened it with the “code ” command. Now copy this source code to your app.py and use HTML web pages for data representation (you can find the complete code from this GitHub repository)

Explanation

As you can see, we have two simple endpoint calling functions and two functions to represent the received data to the user. It is straightforward. It gets the submitted data, builds up a URL string, and sends it as a request.

  • @app.route(‘/result/<string:usd_aud>/<string:usd_eur>/<string:usd_gbp>’): This route expects three string parameters representing the currencies conversion from USD to AUD, EUR, and GBP. It returns HTML displaying these rates.
  • @app.route(‘/rtrates’): This route handles both GET and POST requests. It displays a form for users to input their account key and desired currencies. On submission, it makes a request to the CurrencyLayer API to get real-time exchange rates and redirects to the result_realtime route.
  • @app.route(‘/result/<string:from_c>/<string:to_c>/<string:amount>/<string:quote>/<string:result>’): This route expects five string parameters representing the conversion details. It returns HTML displaying these details.
  • @app.route(‘/conversion’): Similar to /rtrates, this route handles both GET and POST requests. It displays a form for users to input their account key, source currency, target currency, and amount. On submission, it makes a request to the CurrencyLayerl API to perform currency conversion and redirects to the result_conversion route.

Note: Remember to add your unique API key to the above code.

How To Run Our CurrencyLayer API-based Python Currency Converter Web Application?

Depending on the configuration of your system, you can run your Flask web application with these commands. 

Running Python program

Demo in Action!

Python Currency conveter app - Fill out the form by giving what you want to convert

Fill out the form by using your desired currencies; source currency or base currency, target currency, and amount.

Conversion Result

Conversion Result

Python Project- Conversion Result

Get your free CurrencyLayer subscription API key right now and create accurate currency converter apps!

Conclusion

Currency conversion means the rate or value for which one currency, such as the Australian dollar, is exchanged for another currency, such as the Canadian dollar. Businesses and investors often need to convert currencies for international trade, foreign transactions, financial analysis, and mitigating financial risks. Travellers also need currency conversion to get accurate exchange rates when travelling to another country. Thus, currency converter apps are in demand. 

In this tutorial, we’ve created a simple Python currency converter app using an exchange rate Python API and Flask Python. We’ve used the CurrencyLayer API for our tutorial as it’s a reliable currency converter API Python that provides highly accurate exchange rate data. The API supports real-time and historical exchange rate data. It also offers a separate currency conversion endpoint that you can use to convert any amount from one currency to the target currency.

Frequently Asked Questions (FAQs)

How to convert currency in Python?

You can create a Python currency converter app using a currency converter API like the one we created in this tutorial.

How do you get live currency rates in Python?

You can use a currency conversion API like CurrencyLayer to integrate real-time/live currency exchange rates in your Python currency converter app.

How to easily convert currency?

You can use CurrencyLayer API to convert one currency to another easily. The API supports 168 world currencies, such as British Pound, Japanese Yen, Indian rupee, Kuwaiti dinar, US dollar, Hong Kong dollar, and more.

Related posts
API

How To Create A Weather App For Windows And Mobile Using An API

API

The 5 Types of API Marketplaces

API

Building Your Own Geolocation App with IP Geolocation API

APIIPLocation

What Is Geoblocking And How Does It Work?

Leave a Reply

Your email address will not be published. Required fields are marked *