All skills
alffei avatar

/api-expert

@f00cf3b
by Andrew Chenalffei/skill_share68 stars
17

FastAPI 开发最佳实践与约定。适用于编写、审查或排查 FastAPI 应用、REST API 或 Pydantic 模型时。

  • 2 files
  • 2.2 KB
  • Updated 6 months ago
  • GitHub

Use this Skill: https://skilld.dev/gh/alffei/skill_share/api-expert

This session only. Nothing lands on disk.

referencesconventions.md

≈372 tokens on demand. Your agent reads this file only when SKILL.md points to it.

FastAPI Conventions

Route Definitions

  • Use Annotated for all dependency injection: user: Annotated[User, Depends(get_user)]
  • Always specify response_model on routes returning data
  • Use status_code parameter: @app.post("/items", status_code=201)
  • Group related routes with APIRouter and tags

Pydantic Models

  • Always add model_config = ConfigDict(from_attributes=True) for ORM models
  • Use Field() for validation: name: str = Field(min_length=1, max_length=100)
  • Separate request models from response models (never expose internal fields)
  • Use Annotated types for reusable validation: PositiveInt = Annotated[int, Field(gt=0)]

Error Handling

  • Raise HTTPException with specific status codes, never generic 500
  • Use custom exception handlers for domain errors
  • Always include detail with actionable error messages

Async vs Sync

  • Use async def for I/O-bound routes (database, HTTP calls)
  • Use plain def for CPU-bound routes (FastAPI runs them in a thread pool)
  • Never mix async def with blocking calls — use run_in_executor

Dependencies

  • Use Depends() for shared logic (auth, DB sessions, rate limiting)
  • Dependencies can be nested — use this for layered validation
  • Use yield dependencies for cleanup (DB session close, file cleanup)

Security

  • Never store secrets in code — use environment variables
  • Use OAuth2PasswordBearer for token-based auth
  • Always validate and sanitize path/query parameters

Source: SKILL.md on GitHub

No third-party reports yet.

Signed by skilld at f00cf3b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 6 months ago
metadata
{
  "pattern": "tool-wrapper",
  "domain": "fastapi"
}

README badge

README badge for alffei/skill_share/api-expert