Skip to content

截屏2026-09-12 14.11.31.png

FastAPI 技术文档 ​

FastAPI 是一个现代、快速(高性能)的 Python Web 框架,基于标准 Python 类型提示构建,用于创建 API。

特性 ​

  • 快速:性能可与 NodeJS 和 Go 比肩
  • 高效:提升开发速度约 200% 至 300%
  • 简单:易于使用和学习
  • 标准:基于 OpenAPI 和 JSON Schema

快速开始 ​

安装 ​

bash
pip install fastapi
pip install "uvicorn[standard]"

创建应用 ​

创建 main.py:

python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}

运行 ​

bash
uvicorn main:app --reload

访问:

适用读者 ​

  • 熟悉 Python 基础的开发者
  • 从 Flask / Django 迁移的开发者
  • AIagent 开发者
  • 需要构建异步 API 的工程师
  • 对类型提示与数据校验感兴趣的技术人员

贡献 ​

本文档持续更新。如果发现错误或有改进建议,欢迎提交 Issue 或 Pull Request。


开始阅读:快速开始