FastAPI Async SQLModel Starter
Supercharge your FastAPI development. A backend for perfectionists with deadlines and lovers of asynchronous programming.
🔍 Project Overview
A production-ready FastAPI starter — fully async, with SQLModel, Redis, Celery, and Docker. It uses a Django-inspired folder layout and Clean Architecture: routers handle HTTP, services hold business rules, and repositories talk to the database.
Tests live next to each feature and run in GitHub Actions. Deploy the way that fits you: Compose, PaaS, or Nginx. An optional Locust suite is there for load testing.
It is a solid foundation for API work and a fast path to a POC or MVP — especially if you like how Django is organized but want a clearer split of layers.
🌟 Key Features
A strong foundation for API development, with a practical stack and a clear structure:
- 🏛️ Clean Architecture: Routers handle HTTP, services hold business logic, and repositories manage data access.
- ⚡️ Fully Async: The stack is asynchronous end to end.
- 🚀 FastAPI: High-performance APIs with automatic OpenAPI docs.
- 🧰 SQLModel: One model for the database and the API (SQLAlchemy 2.0 + Pydantic), instead of mapping persistence and transport separately.
- 🔐 JWT User Authentication: Secure user authentication with JSON Web Tokens.
- 🍪 Cookie-based Refresh Token: Refresh tokens stored in cookies.
- 🏬 Easy Redis Caching: Simple, effective caching with Redis.
- 👜 Client-side Caching: HTTP caching headers for faster clients.
- 🚦 Celery: Async task queues, scheduled jobs (Beat), and a PostgreSQL result backend.
- ⚙️ Efficient Querying: Fetch only what you need, including joins.
- ⎘ Pagination Support: Built-in pagination for list endpoints.
- 💌 FastAPI-Mail: Async email with templates, off the request path when needed.
- 🛑 Rate Limiter: Redis-backed limiter for controlled API access.
- 👮 Secure FastAPI Docs: Auth-gated docs, hidden outside development.
- 🚚 Deploy Menu: Compose full-stack (Caddy HTTPS), app-only (PaaS / managed databases), or native (Nginx + Supervisor).
- 🦗 Load Testing with Locust: Optional independent suite for performance and stress tests.
- 🧪 Tests next to the code: HTTP and service tests live with each feature. Unit tests run without Docker; the full suite (and a coverage gate) runs in CI.
- ✅ CI: GitHub Actions checks format, types, and tests on every push.
- 🦾 Easy to extend: Add your own apps without fighting the layout.
🎯 Project Goals
📋 Prerequisites
Before you begin, ensure you have the following:
- Python 3.11 or newer.
- Poetry 1.7.1 for dependency management.
- Docker (Engine or Desktop): used for Compose (infra-only / full-stack) and for Testcontainers when you run the full pytest suite.
PostgreSQL and Redis on the host are optional. Prefer Compose infra-only (or the CLI below) unless you explicitly want a native database install.
Installing Poetry
Poetry is a dependency manager for Python. Follow the steps below to install Poetry version 1.7.1 (required):
- Open a terminal.
pip install poetry==1.7.1
poetry --version must print Poetry (version 1.7.1).
- If the version is anything else:
pip uninstall poetry
pip install poetry==1.7.1
To refresh backend/poetry.lock on this version (does not bump resolved packages):
cd backend
poetry lock --no-update
Do not run poetry lock without --no-update (Poetry 1.7.1 resolves/updates dependencies). Native helper scripts live under development/native/scripts/ — see the Development Guide.
Using Docker Compose (recommended for databases)
From the root directory, start PostgreSQL and Redis:
docker compose --env-file backend/.env -f development/compose/infra-only/docker-compose.yml \
--project-name fastapi-async-sqlmodel-starter up -d
docker compose --project-name fastapi-async-sqlmodel-starter ps
docker compose --project-name fastapi-async-sqlmodel-starter down
The command uses environment variables from backend/.env. Copy backend/.env.example first if needed (local defaults use 127.0.0.1).
For full-stack Docker, native host, or other modes, see the Development Guide.
🤖 Running the Project CLI
From the repository root:
python3 setup.py
Or jump to a submenu: python setup.py local, python setup.py tools, python setup.py locust. python setup.py --help lists the commands.
| Option |
What it does |
| 1 – Local Development |
Copies .env.example if needed, generates SECRET_KEY, starts FastAPI / Celery / Flower. Setup wizard appears only when backend/.env is missing required values (ENTER keeps defaults). |
| 2 – Project Tools |
Alembic (upgrade, revision --autogenerate, current), pytest (unit / integration / full / coverage), Ruff, mypy, and a CI-checks shortcut. |
| 3 – Load Testing (Locust) |
Installs the independent Locust env and starts the Web UI or a headless run. |
| 4 – Exit |
Leave the CLI. |
Production deploy is not in this menu — use the Deployment Guide.
For a manual setup, see the Backend README. For load testing details, see the Locust Guide.
🧪 Running Tests
From backend/:
cd backend
poetry run pytest -m unit -v
poetry run pytest -v --cov --cov-report=term-missing --cov-fail-under=80
poetry run mypy src
pytest -m unit needs no Docker (pre-commit and GitHub Actions job checks). The full suite and the 80% coverage gate need Docker so Testcontainers can start PostgreSQL and Redis.
From the repository root you can run the same commands via Project Tools: python setup.py tools.
Details: Testing Guide.
📚 Documentation
| Guide |
Description |
| Coding Agents |
AGENTS.md and first-party skills for this backend |
| Development Guide |
All local development modes (Docker, native, infra-only) |
| Deployment Guide |
Production modes: Caddy (Compose full-stack), PaaS, or Nginx native |
| Database Migration Guide |
Alembic workflow for schema changes |
| Celery Guide |
Celery worker setup and Windows-specific notes |
| Testing Guide |
Unit tests, HTTP tests, coverage gate, mypy, and CI |
| Rate Limit Guide |
Opt-in Redis limiter, path matching, and TRUST_PROXY_HEADERS |
| Uvicorn Guide |
Local Uvicorn, Compose, and native Gunicorn workers |
| Locust Guide |
Optional load testing with Locust |
🌐 Reference Projects
Source available
This repository is a personal starter shared as a gift. Clone it, fork it, or copy it into your project and adapt it there — that is what the MIT license is for.
It is not a community project. I am not looking for pull requests, feature requests, or a contributor community. If you want to change something, do it in your own copy. Happy coding! 🌟
评论