fastapiでASGI/単純なRestfulな非同期APIをすぐに用意したい場合に便利だと思った。
普通ならEC2インスタンスを建てるかLambdaを発行しがちだが、こちらの方がもっと簡単だった。
API疎通までの道のり
githubプロジェクトで下記構成のrepo作成。

index.py
from fastapi import FastAPI
import os
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello from FastAPI on Vercel!"}
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
@app.get("/test-env")
async def get_test_env():
value = os.getenv("TEST_ENV", "NOT_SET")
return {"TEST_ENV": value}requirements.txt
annotated-types==0.7.0
anyio==4.9.0
fastapi==0.115.12
idna==3.10
pydantic==2.11.5
pydantic_core==2.33.2
sniffio==1.3.1
starlette==0.46.2
typing-inspection==0.4.1
typing_extensions==4.14.0vercel.json
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/api/index"
}
]
}
Vercelにログインする。
新規プロジェクト作成→環境変数をTEST_ENV = 適当な値にしてenv疎通確認
github連携で上記repo指定。
勝手にビルドが始まり、”/”にルートが展開される。
