fastapi-best-practices

fastapi-best-practices is a skill for Claude Code, Codex from guyulong/cn-agent-skills. It costs 13 tokens per session (783 once invoked), scanned A, original, MIT.

A Chinese-language guide to recommended practices for building FastAPI web services. FastAPI is a Python framework for creating web APIs, which are interfaces that let software exchange data.

In plain words
What is it for?
Use it as a reference when organizing a FastAPI application, defining routes and request models, connecting a database, adding services, or setting up tests and migrations.
Why use it?
It gives a project structure and code patterns for common concerns such as routes, data models, databases, services, authentication, and tests.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it as a reference when organizing a FastAPI application, defining routes and request models, connecting a database, adding services, or setting up tests and migrations.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/guyulong/cn-agent-skills/fastapi-best-practices
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add guyulong/cn-agent-skills --skill fastapi-best-practices
Clone the repo
git clone --depth 1 https://github.com/guyulong/cn-agent-skills

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for fastapi-best-practices

README.md
[![agentmods](https://agentmods.dev/badge/skills/guyulong/cn-agent-skills/fastapi-best-practices/github.svg)](https://agentmods.dev/skills/guyulong/cn-agent-skills/fastapi-best-practices)
Your own site
<a href="https://agentmods.dev/skills/guyulong/cn-agent-skills/fastapi-best-practices"><img src="https://agentmods.dev/badge/skills/guyulong/cn-agent-skills/fastapi-best-practices/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for fastapi-best-practices

Your own site · 80×15
<a href="https://agentmods.dev/skills/guyulong/cn-agent-skills/fastapi-best-practices"><img src="https://agentmods.dev/badge/skills/guyulong/cn-agent-skills/fastapi-best-practices.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 13 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 783 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00013 $0.00783
Opus 5 $0.00006 $0.00392
Sonnet 5 $0.00003 $0.00157
Haiku 4.5 $0.00001 $0.00078

Measured 9d ago against content hash ee3be7e3f98d, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-09, from the pricing page.

Security

Grade A, and why

fastapi-best-practices scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 9d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/fastapi-best-practices/SKILL.md · 110 lines

How it starts

The opening of the file, as written. The whole thing — 110 lines — stays where its author put it; the contents beside it link to each section on GitHub.

FastAPI 中文最佳实践

项目结构

├── app/
│   ├── __init__.py
│   ├── main.py           # 入口文件
│   ├── config.py          # 配置
│   ├── database.py        # 数据库连接
│   ├── models/            # 数据模型
│   │   ├── user.py
│   │   └── order.py
│   ├── schemas/           # Pydantic模型
│   │   ├── user.py
│   │   └── order.py
│   ├── api/               # API路由
│   │   ├── v1/
│   │   │   ├── user.py
│   │   │   └── order.py
│   │   └── deps.py        # 依赖注入
│   ├── services/          # 业务逻辑
│   │   └── user.py
│   └── utils/             # 工具函数
│       └── security.py
├── tests/
├── alembic/               # 数据库迁移
├── requirements.txt
└── .env

路由模板

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.api.deps import get_db, get_current_user
from app.schemas.user import UserCreate, UserResponse
from app.services.user import UserService

router = APIRouter(prefix="/users", tags=["用户"])

@router.post("/", response_model=UserResponse, summary="创建用户")
async def create_user(
    user_in: UserCreate,
    db: Session = Depends(get_db)
):
    """
    创建新用户
    
    - **username**: 用户名,3-20个字符
    - **password**: 密码,至少8位
    - **phone**: 手机号,11位数字
    """
    service = UserService(db)
    user = await service.create(user_in)
    return user

@router.get("/{user_id}", response_model=UserResponse, summary="获取用户信息")
async def get_user(
    user_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """根据用户ID获取用户信息"""
    service = UserService(db)
    user = await service.get_by_id(user_id)
    if not user:
        raise HTTPException(status_code=404, detail="用户不存在")
    return user

错误处理

from fastapi import HTTPException
from fastapi.responses import JSONResponse

class AppException(Exception):
    def __init__(self, code: int, message: str):
        self.code = code
        self.message = message

@app.exception_handler(AppException)
async def app_exception_handler(request, exc):
    return JSONResponse(
        status_code=200,
        content={"code": exc.code, "message": exc.message, "data": None}
    )

# 使用
raise AppException(code=1001, message="用户名已存在")

Read the full file on GitHub · 110 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 9d ago First seen · 110 lines · 13 tokens per session scan A ee3be7e3f98d

Subscribe to this mod's changes

fastapi-best-practices is a skill published in the GitHub repository guyulong/cn-agent-skills (3 stars, last pushed 3mo ago), licensed MIT. It adds 13 tokens to every session and 783 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.