Skip to content

PaulD103/flask-herokuAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flask-herokuAPI

Requirements

  • Python: A recent Python 3 interpreter to run the Flask backend on.
  • Git: To communicate with your API over the Heroku CLI.
  • Brew: For the installation of heroku.

Creating a Flask API Backend

The next step is to create the Flask project. For this, open your terminal and change your directory to a place, where you want to create your API.

Now type this into your terminal.

Create directory

$ mkdir api

Move into this directory

$ cd api



Unix-based operating systems

I always create a virtual environment called venv in my project directory, so let's do that now:

$ python3 -m venv venv

After that you have to activate that environment:

$ source venv/bin/activate
(venv) $ _

For Windows

Note that the above is for Unix-based operating systems. If you are using Windows, then you will do this instead:

$ python -m venv venv
$ venv\Scripts\activate
(venv) $ _



For this simple example you need only two Python packages - flask and python-dotenv:

(venv) $ pip install flask python-dotenv

For this example I'm going to create a small, single file and single endpoint application. Here is my Flask API project, written as a single file called api.py:

from flask import Flask

app = Flask(__name__)

@app.route('/data')
def get_data():
    return {'data': 'This is the data of your api!'}

This little API responds to the /data URL with a JSON payload such as this:

{"data": "This is the data of your api!"}

As you probably know, Flask imports the application from the place indicated by the FLASK_APP environment variable. To avoid having to manually set this variable every time, I'm going to write a .flaskenv file, which Flask automatically imports into the environment on startup if it finds the python-dotenv package installed. Here is my .flaskenv file:

FLASK_APP=api.py
FLASK_ENV=development

I also added the FLASK_ENV variable, with a setting of development, which enables Flask's debug mode. In a production version, you have to delete the second line!

Run your project

At this point this basic Flask project is complete. To make sure that it is working well you can start it:

(venv) $ flask run

This will be the output in the terminal:

 * Serving Flask app "api.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 673-234-166

To stop the Flask server press Ctrl-C.

Deploy your api to Heroku

Step 1: Install Heroku CLI

(venv) $ brew tap heroku/brew && brew install heroku

Above command is for Mac, for other systems you can click here

Step 2: Install Gunicorn

(venv) $ pip3 install gunicorn

Step 3: Create requirements.txt

(venv) $ pip3 freeze > requirements.txt

Step 4: Create a file named Procfile

web: gunicorn api:app

Step 5: Create an app in Heroku

  • Go to the Heroku page and create an account, if you haven't done already.
  • Click on Create new app under New
  • Choose an name for your app and a region

Step 6: Login into your Heroku

(venv) $ heroku login

Step 7: Remote your project to your app

(venv) $ heroku git:remote -a [nameOfYourAppOnHeroku]
In my case: (venv) $ heroku git:remote -a flask-herokuapi

Step 8: Deploy your application

Commit your code to the repository and deploy it to Heroku using Git.
(venv) $ git add .
(venv) $ git commit -am "flask-api"
(venv) $ git push heroku main

Visit your api:

https://[nameOfYourAppOnHeroku].herokuapp.com/data
In my case: https://flask-herokuapi.herokuapp.com/data