Create and deploy a flask API in Heroku (Part 2)
A guide for creating a flask API and deploying in Heroku
Hi All, welcome back to the second part of creating and deploying a flask API in Heroku.
In the previous post, we have developed a flask API and tried it in localhost. In this post, we will commit the code to GitHub and deploy it in Heroku.
Before we begin let's complete few steps that are pending.
Gunicorn is a Python WSGI HTTP server that will serve your Flask application.
pip install gunicorn
Now you need to create requirements.txt in the project root folder, else the Heroku application will fail to deploy. Run the below command to fetch all the dependencies used in your project.
pip freeze > requirements.txt
Adding a Procfile
Procfile in the project root folder and add the following line:
web: gunicorn app:app
The first app represents the name of the python file that runs your application. The second
app represents your app name.
Our project should have the folder structure as shown below. you can find all the code and folder structures here.
Let's deploy our code in Heroku. To begin that let's create a repo in your GitHub and commit all our codes into the repo.
Creating a Github Repo
Let’s create a GitHub repo and commit our code to it.
Creating a Heroku application
Log in to your Heroku account and create a new app with a unique name.
To add the remote GitHub repository, select Github as a deployment method and link the GitHub repo we have created earlier.
After that select Deploy Branch which reads the Procfile and install all the dependencies in the requirements file and deploy it.
Once deployed you can access your app using https://<app name>.herokuapp.com/.
Now that we have created and deployed a flask API, let's test it in postman.
Accessing Flask API in postman
Let's add a product by calling a POST method.
Now we will try to retrieve the products by calling a GET method.
Similarly, you can try calling other methods too.
From this series, we have learned how to create a new flask restful-API app and deploy it in Heroku.
For part 1 refer here
Stay healthy, stay safe. will meet you all in a new blog