Deploying FastAPI Applications on Vercel: A Step-by-Step Guide 🚀
A Beginner’s Guide to Deploying FastAPI/Flask Apps on Vercel.
Absolutely, let’s dive right in and build a basic FastAPI application to get our feet wet. This will be a great foundation for exploring its functionalities further.
Local setup :
To get started, we’ll need some helpful tools. Let’s install them using pip before creating our basic FastAPI app.
pip install fastapi uvicron
# we are using uvicorn for running our FastAPI
1. create a file called app.py
#app.py
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def hello_world():
return "Hello,World"
if __name__ == '__main__':
import uvicorn
uvicorn.run(app)
2. run it using the below command
python app.py
it should look like this
open http://127.0.0.1:8000/ link to see the application , it should be showing “Hello, World”
3. create a requirements.txt file
pip freeze > requirements.txt
4. create a vercel.json file, which is necessary for vercel deployment
{
"version": 2,
"builds": [
{
"src": "app.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "app.py"
}
]
}
5. create a new repo in Gihub and upload or push all the file we have created
Vercel Dashboard steps :
1. Log into Vercel and create a new project.
in Add New… dropdown , select Project
2. import the repo in Vercel, select the repo and click on Import
3. Configure the Project, give a proper name and if you want to pass secrets, give it in Environment Variable section
-> click on Deploy
4. wait for the deployment to happen
open deployment domain link showed there
🎉 your app is deployed on Vercel, Congratulations
Thanks