How to deploy a Flask API (the Easiest, Fastest, and Cheapest way)

[This article was first published on Python – Predictive Hacks, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

So we have created our Flask API. Now what? How are we going to share our application to the world?

We found the best way to do it without wasting time setting up servers. The only thing you need is your Github repo with your flak API. If you are not familiar with how to use flask, you can see this Python Flask API example that we will use in this post.

First of all, you have to set up your Github repo. There should be an app.py file with your flask code and a requirements.txt containing your required libraries. Feel free to use the repo dominant-colors-of-image that we will use here.

Let’s have a look at our Github repo.

deploy a flask api

The Deployment

We will do it using Digital Ocean. It has a new service called Apps and you can deploy your API by just using your Github repo. You can sign up with our referral link and get 100$ for 60 days to spend on your projects.

After you signed up, go to the sidebar and click Apps. Then click on Launch Your App. It will ask you to connect your Github account so it can have access to your personal Repos. Then you have to select your API’s Repo and click Next.

flask API digital ocean

Now you have to select a Name and Region of your Server and click Next. We will leave the defaults.

python flask API digital ocean

This is an important step. You have to add the right port of your Flask API. Our API runs at 9007 so we have to change the default 8080 to 9007. Then, be sure that the Run Command is the right one. This is just the command you use to run your API as you do locally.

python flask API digital ocean deployment

Finally, we have to select the size of our server. We will select the cheapest one for only 5$ per month. Then we click Launch Basic App and the deployment start taking place!

python flask API digital ocean deployment

We just have to wait a few minutes and if everything is ok, our API will be deployed.

python flask API digital ocean deployment

Now, we are ready to test it! The URL under the name above is the URL that our API uses. Let’s use this image url to see if it works.

How to deploy a Flask API (the Easiest, Fastest, and Cheapest way) 1
https://dominant-color-of-image-6xbq9.ondigitalocean.app/?url=https://images.unsplash.com/photo-1607053075722-dff3fb966413?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80
{
black: 0.1918,
darkgray: 0.0865,
darkolivegreen: 0.0377,
darkslategray: 0.2339,
dimgray: 0.0643,
gainsboro: 0.0993,
gray: 0.038,
lightgray: 0.088,
lightslategray: 0.0375,
silver: 0.0951
}

Success! With just a few clicks we managed to deploy a flask API. Thanks to services like Digital Ocean we can now focus on our code and models instead of spending days of research and development to set up servers. Also, It automatically updates itself when you push any updates to your GitHub Repo. Lastly, you can use it for production using the professional package.

How to deploy a Flask API (the Easiest, Fastest, and Cheapest way) 2
To leave a comment for the author, please follow the link and comment on their blog: Python – Predictive Hacks.

Want to share your content on python-bloggers? click here.