Site icon apilayer Blog

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:

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:
[crayon-6636bf9e7022a968975918/]
Example API response:

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:
[crayon-6636bf9e70235637083371/]
Example API response:

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:
[crayon-6636bf9e70238155698273/]
Example API response:

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. 

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.

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.
[crayon-6636bf9e7023a152223858/]
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)
[crayon-6636bf9e70246516251195/]

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.

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. 

Demo in Action!

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

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.

Exit mobile version