develop-fastapi-app

xiaoland 的头像作者 xiaoland

使用最佳实践构建生产就绪的FastAPI应用程序,包括项目结构、异步模式、Pydantic模型、依赖注入、数据库操作和测试。用于开发FastAPI后端、REST API或Python异步Web服务。

📖 中文导读:使用最佳实践构建生产就绪的FastAPI应用程序,包括项目结构、异步模式、Pydantic模型、依赖注入、数据库操作和测试。用于开发FastAPI后端、REST API或Python异步Web服务。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

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:

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:
ruff check --fix src
ruff format src