zhanymkanov/fastapi-best-practices

Public

FastAPI Best Practices and Conventions we used at our startup

  • Stars18.3k+42 tuần này
  • Forks1.3k
  • Watchers171
  • Issues14
  • Ngày tạo09/08/2022
  • Cập nhật gần nhất27/08/2026
  • Ngôn ngữ—
  • Giấy phép—
README.md82.3 KBRaw

FastAPI Best Practices

Opinionated list of best practices and conventions we use at our startups.

After several years of building production systems, we've made both good and bad decisions that significantly impacted our developer experience. Here are some lessons worth sharing.

Working with an AI agent? See AGENTS.md for the same rules in a terse, machine-readable format with a version matrix, Do/Don't blocks, and an anti-patterns checklist.

简体中文

Contents

Project Structure

There are many ways to structure a project, but the best structure is one that is consistent, straightforward, and free of surprises.

Many example projects and tutorials organize projects by file type (e.g., crud, routers, models), which works well for microservices or smaller projects. However, this approach didn't scale well for our monolith with many domains and modules.

The structure I found more scalable and evolvable is inspired by Netflix's Dispatch, with some minor modifications.

fastapi-project
├── alembic/
├── src
│   ├── auth
│   │   ├── router.py
│   │   ├── schemas.py  # pydantic models
│   │   ├── models.py  # db models
│   │   ├── dependencies.py
│   │   ├── config.py  # local configs
│   │   ├── constants.py
│   │   ├── exceptions.py
│   │   ├── service.py
│   │   └── utils.py
│   ├── aws
│   │   ├── client.py  # client model for external service communication
│   │   ├── schemas.py
│   │   ├── config.py
│   │   ├── constants.py
│   │   ├── exceptions.py
│   │   └── utils.py
│   ├── posts
│   │   ├── router.py
│   │   ├── schemas.py
│   │   ├── models.py
│   │   ├── dependencies.py
│   │   ├── constants.py
│   │   ├── exceptions.py
│   │   ├── service.py
│   │   └── utils.py
│   ├── config.py  # global configs
│   ├── models.py  # global models
│   ├── exceptions.py  # global exceptions
│   ├── pagination.py  # global module e.g. pagination
│   ├── database.py  # db connection related stuff
│   └── main.py
├── tests/
│   ├── auth
│   ├── aws
│   └── posts
├── templates/
│   └── index.html
├── requirements
│   ├── base.txt
│   ├── dev.txt
│   └── prod.txt
├── .env
├── .gitignore
├── logging.ini
└── alembic.ini

Nội dung README thuộc về zhanymkanov và cộng đồng đóng góp, giấy phép Không có giấy phép. Nguồn: github.com/zhanymkanov/fastapi-best-practices