Please need help with deploying FastAPI app
Was created NextJS app with FastAPI
When make request to Python FastAPI it returns 500 Python exit code 1
Direct access to the endpoint shows me that
How it can be solved ?
Please need help with deploying FastAPI app
Was created NextJS app with FastAPI
When make request to Python FastAPI it returns 500 Python exit code 1
Direct access to the endpoint shows me that
How it can be solved ?
Full build logs
21:43:35.691 Running build in Washington, D.C., USA (East) – iad1
21:43:35.692 Build machine configuration: 2 cores, 8 GB
21:43:35.824 Cloning github.com/MaximSaveliev/nutrition-app-monolith (Branch: core-features, Commit: 90aa3f2)
21:43:36.024 Cloning completed: 200.000ms
21:43:37.106 Skipping build cache since Node.js version changed from "20.x" to "22.x"
21:43:38.349 Running "vercel build"
21:43:38.735 Vercel CLI 48.8.2
21:43:38.911 WARN! Due to `builds` existing in your configuration file, the Build and Development Settings defined in your Project Settings will not apply. Learn More: https://vercel.link/unused-build-settings
21:43:41.417 Installing dependencies...
21:43:54.479
21:43:54.480 added 450 packages in 13s
21:43:54.481
21:43:54.481 158 packages are looking for funding
21:43:54.481 run `npm fund` for details
21:43:54.541 Detected Next.js version: 16.0.1
21:43:54.546 Running "npm run build"
21:43:54.654
21:43:54.654 > build
21:43:54.655 > next build
21:43:54.655
21:43:55.464 Attention: Next.js now collects completely anonymous telemetry regarding usage.
21:43:55.465 This information is used to shape Next.js' roadmap and prioritize features.
21:43:55.465 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
21:43:55.465 https://nextjs.org/telemetry
21:43:55.466
21:43:55.480 ▲ Next.js 16.0.1 (Turbopack)
21:43:55.481
21:43:55.555 Creating an optimized production build ...
21:44:04.171 ✓ Compiled successfully in 8.1s
21:44:04.180 Running TypeScript ...
21:44:08.829 Collecting page data ...
21:44:09.217 Generating static pages (0/15) ...
21:44:09.727 Generating static pages (3/15)
21:44:09.728 Generating static pages (7/15)
21:44:09.837 Generating static pages (11/15)
21:44:09.844 ✓ Generating static pages (15/15) in 626.4ms
21:44:09.856 Finalizing page optimization ...
21:44:09.863
21:44:09.866 Route (app)
21:44:09.866 ┌ ○ /
21:44:09.866 ├ ○ /_not-found
21:44:09.866 ├ ƒ /auth/confirm
21:44:09.866 ├ ○ /auth/confirmed
21:44:09.866 ├ ƒ /auth/error
21:44:09.866 ├ ○ /auth/forgot-password
21:44:09.866 ├ ○ /auth/login
21:44:09.867 ├ ○ /auth/sign-up
21:44:09.867 ├ ○ /auth/sign-up-success
21:44:09.867 ├ ○ /auth/update-password
21:44:09.867 ├ ○ /opengraph-image.png
21:44:09.867 ├ ○ /protected
21:44:09.867 └ ○ /twitter-image.png
21:44:09.867
21:44:09.867
21:44:09.868 ○ (Static) prerendered as static content
21:44:09.868 ƒ (Dynamic) server-rendered on demand
21:44:09.868
21:44:10.114 Traced Next.js server files in: 37.86ms
21:44:10.276 Created all serverless functions in: 162.133ms
21:44:10.326 Collected static files (public/, static/, .next/static): 3.329ms
21:44:11.963 No Python version specified in pyproject.toml or Pipfile.lock. Using latest installed version: 3.12
21:44:13.271 Installing required dependencies from requirements.txt...
21:44:13.272 Using uv at "/usr/local/bin/uv"
21:44:14.541 Build Completed in /vercel/output [33s]
21:44:14.864 Deploying outputs...
21:44:27.700 Deployment completed
21:44:28.660 Creating build cache...
21:44:40.361 Created build cache: 11.700s
21:44:40.362 Uploading build cache [153.33 MB]
21:44:42.595 Build cache uploaded: 2.233s
Root cause candidates (based on your logs)
|
|
|----|
Type
|
|
|----|
Symptom
|
|
|----|
Likely Fix
|
|
|----|
1. Python runtime mismatch
|
|
|----|
Log shows: “No Python version specified… using 3.12.†Some FastAPI dependencies (e.g., uvicorn, starlette) may not yet fully support 3.12 on Vercel.
|
|
|----|
✅ Add a .python-version or runtime.txt file specifying 3.11. Example: echo “3.11.9” > runtime.txt.
|
|
|----|
2. Missing entrypoint
|
|
|----|
Vercel expects a file like api/index.py exporting a handler object or app (for FastAPI). If your FastAPI file is main.py, but you deployed /api, Vercel can’t find it.
|
|
|----|
✅ Ensure your project has /api/main.py and inside it: python\nfrom fastapi import FastAPI\napp = FastAPI()\n\n@app.get(‘/’)\ndef root():\n  return {\“status\”: \“ok\”}\n\n# Vercel handler\nhandler = app\n
|
|
|----|
3. Dependency install mismatch
|
|
|----|
You have a requirements.txt, but FastAPI imports may fail at runtime.
|
|
|----|
✅ Add uvicorn and fastapi explicitly: text\nfastapi\nuvicorn\n and redeploy.
|
|
|----|
4. Wrong folder or route mapping
|
|
|----|
The log shows both Next.js and FastAPI being built — ensure Vercel isn’t serving your backend under /.
|
|
|----|
✅ In your vercel.json: json\n{\n \“builds\”: [\n  {\“src\”: \“api/*.py\”, \“use\”: \“@vercel/python\”},\n  {\“src\”: \“next.config.js\”, \“use\”: \“@vercel/next\”}\n ],\n \“routes\”: [\n  {\“src\”: \“/api/(.*)\”, \“dest\”: \“/api/\\1.py\”}\n ]\n}\n
mkdir -p api && cat > api/main.py <<‘PYCODE’
from fastapi import FastAPI
app = FastAPI()
@app.get(“/”)
def read_root():
return {“status”: “ok”, “source”: “FastAPI on Vercel”}
handler = app
PYCODE
cat > requirements.txt <<‘REQS’
fastapi==0.115.0
uvicorn==0.32.0
starlette==0.37.2
REQS
cat > runtime.txt <<‘RUNTIME’
3.11.9
RUNTIME
cat > vercel.json <<‘JSON’
{
“version”: 2,
“builds”: [
{ “src”: “api/.py", “use”: “@vercel*/python” },
{ “src”: “next.config.js”, “use”: “@vercel/next” }
],
“routes”: [
{ “src”: "/api/(.*)”, “dest”: “/api/$1.py” },
{ “src”: “/(.*)”, “dest”: “/$1” }
]
}
JSON
echo “
PMEi-Vercel hybrid FastAPI+Next.js config created.
Now commit and run: vercel --prod”
Ok thank you sooo much
Will try
If it works would you like me to extend this config to support CORS + JSON body parsing (useful if your Next.js frontend calls FastAPI)?
will be so thankful ![]()
i have made some changes and it start works
but don’t like how code is ranged like folders and so on
so i think i make it work and after rerange code folder and files
and as i understand i need to change routs of build and sources
Excellent — here’s a production-ready FastAPI + Next.js on Vercel setup that includes
CORS support (so your Next.js frontend can call your API)
JSON body parsing (FastAPI does this automatically, but we’ll show proper usage)
Clean Vercel routing
Folder structure
root/
├── api/
│ └── main.py
├── requirements.txt
├── runtime.txt
├── next.config.js
└── vercel.json
![]()
api/main.py
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# ---- CORS configuration ----
# Replace with your actual deployment origin(s)
origins = [
"https://your-vercel-app.vercel.app",
"http://localhost:3000"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # or \["\*"\] during testing
allow_credentials=True,
allow_methods=\["\*"\],
allow_headers=\["\*"\],
)
# ---- Routes ----
@app.get(“/”)
def read_root():
return {"status": "ok", "source": "FastAPI on Vercel"}
@app.post(“/echo”)
async def echo_json(request: Request):
"""
Demonstrates JSON body parsing.
FastAPI automatically parses JSON from the request body.
"""
data = await request.json()
return {"received": data, "message": "Echo successful"}
# Required for Vercel handler
handler = app
Notes
FastAPI automatically parses JSON using request.json() or Pydantic models.
CORS middleware is crucial if your Next.js app calls /api endpoints from another origin.
![]()
requirements.txt
fastapi==0.115.0
uvicorn==0.32.0
starlette==0.37.2
![]()
runtime.txt
3.11.9
![]()
vercel.json
{
“version”: 2,
“builds”: [
{ "src": "api/\*.py", "use": "@vercel/python" },
{ "src": "next.config.js", "use": "@vercel/next" }
],
“routes”: [
{ "src": "/api/(.\*)", "dest": "/api/$1.py" },
{ "src": "/(.\*)", "dest": "/$1" }
]
}
Test
Local test
uvicorn api.main:app --host 0.0.0.0 --port 8000
Then:
GET http://localhost:8000/ → returns {“status”: “ok”, “source”: “FastAPI on Vercel”}
POST http://localhost:8000/echo with JSON body {“hello”: “world”} → returns the same JSON echoed back.
On Vercel
After deploy:
https://.vercel.app/api/
https://.vercel.app/api/echo
Result
Next.js frontend can safely call FastAPI backend via fetch(‘/api/echo’, {…}) with no CORS errors.
JSON request bodies parsed automatically.
Stable on Python 3.11 with lawful, minimal config.
That’s great to hear it’s working now!
The setup I shared gives you a clean working base — it connects Next.js (your frontend) and FastAPI (your backend) on Vercel so they can run together without errors.
It also includes support for CORS (so your frontend can talk to your API safely) and JSON parsing (so your API can handle data you send from forms or requests).
You’re right — once you reorganize your folders or move files around, you’ll just need to update the routes and source paths in the vercel.json file. That tells Vercel where your API and frontend live after you rearrange things.
So, what you’re doing makes sense — get it working first, then tidy the folder structure and adjust the build paths afterward. ![]()
Thank you so much it is very helpful!
New path of gaining experience )
Hello, Dave @philmirrorenginei
Sorry for one more answer
Does Vercel needs specific names of folders or not ?
And what means build routes?
And can I pass path to main.py where is not created endpoints just creates application instance in main.py in api/auth/py are endpoint
FastAPI docs i get via main_url/api/docs (mean SwagerUI)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.