From 2afb38098ea4146fa263bd9ffc495cc6043d2c1e Mon Sep 17 00:00:00 2001 From: Aasish Raj Date: Thu, 28 Aug 2025 17:18:41 +0530 Subject: [PATCH] Add FastAPI example with Vercel configuration ### Description This commit introduces a new FastAPI example, including the main application file, a README for setup and deployment instructions, a requirements file for dependencies, and a Vercel configuration file for serverless deployment. ### Type of Change - [X] New Example - [ ] Example updates (Bug fixes, new features, etc.) - [ ] Other (changes to the codebase, but not to examples) --- python/fastapi/README.md | 48 +++++++++++++++++++++++++++++++++ python/fastapi/main.py | 11 ++++++++ python/fastapi/requirements.txt | 39 +++++++++++++++++++++++++++ python/fastapi/vercel.json | 14 ++++++++++ 4 files changed, 112 insertions(+) create mode 100644 python/fastapi/README.md create mode 100644 python/fastapi/main.py create mode 100644 python/fastapi/requirements.txt create mode 100644 python/fastapi/vercel.json diff --git a/python/fastapi/README.md b/python/fastapi/README.md new file mode 100644 index 0000000000..47bf83f3dc --- /dev/null +++ b/python/fastapi/README.md @@ -0,0 +1,48 @@ +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Ffastapi) + +# FastAPI + Vercel + +Use FastAPI on Vercel with Serverless Functions using the Python Runtime. + +## How it Works + +- The ASGI app is exported as `app` in `main.py` and serves `/`: + +```python +# main.py +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"message": "Hello, World!"} +``` + +- Vercel builds and routes all requests to `main.py` with `@vercel/python`: + +```json +// vercel.json +{ + "builds": [{ "src": "main.py", "use": "@vercel/python" }], + "routes": [{ "src": "/(.*)", "dest": "main.py" }] +} +``` + +## Running Locally + +```bash +uvicorn main:app --reload +# or +python main.py +``` + +App available at `http://localhost:8000`. + +## One-Click Deploy + +Deploy with Vercel: + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fpython%2Ffastapi) + + diff --git a/python/fastapi/main.py b/python/fastapi/main.py new file mode 100644 index 0000000000..63767f416c --- /dev/null +++ b/python/fastapi/main.py @@ -0,0 +1,11 @@ +from fastapi import FastAPI +import uvicorn + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"message": "Hello, World!"} + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file diff --git a/python/fastapi/requirements.txt b/python/fastapi/requirements.txt new file mode 100644 index 0000000000..f5c4271389 --- /dev/null +++ b/python/fastapi/requirements.txt @@ -0,0 +1,39 @@ +annotated-types==0.7.0 +anyio==4.10.0 +certifi==2025.8.3 +click==8.2.1 +dnspython==2.7.0 +email-validator==2.3.0 +fastapi==0.116.1 +fastapi-cli==0.0.8 +fastapi-cloud-cli==0.1.5 +h11==0.16.0 +httpcore==1.0.9 +httptools==0.6.4 +httpx==0.28.1 +idna==3.10 +Jinja2==3.1.6 +markdown-it-py==4.0.0 +MarkupSafe==3.0.2 +mdurl==0.1.2 +pydantic==2.11.7 +pydantic_core==2.33.2 +Pygments==2.19.2 +python-dotenv==1.1.1 +python-multipart==0.0.20 +PyYAML==6.0.2 +rich==14.1.0 +rich-toolkit==0.15.0 +rignore==0.6.4 +sentry-sdk==2.35.1 +shellingham==1.5.4 +sniffio==1.3.1 +starlette==0.47.3 +typer==0.16.1 +typing-inspection==0.4.1 +typing_extensions==4.15.0 +urllib3==2.5.0 +uvicorn==0.35.0 +uvloop==0.21.0 +watchfiles==1.1.0 +websockets==15.0.1 diff --git a/python/fastapi/vercel.json b/python/fastapi/vercel.json new file mode 100644 index 0000000000..bc1bf39751 --- /dev/null +++ b/python/fastapi/vercel.json @@ -0,0 +1,14 @@ +{ + "builds": [ + { + "src": "main.py", + "use": "@vercel/python" + } + ], + "routes": [ + { + "src": "/(.*)", + "dest": "main.py" + } + ] + } \ No newline at end of file