FastAPI Project Structure: What a Production-Ready Python API Needs in 2026

FastAPI Project Structure: What a Production-Ready Python API Needs in 2026

FastAPI is easy to start with. A single main.py with a few routes runs in seconds, and that is exactly why so many Python APIs end up as a tangled single file with no tests, no linting, and a Dockerfile copied from a blog post that no one can explain.

The gap is not the framework. It is the structure around it: where code lives, how it is tested, how it is built, and how a new developer on the team knows what to do next. Here is what a production-ready FastAPI project needs in 2026, and a practical way to get it without spending a day wiring it up by hand.

The core problem: FastAPI projects are usually just a single file

A typical early FastAPI project is main.py with routes, models, and database calls all in one place. It works. It is also exactly the shape of a project that gets rewritten six months later.

The fix is not more files for the sake of more files. It is separating the things that change at different speeds: application setup, business logic, data access, and configuration. Once those have clear homes, everything else (tests, CI, deployment) has somewhere to attach.

The directory layout that holds up

A production-ready FastAPI project does not need a framework-specific pattern. It needs clear boundaries:

  • app/main.py — application entry point and router registration
  • app/api/ — route handlers, grouped by domain
  • app/core/ — configuration, security, and shared infrastructure
  • app/models/ — database models or schemas
  • app/services/ — business logic that routes should not contain
  • tests/ — test suite that mirrors the app/ layout
  • .env.example — documented environment variables, committed
  • Makefile — common commands so nobody has to remember them

The exact names matter less than the rule: a new developer should be able to open the repo, read .env.example, run make dev, and have a working local environment without asking questions.

What every FastAPI project needs before it goes to production

Tests that run in CI, not just on your laptop

pytest with httpx for the ASGI transport is the standard FastAPI testing setup. The important part is that the suite runs in CI on every push, not that it exists locally. A test suite that only passes on the author’s machine is a liability.

Linting and formatting enforced automatically

ruff has become the default for Python in 2026: it is fast, it replaces several tools at once, and it is easy to enforce in CI. Adding it as a pre-configured preset saves the hour-long argument about which linter the team should use.

A multi-stage Dockerfile

A production image should build and run in separate stages: dependencies in one layer, application code in another, and a final image that does not carry the build toolchain. Combined with docker-compose.yml for the local stack, this makes the environment reproducible on any machine.

CI/CD that runs on every push

GitHub Actions is the default for most teams: lint, test, build, and deploy on every push. The pipeline does not need to be clever. It needs to exist, be green, and fail loudly when something breaks.

Environment configuration with sane defaults

.env.example documents what the application needs. .env.development and .env.production separate local and production settings. Secrets never go in the repository; the example file shows the keys, and the real values live in the deployment environment.

Why teams end up with a boilerplate problem instead

None of this is hard individually. The friction is that setting it up takes an afternoon every time a new project starts: write the Dockerfile, add the GitHub Actions workflow, configure ruff, wire up pytest, create the env files, write the Makefile. Multiply that by every microservice and every new repo, and the team has spent a full week on boilerplate that is identical across projects.

That is the gap a scaffolder fills. Instead of copying a project directory from a template that is three years old and undocumented, you generate a current, working structure in under a second and spend the saved time on the actual application.

A FastAPI starter that ships the whole setup

ScaffoldKit generates production-ready project directories for Python (FastAPI), Node.js (Express), and Go in under one second. The FastAPI stack includes everything discussed above, pre-configured and ready to run:

  • Multi-stage Dockerfile and docker-compose.yml with frontend, backend, and database
  • GitHub Actions CI/CD for lint, test, build, and deploy on every push
  • pytest testing setup
  • ruff linting preset
  • .env.example, .env.development, .env.production
  • A Makefile with common commands (test, lint, dev, build)
  • .editorconfig, .gitignore, and a README template

Usage is three commands:

npm install -g @ezralabs/scaffoldkit
scaffoldkit init my-project --stack=fastapi
cd my-project
make dev

It runs on Linux, macOS, and Windows (WSL), and it is a one-time purchase with free updates. At $39 USD, the alternative is an afternoon of copy-pasting per project, forever.

Structure is a decision you make once

A production-ready FastAPI project is not about the perfect folder tree. It is about making the right decisions once and having every future project inherit them: tests that run in CI, linting that is enforced, a build that is reproducible, and a README that tells the next developer exactly what to do. ScaffoldKit encodes those decisions so the next project starts at the interesting part. If you are also tired of hand-writing environment setup, DevKit automates the machine-level configuration, or get both in the ScaffoldKit + DevKit Bundle. Explore everything at the Ezra Labs shop.

Similar Posts