Token导航 LogoToken导航TokenDH.com
开发需要联网github未标认证来源可访问许可证需确认审计通过

fastapiFastAPI 开发

Agent Skill

用于辅助 Python 项目开发、测试、依赖管理和常见框架工作流。它适合让 Agent 阅读 Python 代码、定位测试问题、整理运行命令、生成脚本或分析数据处理逻辑。使用时需要确认项目虚拟环境、依赖版本和测试入口;涉及执行脚本、读写文件、访问数据库或调用外部 API 时,应先明确运行目录和输入输出范围,避免误改生产数据。

总安装

48,480

周安装

1,983

GitHub Stars

97,781

下载量

15,840
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:fastapi(FastAPI 开发)
来源仓库:https://github.com/fastapi/fastapi
仓库路径:skills/fastapi
安装命令:
npx skills add https://github.com/fastapi/fastapi --skill fastapi
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/fastapi/fastapi --skill fastapi

简介

遵循 FastAPI 最佳实践进行现代 API 开发,强调类型安全与性能优化。

  • 适用于 RESTful 服务构建、微服务架构与前后端分离项目。
  • 通过 Pydantic 模型定义数据结构与响应格式,自动生成接口文档。
  • 异步函数仅用于非阻塞操作,避免在事件循环中执行 CPU 密集型计算。
  • 生产环境务必启用 CORS、速率限制与安全中间件防护措施。

SKILL.md

FastAPI

Official FastAPI skill to write code with best practices, keeping up to date with new versions and features.

Use the fastapi CLI

Run the development server on localhost with reload:

fastapi dev

Run the production server:

fastapi run

Add an entrypoint in pyproject.toml

FastAPI CLI will read the entrypoint in pyproject.toml to know where the FastAPI app is declared.

[tool.fastapi]
entrypoint = "my_app.main:app"

Use fastapi with a path

When adding the entrypoint to pyproject.toml is not possible, or the user explicitly asks not to, or it's running an independent small app, you can pass the app file path to the fastapi command:

fastapi dev my_app/main.py

Prefer to set the entrypoint in pyproject.toml when possible.

Use Annotated

Always prefer the Annotated style for parameter and dependency declarations.

It keeps the function signatures working in other contexts, respects the types, allows reusability.

In Parameter Declarations

Use Annotated for parameter declarations, including Path, Query, Header, etc.:

from typing import Annotated

from fastapi import FastAPI, Path, Query

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(
    item_id: Annotated[int, Path(ge=1, description="The item ID")],
    q: Annotated[str | None, Query(max_length=50)] = None,
):
    return {"message": "Hello World"}

instead of:

# DO NOT DO THIS
@app.get("/items/{item_id}")
async def read_item(
    item_id: int = Path(ge=1, description="The item ID"),
    q: str | None = Query(default=None, max_length=50),
):
    return {"message": "Hello World"}

For Dependencies

Use Annotated for dependencies with Depends().

Unless asked not to, create a new type alias for the dependency to allow re-using it.

from typing import Annotated

from fastapi import Depends, FastAPI

app = FastAPI()

def get_current_user():
    return {"username": "johndoe"}

CurrentUserDep = Annotated[dict, Depends(get_current_user)]

@app.get("/items/")
async def read_item(current_user: CurrentUserDep):
    return {"message": "Hello World"}

instead of:

# DO NOT DO THIS
@app.get("/items/")
async def read_item(current_user: dict = Depends(get_current_user)):
    return {"message": "Hello World"}

Do not use Ellipsis for *path operations* or Pydantic models

Do not use ... as a default value for required parameters, it's not needed and not recommended.

Do this, without Ellipsis (...):

from typing import Annotated

from fastapi import FastAPI, Query
from pydantic import BaseModel, Field

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float = Field(gt=0)

app = FastAPI()

@app.post("/items/")
async def create_item(item: Item, project_id: Annotated[int, Query()]): ...

instead of this:

# DO NOT DO THIS
class Item(BaseModel):
    name: str = ...
    description: str | None = None
    price: float = Field(..., gt=0)

app = FastAPI()

@app.post("/items/")
async def create_item(item: Item, project_id: Annotated[int, Query(...)]): ...

Return Type or Response Model

When possible, include a return type. It will be used to validate, filter, document, and serialize the response.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    description: str | None = None

@app.get("/items/me")
async def get_item() -> Item:
    return Item(name="Plumbus", description="All-purpose home device")

Important: Return types or response models are what filter data ensuring no sensitive information is exposed. And they are used to serialize data with Pydantic (in Rust), this is the main idea that can increase response performance.

The return type doesn't have to be a Pydantic model, it could be a different type, like a list of integers, or a dict, etc.

When to use response_model instead

If the return type is not the same as the type that you want to use to validate, filter, or serialize, use the response_model parameter on the decorator instead.

from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    description: str | None = None

@app.get("/items/me", response_model=Item)
async def get_item() -> Any:
    return {"name": "Foo", "description": "A very nice Item"}

This can be particularly useful when filtering data to expose only the public fields and avoid exposing sensitive information.

from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class InternalItem(BaseModel):
    name: str
    description: str | None = None
    secret_key: str

class Item(BaseModel):
    name: str
    description: str | None = None

@app.get("/items/me", response_model=Item)
async def get_item() -> Any:
    item = InternalItem(
        name="Foo", description="A very nice Item", secret_key="supersecret"
    )
    return item

Performance

Do not use ORJSONResponse or UJSONResponse, they are deprecated.

Instead, declare a return type or response model. Pydantic will handle the data serialization on the Rust side.

Including Routers

When declaring routers, prefer to add router level parameters like prefix, tags, etc. to the router itself, instead of in include_router().

Do this:

from fastapi import APIRouter, FastAPI

app = FastAPI()

router = APIRouter(prefix="/items", tags=["items"])

@router.get("/")
async def list_items():
    return []

# In main.py
app.include_router(router)

instead of this:

# DO NOT DO THIS
from fastapi import APIRouter, FastAPI

app = FastAPI()

router = APIRouter()

@router.get("/")
async def list_items():
    return []

# In main.py
app.include_router(router, prefix="/items", tags=["items"])

There could be exceptions, but try to follow this convention.

Apply shared dependencies at the router level via dependencies=[Depends(...)].

Dependency Injection

See the dependency injection reference for detailed patterns including yield with scope, and class dependencies.

Use dependencies when the logic can't be declared in Pydantic validation, depends on external resources, needs cleanup (with yield), or is shared across endpoints.

Apply shared dependencies at the router level via dependencies=[Depends(...)].

Async vs Sync *path operations*

Use async *path operations* only when fully certain that the logic called inside is compatible with async and await (it's called with await) or that doesn't block.

from fastapi import FastAPI

app = FastAPI()

# Use async def when calling async code
@app.get("/async-items/")
async def read_async_items():
    data = await some_async_library.fetch_items()
    return data

# Use plain def when calling blocking/sync code or when in doubt
@app.get("/items/")
def read_items():
    data = some_blocking_library.fetch_items()
    return data

In case of doubt, or by default, use regular def functions, those will be run in a threadpool so they don't block the event loop.

The same rules apply to dependencies.

Make sure blocking code is not run inside of async functions. The logic will work, but will damage the performance heavily.

When needing to mix blocking and async code, see Asyncer in the other tools reference.

Streaming (JSON Lines, SSE, bytes)

See the streaming reference for JSON Lines, Server-Sent Events (EventSourceResponse, ServerSentEvent), and byte streaming (StreamingResponse) patterns.

Tooling

See the other tools reference for details on uv, Ruff, ty for package management, linting, type checking, formatting, etc.

Other Libraries

See the other tools reference for details on other libraries:

  • Asyncer for handling async and await, concurrency, mixing async and blocking code, prefer it over AnyIO or asyncio.
  • SQLModel for working with SQL databases, prefer it over SQLAlchemy.
  • HTTPX for interacting with HTTP (other APIs), prefer it over Requests.

Do not use Pydantic RootModels

Do not use Pydantic RootModel, instead use regular type annotations with Annotated and Pydantic validation utilities.

For example, for a list with validations you could do:

from typing import Annotated

from fastapi import Body, FastAPI
from pydantic import Field

app = FastAPI()

@app.post("/items/")
async def create_items(items: Annotated[list[int], Field(min_length=1), Body()]):
    return items

instead of:

# DO NOT DO THIS
from typing import Annotated

from fastapi import FastAPI
from pydantic import Field, RootModel

app = FastAPI()

class ItemList(RootModel[Annotated[list[int], Field(min_length=1)]]):
    pass

@app.post("/items/")
async def create_items(items: ItemList):
    return items

FastAPI supports these type annotations and will create a Pydantic TypeAdapter for them, so that types can work as normally and there's no need for the custom logic and types in RootModels.

Use one HTTP operation per function

Don't mix HTTP operations in a single function, having one function per HTTP operation helps separate concerns and organize the code.

Do this:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str

@app.get("/items/")
async def list_items():
    return []

@app.post("/items/")
async def create_item(item: Item):
    return item

instead of this:

# DO NOT DO THIS
from fastapi import FastAPI, Request
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str

@app.api_route("/items/", methods=["GET", "POST"])
async def handle_items(request: Request):
    if request.method == "GET":
        return []

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

37.8%
按下载量换算5,988

Claude

30.88%
按下载量换算4,891

Cursor

17.55%
按下载量换算2,780

Gemini CLI

8.58%
按下载量换算1,359

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills