Easy Currency Conversion in Heroku

In this tutorial we will build a simple currency converter app using Exchange rates API(A free currency converter API).We will deploy this app into Heroku.

We will create laravel application and deployed this app into heroku. Create HTTP api end-points to access some exchange api methods like latest exchange rate, currency conversion etc.

Development Environment

We are going to use PHP 7 with the help of Laravel (A powerful php framework to build php).I am using unofficial Laravel Exchange rates API Wrapper. Let’s setup the environment.

Assuming, you have already installed the latest stable PHP using xampp or by stable build.

Create A Laravel Project

Laravel is a web application framework with expressive, elegant syntax. A web framework provides a structure and starting point for creating your application. Create a laravel project using composer or laravel installer. Let’s install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, You need to install them? Run the below command to create a laravel project into the terminal-

composer create-project --prefer-dist laravel/laravel currency-conversion-app

We will rename .env.example file to .env file. This file will have application level environment variable Like ACCESS_KEY.

Install the unofficial Currency Exchange Rates API SDK using composer:

composer require benmajor/exchange-rates-api

GET The ACCESS_KEY and Add into .env

The ACCESS_KEY is a token that authorize you as an user to access exchange rates api, we need to register form and submit data. You will redirect dashboard page once successfully registered, here you will get Your API Access Key.

Let’s add into the laravel /currency-conversion-app/.env file.

ACCESS_KEY=

Create /latest End points

Let’s get the latest exchange rates from the European Central Bank:

Added entry into routes/api.php file:

Create ExchangeRateAPIController.php under Http/Controllers folder and define latest() method into this controller file:

We have created ExchangeRatesAPI() client using ACCESS_KEY, the fetch latest exchange currency rate and return as a response to the end user.

Get Historical data Using Easy Exchange API

Let’s get the latest exchange rates from the European Central Bank:

Added entry into routes/api.php file:

Create ExchangeRateAPIController.php under Http/Controllers folder and define historicalData() method into this controller file:

Deploy Easy Exchange API Laravel App on Heroku

Created Exchange Rates API laravel app into the development environment. Now deploy this application into the Heroku:

Install Heroku CLI

You can download heroku-cli at you system according to your OS:

https://devcenter.heroku.com/articles/heroku-cli#download-and-install

Once installation is finished, go back to your terminal and try to execute below command:

heroku login

The heroku login will appear. Click login.

You will you go back to your terminal you will see loggedin message into terminal.

Creating a Procfile

This file tells tells Heroku what command to use to launch the web server with the correct settings. Heroku will launch an Apache web server together with PHP to serve applications from the root directory(/public for laravel) of the project.

Inside your Procfile, put this code line.
web: vendor/bin/heroku-php-apache2 public/

git init – This is for git initialization
heroku create – This command will create your app and repository name in Heroku

Now, commit and push your project to Heroku.

git add .
git commit –m "initial commit"
git push heroku master

Add Config Vars settings in Heroku

Click settings then add the config vars. Follow some config in your .env file. The APP_ENV, ACCESS_KEY and APP_URL is your heroku url.

Then access your heroku app

http://servername.herokuapp.com/api/v1/latest

You will get latest Currency rates into your browser.

Conclusion:

We have created Exchange Rate API app using laravel, You can define endpoints as per your requirement. We have deployed laravel app into heroku. You can deployed exchange api into any cloud platform.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.