FastAPI: How to deploy FastAPI on Vercel
Assalam o Alaikum and Hi there! Today we will deploy our FastAPI on vercel because there is no free place to deploy it, other than vercel.
Roman urdu
Scroll to the bottom to read in English!
Mujhy umeed hai ky aap ny apna FastAPI ka project bana lya hoga aur wo chal bhi rha hoga, agar nhi tou aap ye blog parh skty hain.
Aap ny apnay project ky root mein vercel.json ki file banani hai, aur ab aap ka folder structure kuch is tarah ka dikhy ga:

vercel.json ki file mein aap ny ye code likhna hai:
{
"version": 2,
"builds": [
{
"src": "main.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "main.py"
}
]
}j
Ismein jahan, jahan pr main.py hai, wahan pr aap ny apni root ya starting file ka name likhna hai. Folder structure mein aap deikh skty hain, ky meri file ka name main.py hai, agar aap ki file ka name kuch aur hai, tou aap wo name likhein gy.
Aap ky starting point mein kuch is tarah ka code hoga:
from fastapi import FastAPI
app:FastAPI = FastAPI()
@app.get("/")
def index():
return {"message": "Hello, World"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", reload=True)
Pushing it to GitHub
Ab aap ny aik repo banani hai, aur apna code GitHub pr push kr dena hai.
Next aap ny vercel pr jana hai, aur right-top pr apko aik option dikhy gi, “Add New”, uspy click krky, aap ny “Project” pr click krna hai.

Ye aap ko aik new pane pr ly jaye ga, jahan wo aap ko GitHub sy connect krny ki option dy rha hoga, agar aap ny pehly nhi kya hua, aur agar aap ny pehly sy kya hua hai, tou wo aap ko apki repo’s dikha rha hoga.

Agar aap ko ismein aap ki repo nhi dikh rhi tou search kr lein, agar phir bhi nhi milti, tou aap ky pass yahan pr option aye gi, GitHub sy repo import krny ki.

Tou aap ny “Configure GitHub App” pr click krna hai, aur ye aap ky pass aik new window open kr dy ga. Jahan pr aap ko jo repo chahye usko select krna hai.

Scroll down karein jb tk apko ye na dikh jaye:

Yahan pr aap chahein tou “All repositories” choose kr lein, ya chahein tou “Only select repositories” choose kr lein.
Repo choose krny ky baad wo apko wapis vercel pr ly aye ga, aur us repo ky samnay import ki option dy ga. Us pr jesy hi click karein gy tou apkay pass ye modal popup hoga:

Yahan sy aap ny deploy pr click kr dena hai, aur apka project deploy ho jaye ga.
Deployment issue
Deployment krty waqt kuch major issues askty hain, usmein sb sy bari wajah, apky project mein requirements.txt ki file na hona hai, ya uska khali hona hai.
requirements.txt ki file create krny ky lye, apnay activated venv ky terminal ya cmd mein ye command run karein.
# pip ky sath
pip freeze > requirements.txt
# uv ky sath
uv pip freeze > requirements.txt
Joky aap ky pass kuch aisi dikhy gi:

Deployement Success
Agar koi issue nhi ata tou apka project deploy ho jaye ga, aur apkay pass kuch aisa pane arha hoga:

Ab aap ny kisi bhi dusray project sy jb apni API ko call krna hai, tou ye domain use krni hai
https://fast-test-kappa.vercel.app
# ya jo bhi apki domain hai
That’s it!
Aglay blog mein miltay hain, Python aur TypeScript mein kuch zabardast krty hain.
English
I expect that you have your FastAPI project up & running, if not please read this blog first.
First, you have to create a vercel.json file in the root of your project so, that your folder structure might look like this:

In your vercel.json file, copy/paste this code.
{
"version": 2,
"builds": [
{
"src": "main.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "main.py"
}
]
}j
In this code, “main.py”, is my filename, write your filename here in the places of main.py, that is the starting point of your project.
Your starting point code should look like this:
from fastapi import FastAPI
app:FastAPI = FastAPI()
@app.get("/")
def index():
return {"message": "Hello, World"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", reload=True)
Pushing to GitHub
Create a new repo on GitHub and push this code to GitHub.
Next, you need to go to vercel and at the right-top, you’ll see an option, “Add New”, after clicking on it, there will be a dropdown, and from it select “Project”.

It’ll take you to a new pane, where it’ll be asking you to connect to GitHub if you haven’t already, and if you have, then it’ll show you your repos.

If you don’t find your repo in this, search for it. If it is not there, then click “Configure GitHub App” to import your repo from GitHub.

After clicking on, Configure GitHub App, it’ll open a new window.

Scroll down, until you see this.

Now it’s up to you whether you want to choose “All repos” or “Only selected repos”.
After that, it’ll bring you back to vercel, where you will see an import button beside your project. As you click on it, this popup will appear.

Your project will be deployed, after clicking on deploy.
Deployment issues
You will face deployment issues if there is no requirements.txt file in your project, or if it's empty.
To create a requirements.txt file, open your terminal or cmd where venv is activated, and run this command.
# pip ky sath
pip freeze > requirements.txt
# uv ky sath
uv pip freeze > requirements.txt
The file will look like this from the inside,

Deployment Success
If your deployment is successful, then you’ll see this page.

Now to use this API in either your frontend or somewhere else, you need to use this domain:
https://fast-test-kappa.vercel.app
# or your domain name
That’s it!
See you in the next blog with some more exciting work on TypeScript & Python.