Deploy a Flask app on Vercel
Deploy a Flask app to Vercel with the Python runtime and Vercel Functions.
Vercel looks for a Flask instance named app at supported entrypoints in
your repository.
Create a Flask app or use an existing one:
Initialize a new Flask project with the Vercel CLI init command:
vc init flaskThis clones the Flask example repository in a directory called flask.
To run a Flask application on Vercel, define an app instance that initializes Flask at a supported entrypoint:
app.py,index.py,server.py,main.py,wsgi.py, orasgi.py- the same filenames inside
src/orapp/
For example:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return {"message": "Hello, World!"}To point Vercel to a Flask app in a custom module, set tool.vercel.entrypoint in pyproject.toml:
[tool.vercel]
entrypoint = "backend.server:app"The tool.vercel.entrypoint value tells Vercel to look for a Flask instance named app in ./backend/server.py.
The build property in [tool.vercel.scripts] defines the Build Command for Flask deployments. It runs after dependencies are installed and before your application is deployed.
[tool.vercel.scripts]
build = "python build.py"For example:
def main():
print("Running build command...")
with open("build.txt", "w") as f:
f.write("BUILD_COMMAND")
if __name__ == "__main__":
main()Use vercel dev to run your application locally.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
vercel devDeploy the project by connecting your Git repository or by using the Vercel CLI:
vc deployTo serve static assets, place them in the public/** directory. Vercel serves
those files from the CDN with default headers
unless you override them in vercel.json.
from flask import Flask, redirect
app = Flask(__name__)
@app.route("/favicon.ico")
def favicon():
# /vercel.svg is automatically served when included in the public/** directory.
return redirect("/vercel.svg", code=307)When you deploy a Flask app to Vercel, it becomes a single Vercel Function. Vercel uses Fluid compute by default, so the function scales with traffic.
To configure that function, add an entry to the functions
object in vercel.json keyed by your
resolved entrypoint file. For example, to let an app defined in main.py
run for up to 60 seconds, set maxDuration:
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"functions": {
"main.py": {
"maxDuration": 60
}
}
}For more options, see Configuring
functions and the functions
property.
All Vercel Functions limitations apply to Flask applications, including:
- Application size: The Flask application becomes a single bundle, which has a standard bundle size limit of 500MB. Large Functions support Python bundles up to 5GB on Fluid compute when enabled (public beta).
For more about deploying Flask on Vercel, see:
Was this helpful?