AgentSkillsCN

develop-fastapi-app

以最佳实践构建生产就绪的 FastAPI 应用程序,涵盖项目结构、异步模式、Pydantic 模型、依赖注入、数据库操作,以及测试流程。当您开发 FastAPI 后端、REST API,或 Python 异步 Web 服务时,可选用此功能。

SKILL.md
--- frontmatter
name: develop-fastapi-app
description: Build production-ready FastAPI applications with best practices for project structure, async patterns, Pydantic models, dependency injection, database operations, and testing. Use when developing FastAPI backends, REST APIs, or Python async web services.
license: Apache-2.0
metadata:
  author: zhanymkanov, xiaoland
  version: "1.0"

Developing a FastAPI Application

When to use this skill

Use this skill when:

  • Starting a new FastAPI project
  • Refactoring existing FastAPI applications
  • Implementing REST APIs with Python
  • Working with async/await patterns in web services
  • Setting up database models and migrations
  • Writing tests for FastAPI endpoints
  • Configuring dependency injection patterns
  • Optimizing API performance and code quality

Project Structure

Organize projects by domain rather than file type for better scalability:

Example
fastapi-project/
├── app/
│   ├── schemas/                 # data structure and constraints
│   ├── models/                  # schema with behavior
│   ├── managers/                # domain orchestrator
│   ├── exceptions/              # business exception
│   ├── routes/                  # domain orchestrator
│   ├── settings.py              # application global settings
│   ├── engine.py                # database engine
├── utils/
├── libs/
├── tests/
│   ├── conftest.py
│   ├── <by_domain>/test_<xxx>.py
├── migrations/
├── scripts/
├── docs/
├── .env
├── .env.example
├── logging.ini
├── pyproject.toml
└── run.py                       # FastAPI app

File Templates:

Notes:

  • Store all domain modules inside app/ folder
  • Each subfolder (schemas/, models/, managers/, routes/) is organized by domain
    • e.g., app/schemas/posts.py, app/managers/posts_manager.py, app/routes/posts.py
  • Import from other packages with explicit module names

References

Read revelant references before you really start your task:

General Best Practices

  • Use Ruff for fast linting and formatting:
bash
ruff check --fix src
ruff format src