How to Deploy Flask App to Vercel for Free

Minh Vu

By Minh Vu

Updated Feb 03, 2024

Figure: How to Deploy Flask App to Vercel for Free

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

In this tutorial, I will show you how to deploy your Flask app to Vercel for free in a few simple steps.

This tutorial works as of 2024 as there are some tutorials out there that are outdated when Vercel changed its features. So don't worry, this tutorial will work for you.

Please note that Vercel only supports Flask as a serverless function, which means it can only serve as an API.

Contents

How to Deploy Flask App to Vercel for Free

1. Create a Flask app

First, let's create a simple Flask app. If you already have one, you can skip this step.

The project structure should look like this:

structure
flask-vercel ├─ vercel.json ├─ requirements.txt └─ api └─ index.py

To create a Flask app, run the following commands:

console
mkdir flask-vercel cd flask-vercel python -m venv venv source venv/bin/activate # use this for Linux/Mac # venv\Scripts\activate # use this for Windows pip install Flask

Then, create a file named api/index.py with the following content:

api/index.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello from Minh Vu!"

Let's test if the app works by running the following command:

console
flask --app api/index.py run

Then, open your browser and go to http://localhost:5000. You should see the following message:

browser
Hello from Minh Vu!

That's all, keep it simple for now. Let's move on to the next step.

2. Create a Vercel account

Vercel is a cloud hosting service that offers a generous free plan for developers. So you don't have to pay anything while following this tutorial.

To create an account, visit the registration page and follow the instructions.

This is a simple step so I won't go into details.

3. Deploy Flask app to Vercel using CLI

Now, let's deploy the Flask app to Vercel using the Vercel CLI.

Here are the steps:

  1. Install the Vercel CLI.

    console
    npm install -g vercel
  2. Create the vercel.json file and add the following content:

    vercel.json
    { "rewrites": [{ "source": "/(.*)", "destination": "/api/index" }] }
  3. Now, create the requirements.txt file:

    console
    pip freeze > requirements.txt
  4. Login to Vercel.

    console
    vercel login
  5. Choose Login with Email. Then enter your email and wait for the confirmation email from Vercel.

    console
    ? Enter your email address: <your_email>
  6. As you log in, you can deploy the Flask app to Vercel by running the following command:

    console
    vercel --prod

    Enter the same information with the image below.

    Deploy Flask app to Vercel
    Figure: Deploy Flask app to Vercel

Cool, the Flask app is deployed at https://flask-vercel-dun.vercel.app.

Conclusion

In this tutorial, you have learned how to deploy a Flask app to Vercel for free in a few simple steps. The post is updated as of 2024, so you can follow it without any issues.

If you have any questions, feel free to leave a comment below.

Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.