Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Project structure · Backend

FastAPI project structure

Opens with credits

A typed Python API for an orders app, built the way the FastAPI docs and the official full stack template lay it out: one app package, a router per feature, Pydantic schemas at the edges, services in the middle and SQLAlchemy models with Alembic migrations underneath. For students who have finished the FastAPI tutorial and want to know where the second, tenth and fiftieth file go.

Curated by Tauseef FayyazIntermediate188 files and foldersPython 3.14, FastAPI 0.142, Pydantic 2.13Checked October 2026
Explore the filesPick any file to see what it is, why it exists and when you add it.

How it grows

Each stage opens the explorer there

  1. 1Day 1What the scaffold and your first hour give you12 files and folders
  2. 2GrowingWhat a real app with features and tests adds+91 files and folders
  3. ProductionWhat running it for real users adds+42 files and foldersOpens with credits
  4. ExpertWhat a team needs to keep it healthy for years+43 files and foldersOpens with credits

How it is organised

  • One `app` package holds all the code, so imports look the same in the dev server, the tests and the container.
  • Split by layer, one file per feature in each layer: `api/routes/orders.py`, `schemas/order.py`, `services/orders.py`, `db/models/order.py`. This is the layout the FastAPI docs and the official template use. Past roughly ten features, many teams switch to one folder per feature (`orders/router.py`, `orders/schemas.py`); both are fine, mixing them is not.
  • Routes stay thin: they parse input, call a service and return a schema. Business rules live in services, which know nothing about HTTP.
  • Pydantic schemas are the API contract; SQLAlchemy models are the database shape. Keep them separate even when they look alike today.
  • Every setting comes from the environment through one `Settings` class. Nothing secret is written in code or committed.
  • Shared things (database session, current user, settings) arrive through `Depends`, so tests can swap them with `app.dependency_overrides`.
  • Queries live in repositories, rules and transactions in services. Services are tested fast with factories; repositories and migrations are tested against a real PostgreSQL in a container.
  • Slow or retryable work (emails, webhooks, reports) leaves the request through a Celery task that receives ids, never ORM objects.
  • Tests grow in layers: fast unit tests at Growing, API tests on every push, integration tests on real PostgreSQL at Production, contract, load and mutation tests at Expert.
  • Every tool reads its settings from pyproject.toml where it can, and the Makefile, pre-commit and CI call the same commands, so "it passed locally" means it passes in CI.

At the top of the project

Open one in the explorer for the full story

Learn more on Sefism

Roadmaps, cheat sheets and projects that go with FastAPI.

See 7 more
Checked against 23 official sources, October 2026. If something has changed, the docs win.Show sources

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com