Host a Serverless Flask App on Vercel
Tags: vercel, python, serverless, flask
Published: 2022-12-21 10:40:52

**Flask** is a popular Python web framework that makes it simple to create web applications and API's in a short period of time. If you've created a Flask app and are worried about configuring a server as Heroku's free plan is no more, then worry not; you can run Flask as a serverless function on Vercel (yes, you have heard it right; we are gonna deploy a Flask app on Vercel).

## Prerequisites
Before we begin, you should have the following things set up:

- A Vercel account. You can sign up for a free account at [vercel.com](https://vercel.com/).
- The Vercel CLI (Command Line Interface) installed on your computer. You can install the Vercel CLI by running the following command in your terminal.

```bash
npm install -g vercel
``` 



## Deploying the Flask App
Open up your favourite code editor and initialize the following files and folders in the below order:

```
project
│───vercel.json   
│───requirements.txt
└───api
     │───app.py
```


In the `app.py` file. Lets us write a basic Hello World program which you can modify as per your needs.


```python
from flask import Flask
app = Flask(__name__)

@app.get('/')
def hello_world():
    return "Hello, World!"
```

In the `requirements.txt`, write

```
Flask
```

It is the only PyPi package we need. The Web Server Gateway Interface (WSGI) is provided automatically by Vercel during the runtime.

The `vercel.json` file contains the configuration for this project.

A Sample Configuration for Flask is:

```json
{
  "routes": [
    {
      "src": "/(.*)",
      "dest": "api/app.py"
    }
  ]
}
```

The above code routes any request to the original page to the Flask Server which was written in `app.py`.

Now deploy the Flask app onto vercel by running:

```bash
vercel deploy --prod
```


Follow the prompts asked and Vercel will then build and deploy your app, and you will be provided with a URL where you can access your app. You can also access your website's URL by going to your Vercel dashboard.

The URL for my Flask App which I have made using above steps is [vercel-flask-demo-one.vercel.app](https://vercel-flask-demo-one.vercel.app/)


## Additional Configuration
If you want to customize the way your Flask app is deployed, you can add additional options to the `vercel.json` file. For example, you can specify the Python version to use, or add environment variables to your app, specify the maximum execution time and the memory for a request etc. 

## Bonus
If you are thinking about running FastAPI in Vercel then its absolutely possible. Just replace `Flask` in `requirements.txt` with `fastapi` and Vercel will automatically use the corresponding ASGI to serve it.

You can read more on: [Python Runtimes](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python)


I have created this blog post to show that it is possible to deploy common Python Web apps like Flask and FastAPI on Vercel by use of Serverless Functions as many people just use Vercel for hosting their Frontends and never fully explore it. In the next article, I will show you how you can host Serverless Bots on Vercel.


In case if you still have any questions regarding this post or want to discuss something with me feel free to connect on [LinkedIn](https://www.linkedin.com/in/dhanushreddy29/) or [Twitter](https://twitter.com/dhanushreddy291).


> If you run an organization and want me to write for you please do connect with me on my Socials 🙃