截至 2026-03-28,FastAPI 官方首页和 CLI 文档仍然推荐:
pip install "fastapi[standard]"最小应用长这样:
from fastapi import FastAPI
app = FastAPI(title="软件城市政务大厅")
@app.get("/")
async def root():
return {"message": "政务大厅已开门"}开发环境运行:
fastapi dev生产模式运行:
fastapi run启动之后,默认会有几个你几乎每天都要看的入口:
http://127.0.0.1:8000/ -> 你的接口
http://127.0.0.1:8000/docs -> Swagger UI 交互文档
http://127.0.0.1:8000/redoc -> ReDoc 文档
http://127.0.0.1:8000/openapi.json -> 原始 OpenAPI SchemaFastAPI 最爽的第一击:
你不是先写代码,再另写文档。 你是写一次函数签名,代码和文档一起长出来。