- 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.
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
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) $ _
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!
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.
(venv) $ brew tap heroku/brew && brew install heroku
Above command is for Mac, for other systems you can click here
(venv) $ pip3 install gunicorn
(venv) $ pip3 freeze > requirements.txt
web: gunicorn api:app
- 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
(venv) $ heroku login
(venv) $ heroku git:remote -a [nameOfYourAppOnHeroku]
In my case: (venv) $ heroku git:remote -a flask-herokuapi
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
https://[nameOfYourAppOnHeroku].herokuapp.com/data
In my case: https://flask-herokuapi.herokuapp.com/data