Fastapi Prod Ready
ASecurityBuild and harden FastAPI + PostgreSQL + SQLAlchemy 2.0 (async) + Alembic backends from single-user prototype to multi-user production readiness — event-loop safety, Read-Modify-Write concurrency locks, idempotency, async session & pool hygiene, zero-downtime PostgreSQL migrations, API security (response_model, IDOR, CORS, safe errors, timeouts), and container/health ops. Preserves existing architecture without rewrites. For read-only audits use backend-review, concurrency-check, or migration-...
- 2 stars
- 0 votes
- 0 copies
- 0 views
- Added November 29, 2026
Works with
Security analysis
100/100Pro scans all 20 files and shows the line behind each finding
npx -y skills add tuvatechvalley/fastapi-prod-ready --skill fastapi-prod-ready --agent claude-codeAre you the author of Fastapi Prod Ready?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/tuvatechvalley-fastapi-prod-ready)---
name: fastapi-prod-ready
description: Build and harden FastAPI + PostgreSQL + SQLAlchemy 2.0 (async) + Alembic backends from single-user prototype to multi-user production readiness — event-loop safety, Read-Modify-Write concurrency locks, idempotency, async session & pool hygiene, zero-downtime PostgreSQL migrations, API security (response_model, IDOR, CORS, safe errors, timeouts), and container/health ops. Preserves existing architecture without rewrites. For read-only audits use backend-review, concurrency-check, or migration-review. Always ends with BACKEND_REPORT.md.
---
# FastAPI production-ready
Goal: take a FastAPI + PostgreSQL + SQLAlchemy 2.0 (async) + Alembic backend from single-user prototype to real multi-user concurrency (2–20+ simultaneous users), zero-downtime migrations, and production security. Every task ends with `BACKEND_REPORT.md`.
Talk to the user in their language. Write reports in English.
## Reference guides (load on demand)
`<skill>` = the directory containing this `SKILL.md`. Load only the guides relevant to your current step:
| Guide | Read when |
|---|---|
| `EXISTING-PROJECTS.md` | hardening an existing FastAPI project without rewriting routers, DI, or folder layout |
| `CONCURRENCY-AND-ASYNC.md` | fixing event-loop blocks, Read-Modify-Write races, idempotency, or multi-worker global state |
| `SQLALCHEMY-AND-POSTGRES.md` | fixing `async_sessionmaker`, `lazy="raise"`, `BackgroundTasks` sessions, transaction scope, pool tuning, or FK indexes |
| `MIGRATIONS-ZERO-DOWNTIME.md` | writing or hardening Alembic / SQL migrations (`CONCURRENTLY`, expand-and-contract, `lock_timeout`, `downgrade()`) |
| `SECURITY-AND-OPS.md` | hardening `response_model`, IDOR ownership filters, CORS, exception handlers, timeouts, redacting logs, non-root `Dockerfile`, `/healthz` & `/readyz` |
| `AUDIT-AND-REPORTING.md` | running AST audits, live probes, manual verification, and formatting reports |
## Toolkit scripts (`<skill>/scripts/`, Python 3.11+ stdlib only)
| Script | Purpose |
|---|---|
| `install.py <project> [--starter]` | installs `tools/fastapi_prod_ready/` + `.gitignore` rules; `--starter` scaffolds a clean production starter into a fresh folder |
| `verify.py [project] [--url <endpoint>]` | runs optional `ruff`/`pytest` + `check_forbidden.py` + `backend_report.py` (+ optional live probe) |
| `backend_report.py <project>` | unified AST + migration + forbidden scan -> `BACKEND_REPORT.md`, `findings.json`, `MIGRATION_REPORT.md` |
| `ast_audit.py <project>` | AST checks for event loop, RMW races, SQLAlchemy 2.0 async, API security & ops |
| `migration_audit.py <project>` | Alembic & SQL zero-downtime lock/data-loss audit + unindexed Foreign Keys -> `MIGRATION_REPORT.md` |
| `concurrency_probe.py [--project P] [--url U]` | AST concurrency scan + live `asyncio` parallel burst probe (`2/10/20`, `1 vs N` stall ratio, double-click idempotency) -> `CONCURRENCY_REPORT.md` |
| `check_forbidden.py <project>` | blocks on committed `.env`/keys, hardcoded secrets, `verify=False`, raw SQL interpolation; warns on `print()`, PII logs, empty `except: pass` |
## Workflows
### A. New FastAPI backend
1. Confirm project name, Python version (≥ 3.11), and PostgreSQL async driver (`asyncpg`).
2. Run `python <skill>/scripts/install.py <project_dir> --starter`.
3. Build requested domain models, Alembic migrations, routers, and tests following `templates/starter_app/` conventions (`expire_on_commit=False`, `lazy="raise"`, indexed FKs, `response_model`, `with_for_update()` / atomic updates on mutable counters).
4. Run **Finish**.
### B. Harden an existing FastAPI backend
1. Read `EXISTING-PROJECTS.md`. **Preserve existing folder structure, routers, DI style, and ORM conventions** — never rewrite working modules or force a new framework.
2. Run `python <skill>/scripts/install.py .` (never pass `--starter` on existing code).
3. Run `python tools/fastapi_prod_ready/verify.py .` and inspect `BACKEND_REPORT.md`.
4. Fix blockers in priority order: **data corruption / concurrency races -> event-loop blocks -> SQLAlchemy session/pool bugs -> migration locks -> API security & ops -> warnings**.
5. Re-run `verify.py` (and `concurrency_probe.py --url` if the app runs locally) until all ❌ blockers are resolved or explicitly accepted by the user, then run **Finish**.
### C. Read-only audits
Delegate to sibling skills:
- General pre-production review -> **backend-review**
- Multi-user concurrency proof & async race tests -> **concurrency-check**
- Alembic / SQL lock & rollback review -> **migration-review**
## Non-negotiable rules
- **Concurrency (`CONCURRENCY-AND-ASYNC.md`):** Never run blocking I/O or heavy CPU inside `async def`. Never do `SELECT -> if -> UPDATE` on balances, stock, bookings, or coupons without `.with_for_update()`, atomic `UPDATE ... WHERE`, or DB `UNIQUE`/`CHECK` + `IntegrityError`. Never keep rate-limit counters, sessions, revoked tokens or OTP/nonce state in module-level globals (each worker sees its own copy); a per-process cache is fine.
- **SQLAlchemy 2.0 Async (`SQLALCHEMY-AND-POSTGRES.md`):** Always set `expire_on_commit=False` on `async_sessionmaker`. Set `lazy="raise"` (or explicit async-safe loading) on relationships. Never pass `Depends(get_db)` into `BackgroundTasks.add_task`. Never make outbound HTTP calls inside `async with db.begin():`. Configure `pool_pre_ping=True`, `pool_size`, `max_overflow`, and `statement_timeout`. Index every Foreign Key column.
- **Migrations (`MIGRATIONS-ZERO-DOWNTIME.md`):** Use `postgresql_concurrently=True` inside `autocommit_block()` for indexes on existing tables. Never add `nullable=False` without a safe `server_default` or expand-and-contract. Always set `lock_timeout` and `statement_timeout`. Never ship an empty `downgrade()`.
- **Security & Ops (`SECURITY-AND-OPS.md`):** Declare `response_model` (or Pydantic return type) on every data endpoint. Authenticate and scope every `/{id}` route to the caller's ownership/tenant. Never combine `allow_origins=["*"]` with `allow_credentials=True`. Never leak `str(e)` or tracebacks to clients. Set explicit `timeout` on every HTTP call. Redact secrets/tokens/PII in logs; no committed `.env`; non-root `Dockerfile`; separate `/healthz` and DB-checked `/readyz`.
## Finish (mandatory)
1. Run `python tools/fastapi_prod_ready/verify.py .` (or `python <skill>/scripts/backend_report.py .` if not installed). Never claim skipped tests or unrun probes passed.
2. Deliver `reports/fastapi-prod-ready/BACKEND_REPORT.md` (plus `MIGRATION_REPORT.md` / `CONCURRENCY_REPORT.md` when applicable).
3. Reply in the user's language in 3–6 lines: verdict, remaining blockers, fixes applied, and the single next step.
Files in this skill
- AUDIT-AND-REPORTING.md
- CONCURRENCY-AND-ASYNC.md
- EXISTING-PROJECTS.md
- MIGRATIONS-ZERO-DOWNTIME.md
- SECURITY-AND-OPS.md
- SKILL.md
- SQLALCHEMY-AND-POSTGRES.md
- scripts/ast_audit.py
- scripts/backend_report.py
- scripts/check_forbidden.py
- scripts/concurrency_probe.py
- scripts/install.py
- scripts/migration_audit.py
- scripts/verify.py
- templates/starter_app/.env.example
- templates/starter_app/.gitignore
- templates/starter_app/AGENTS.md
- templates/starter_app/CLAUDE.md
- templates/starter_app/Dockerfile
- templates/starter_app/alembic.ini
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.