全书目录

第二篇:FastAPI 完全指南 —— 软件城市政务大厅宇宙

第三章:启动大厅 —— 从 Hello World 到自动文档

1 分钟 142 字 第 49 / 962 个阅读单元

截至 2026-03-28,FastAPI 官方首页和 CLI 文档仍然推荐:

bash
pip install "fastapi[standard]"

最小应用长这样:

py
from fastapi import FastAPI

app = FastAPI(title="软件城市政务大厅")

@app.get("/")
async def root():
    return {"message": "政务大厅已开门"}

开发环境运行:

bash
fastapi dev

生产模式运行:

bash
fastapi run

启动之后,默认会有几个你几乎每天都要看的入口:

text
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 Schema

FastAPI 最爽的第一击

你不是先写代码,再另写文档。 你是写一次函数签名,代码和文档一起长出来。