Fastapi Foundation
ASecurityBuild the foundation of a new professional FastAPI backend from zero - hexagonal modular-monolith layout (modules/platform/shared/entrypoints), typed settings with production fail-fast checks, startup preconditions and essential indexes, composition root, infrastructure clients (MongoDB, Redis, MinIO), Docker/compose/Nginx, import-linter architecture contracts, make check, CI. Use whenever the user says new project, start a backend, project skeleton, structure, boilerplate, "stage 0", Docker/...
- 2 stars
- 0 votes
- 0 copies
- 0 views
- Added November 27, 2026
Works with
Security analysis
100/100Pro scans all 4 files and shows the line behind each finding
npx -y skills add amralhorimy/fastapi-pro-skills --skill fastapi-foundation --agent claude-codeAre you the author of Fastapi Foundation?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/amralhorimy-fastapi-foundation)---
name: fastapi-foundation
description: Build the foundation of a new professional FastAPI backend from zero - hexagonal modular-monolith layout (modules/platform/shared/entrypoints), typed settings with production fail-fast checks, startup preconditions and essential indexes, composition root, infrastructure clients (MongoDB, Redis, MinIO), Docker/compose/Nginx, import-linter architecture contracts, make check, CI. Use whenever the user says new project, start a backend, project skeleton, structure, boilerplate, "stage 0", Docker/Nginx for FastAPI, or wants to restructure a project's base architecture.
---
# FastAPI Foundation (Stage 0)
Goal: a project that runs, refuses to start unsafely, enforces its own architecture, and is observable -
*before* the first feature. Features then arrive as modules (`fastapi-add-module`).
First pick the profile (`fastapi-pro-workflow` section 1). **Profile A** below; **Profile B** is in
`references/lite-profile.md`.
## Procedure (Profile A)
1. **Clarify** only what changes the design: purpose, infra (Mongo/Redis/MinIO/Firebase/...), auth provider,
workers now or later, doc languages. Write the first ADR: modular monolith + hexagonal (template in
`fastapi-docs-release`).
2. **Create the tree** from `references/layout.md` - the Python package is `src/app/`; create all folders on day one, with `__init__.py`.
3. **Architecture enforcement first**, before any feature: `pyproject.toml` import-linter contracts +
`tests/architecture/test_layers.py` + `make check`. Templates in `references/templates.md`. A rule that is not
tested will be broken within a month.
4. **Settings** (`platform/settings.py`): typed, env-driven, one `get_settings()`; `production_problems()` returns
every unacceptable production setting (debug on, placeholder or short secrets, missing tokens) and the
app **refuses to start** with a `StartupError`.
5. **Platform clients** with a `<tech>_guard` and `errors.py` each (`platform/mongo|redis|storage|auth`).
6. **Declarative indexes**: each module has `adapters/persistence/indexes.py` listing `IndexSpec`s;
`essential=True` for anything correctness depends on (uniqueness) -> startup fails if it cannot be built;
performance indexes only warn.
7. **Error + logging core** (`fastapi-errors-logging`): taxonomy, classifier, handlers, correlation id, JSON logs.
8. **Composition root** `entrypoints/api.py`: `create_app()` factory, lifespan with `AsyncExitStack`
(reverse-order cleanup even if a later step fails), builds long-lived resources onto `app.state`,
mounts every module router. Workers get their own entrypoints (`entrypoints/worker.py`, ...).
9. **Health**: `/healthz` (alive) and `/readyz` (dependencies) - see `fastapi-observability`.
10. **Containers**: one root `Dockerfile`, several commands (api, worker, ...); `docker-compose.yml` is for
local development only, with local ignored `.env` secrets; do not present it as production deployment. Production
requires TLS at a trusted edge and forwarded headers trusted only from explicitly configured proxy addresses.
Templates in `references/templates.md`.
11. **Prove it**: `make up`, `/readyz` green, stop Redis -> readiness fails, `make check` green. Show output.
12. **Hand over**: README run section + next three stages.
## Decisions to make explicitly
| Decision | Default | Change when |
|---|---|---|
| Python | 3.12+ | team constrained |
| Layout | `src/<pkg>/` + absolute imports (ruff `TID` bans relative imports) | - |
| Mongo driver | `pymongo` async API (`AsyncMongoClient`, `pymongo.asynchronous.collection.AsyncCollection`) | Motor already used (deprecated) |
| Mongo topology | single-node **replica set** if multi-document transactions are needed | no transactions needed |
| Redis | `redis.asyncio`; AOF on; `noeviction` if `maxmemory` set (queues live there) | - |
| Object storage | MinIO, client uploads via presigned URL, storage webhook confirms | tiny private files |
| Type checker | pyright (the sole type checker in this profile) | - |
| IDs | ObjectId strings or `uuid4().hex` via an injected `IdGenerator` | - |
| Time | tz-aware UTC via an injected `Clock` | - |
| Errors | `DomainError` taxonomy, HTTP status decided only in `platform` | - |
## Rules from day one
- `main`/entrypoint files only **compose**; no business logic.
- Clock, id generator, event emitter are **injected** (testable, deterministic).
- No global clients created at import time; everything lives on `app.state` or is passed in.
- The sample Compose Mongo service is standalone development infrastructure, not a transaction-ready replica set. If a feature
needs multi-document transactions, add and test a replica-set deployment profile, including authentication and initialization.
- Secrets live outside Git (`secrets/`, local `.env`); copy `.env.example` to `.env`, replace every placeholder with
locally generated values, and never commit `.env`. `production_problems` rejects placeholders.
- Debug routes exist only when `DEBUG_MODE=true`, and production refuses to start with it on.
- Docs are part of the foundation: `ARCHITECTURE.md`, `docs/adr/`, `docs/errors.md`, `docs/modules/`.
## Definition of done for Stage 0
- [ ] `make check` (lint + pyright + import-linter + tests) green; a deliberately bad import fails it
- [ ] Local-development `docker compose up` healthy; Nginx is the only published port; production TLS/proxy trust is configured separately
- [ ] Production-mode start with placeholder secrets **fails** with a clear message
- [ ] Forced error returns the standard problem body and one safe classified log line with the same request id; any deeper
diagnostics use a separately tested redaction and access-control policy
- [ ] README: run + test in under 10 lines
Files in this skill
- SKILL.md
- references/layout.md
- references/lite-profile.md
- references/templates.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.