fastapi/fastapi
GitHub: fastapi/fastapi
FastAPI 是一个高性能、易于学习、基于 Python 类型提示的现代 Web 框架,用于快速构建生产级 API。
Stars: 100768 | Forks: 9655
FastAPI framework, high performance, easy to learn, fast to code, ready for production
**文档**:[https://fastapi.tiangolo.com](https://fastapi.tiangolo.com) **源代码**:[https://github.com/fastapi/fastapi](https://github.com/fastapi/fastapi) FastAPI 是一个现代、快速(高性能)的 Web 框架,用于基于标准 Python 类型提示使用 Python 构建 API。 主要特性包括: * **快速**:非常高的性能,与 **NodeJS** 和 **Go** 并驾齐驱(得益于 Starlette 和 Pydantic)。[目前可用的最快 Python 框架之一](#performance)。 * **编码快捷**:将功能开发速度提高约 200% 到 300%。 * * **Bug 更少**:减少约 40% 的人为(开发者)诱导错误。 * * **直观**:出色的编辑器支持。补全无处不在。调试时间更少。 * **简单**:设计得易于使用和学习。阅读文档的时间更少。 * **简明**:最小化代码重复。每个参数声明都能实现多种功能。Bug 更少。 * **健壮**:获取可用于生产环境的代码。带有自动交互式文档。 * **基于标准**:基于(并完全兼容)API 的开放标准:[OpenAPI](https://github.com/OAI/OpenAPI-Specification)(以前称为 Swagger)和 [JSON Schema](https://json-schema.org/)。 * 估算基于内部开发团队构建生产应用程序所进行的测试。 ## 评价
“_[...] 我最近大量使用 **FastAPI**。 [...] 我实际上正计划将其用于我团队在 **Microsoft 的所有 ML 服务**。其中一些正在集成到核心 **Windows** 产品和某些 **Office** 产品中._”
## FastAPI 配置
[**FastAPI Conf '26**](https://fastapiconf.com) 将于 **2026 年 10 月 28 日**在**荷兰阿姆斯特丹**举行。一切关于 FastAPI 的内容,全部来自官方源头。 🎤
Kabir Khan - Microsoft (ref)
“_我们采用了 **FastAPI** 库来启动一个 **REST** 服务器,可以通过查询它来获取 **预测结果**。[用于 Ludwig]_”
Piero Molino, Yaroslav Dudin, 和 Sai Sumanth Miryala - Uber (ref)
“_**Netflix** 很高兴地宣布开源发布我们的**危机管理**编排框架:**Dispatch**![使用 **FastAPI** 构建]_”
Kevin Glisson, Marc Vilanova, Forest Monsen - Netflix (ref)
“_如果有人想要构建生产级的 Python API,我强烈推荐 **FastAPI**。它**设计精美**、**易于使用**且**高度可扩展**,它已成为我们 API 优先开发策略中的**关键组件**,并驱动着许多自动化和服务,例如我们的虚拟 TAC 工程师._”
Deon Pillsbury - Cisco (ref)
## FastAPI 迷你纪录片
在 2025 年底发布了一部 [FastAPI 迷你纪录片](https://www.youtube.com/watch?v=mpR8ngthqiE),你可以在线观看:
## **Typer**,CLI 界的 FastAPI
```
$ pip install "fastapi[standard]"
---> 100%
```
**注意**:确保你将 `"fastapi[standard]"` 放在引号中,以确保它在所有终端中都能正常工作。
## 示例
### 创建它
创建一个文件 `main.py`,内容如下:
```
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
```
或者使用 async def...
如果你的代码使用 `async` / `await`,请使用 `async def`:
```
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
```
**注意**:
如果你不了解,请查看文档中的 _“赶时间?”_ 部分,了解 [`async` 和 `await`](https://fastapi.tiangolo.com/async/#in-a-hurry)。
```
$ fastapi dev
╭────────── FastAPI CLI - Development mode ───────────╮
│ │
│ Serving at: http://127.0.0.1:8000 │
│ │
│ API docs: http://127.0.0.1:8000/docs │
│ │
│ Running in development mode, for production use: │
│ │
│ fastapi run │
│ │
╰─────────────────────────────────────────────────────╯
INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [2248755] using WatchFiles
INFO: Started server process [2248757]
INFO: Waiting for application startup.
INFO: Application startup complete.
```
关于 fastapi dev 命令...
`fastapi dev` 命令会自动读取你的 `main.py` 文件,检测其中的 **FastAPI** 应用,并使用 [Uvicorn](https://www.uvicorn.dev) 启动服务器。
默认情况下,`fastapi dev` 将启用自动重载以进行本地开发。
你可以在 [FastAPI CLI 文档](https://fastapi.tiangolo.com/fastapi-cli/)中阅读更多相关信息。
```
$ fastapi deploy
Deploying to FastAPI Cloud...
✅ Deployment successful!
🐔 Ready the chicken! Your app is ready at https://myapp.fastapicloud.dev
```
CLI 将自动检测你的 FastAPI 应用并将其部署到云端。如果你未登录,你的浏览器将打开以完成身份验证过程。
就是这样!现在你可以通过该 URL 访问你的应用。 ✨
#### 关于 FastAPI Cloud
**[FastAPI Cloud](https://fastapicloud.com)** 由 **FastAPI** 背后的同一作者和团队构建。
它简化了以最少精力**构建**、**部署**和**访问** API 的过程。
它将使用 FastAPI 构建应用的相同**开发者体验**带到了将它们**部署**到云端的过程中。 🎉
FastAPI Cloud 是 *FastAPI 和朋友们* 开源项目的主要赞助商和资金提供者。 ✨
#### 部署到其他云服务提供商
FastAPI 是开源的且基于标准。你可以将 FastAPI 应用部署到你选择的任何云服务提供商。
遵循你的云服务提供商的指南,与他们一起部署 FastAPI 应用。 🤓
## 性能
独立的 TechEmpower 基准测试表明,在 Uvicorn 下运行的 **FastAPI** 应用程序是[目前可用的最快 Python 框架之一](https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7),仅排在 Starlette 和 Uvicorn(由 FastAPI 内部使用)之后。(*)
要了解更多相关信息,请参阅 [基准测试](https://fastapi.tiangolo.com/benchmarks/)部分。
## 依赖项
FastAPI 依赖于 Pydantic 和 Starlette。
### `standard` 依赖项
当你使用 `pip install "fastapi[standard]"` 安装 FastAPI 时,它会包含 `standard` 可选依赖组:
被 Pydantic 使用:
* [`email-validator`](https://github.com/JoshData/python-email-validator) - 用于电子邮件验证。
被 Starlette 使用:
* [`httpx`](https://www.python-httpx.org) - 如果你想使用 `TestClient` 则是必需的。
* [`jinja2`](https://jinja.palletsprojects.com) - 如果你想使用默认模板配置则是必需的。
* [`python-multipart`](https://github.com/Kludex/python-multipart) - 如果你想使用 `request.form()` 支持表单“解析”则是必需的。
被 FastAPI 使用:
* [`uvicorn`](https://www.uvicorn.dev) - 用于加载和提供你的应用的服务器。这包括 `uvicorn[standard]`,后者包含高性能服务所需的一些依赖项(例如 `uvloop`)。
* `fastapi-cli[standard]` - 用于提供 `fastapi` 命令。
* 这包括 `fastapi-cloud-cli`,它允许你将 FastAPI 应用部署到 [FastAPI Cloud](https://fastapicloud.com)。
### 不带 `standard` 依赖项
如果你不想包含 `standard` 可选依赖项,你可以使用 `pip install fastapi` 而不是 `pip install "fastapi[standard]"` 来安装。
### 不带 `fastapi-cloud-cli`
如果你想安装带有 standard 依赖项但没有 `fastapi-cloud-cli` 的 FastAPI,你可以使用 `pip install "fastapi[standard-no-fastapi-cloud-cli]"` 来安装。
### 额外可选依赖项
你可能需要安装一些额外的依赖项。
额外的可选 Pydantic 依赖项:
* [`pydantic-settings`](https://docs.pydantic.dev/latest/usage/pydantic_settings/) - 用于设置管理。
* [`pydantic-extra-types`](https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/) - 用于与 Pydantic 一起使用的额外类型。
额外的可选 FastAPI 依赖项:
* [`orjson`](https://github.com/ijl/orjson) - 如果你想使用 `ORJSONResponse` 则是必需的。
* [`ujson`](https://github.com/esnme/ultrajson) - 如果你想使用 `UJSONResponse` 则是必需的。
## 许可证
该项目在 MIT 许可证条款下授权。标签:API, AV绕过, FastAPI, Python, Web框架, 后端开发, 无后门, 逆向工具
