- Keep endpoints thin and delegate business logic to services.
- Do not add endpoint-level
try/except; rely on global exception handlers. - Return
APIResponse[T]from API endpoints. - Use query dependencies (
get_pagination,get_sorting,get_search) for list endpoints. - Prefer test-first changes (red -> green -> refactor).
If you are new to this repository, read the "End-to-end walkthrough" section at the bottom of this guide first.
- Create DB model in
app/db/models/. - Create migration (
make migration-create MESSAGE="...") and apply it. - Create schemas in
app/api/schemas/v1/. - Implement service in
app/api/services/. - Implement endpoint module in
app/api/endpoints/v1/. - Register route in
app/api/endpoints/v1/__init__.py. - Add tests in
tests/unit/for service and endpoint behavior first or in parallel. - Update docs in
docs/API.mdanddocs/PROJECT_STRUCTURE.mdif needed.
make test-unit
make autofix
make all-checksBefore opening a PR:
make test
make all-checksFor command details and alternatives, see COMMANDS.md.
- Business errors should raise custom exceptions from
app/api/exceptions.py. - Map errors centrally in
app/core/exception_handlers.py. - Use
dev_codevalues that are stable and machine-readable. - Keep response messages client-friendly and consistent.
- Default DB: SQLite.
- Production DB: PostgreSQL via
DATABASE_URL. - See
docs/DATABASE.mdanddocs/CONFIGURATION.md.
- Endpoint returns
APIResponse[T]and includes stabledev_code. - Service layer contains business logic; endpoint remains orchestration-only.
- Unit tests cover success + main error paths.
make testandmake all-checkspass.- Documentation links and new docs are validated by
make check-docs. - Docs updated when API behavior or project structure changes.
- Writing business logic directly in endpoint handlers.
- Catching broad exceptions in endpoint functions.
- Returning ad-hoc response shapes that bypass envelope helpers.
- Merging schema changes without migration review.
By following the golden workflow above you will:
- run the API locally in the Dev Container,
- understand the service-first architecture,
- implement a feature end-to-end with tests and docs,
- keep quality gates green.
Use envelope-based responses for all API handlers and keep dev_code values stable. This allows frontend/mobile clients to rely on predictable success/error parsing.
cURL:
curl -X POST "http://localhost:8000/api/v1/projects" \
-H "Content-Type: application/json" \
-H "X-Request-ID: demo-dev-guide" \
-d '{"name":"My First Project","description":"Created from cURL"}'TypeScript fetch:
type ApiResponse<T> = {
success: boolean;
dev_code: string;
message: string;
data: T | null;
errors: Array<{ field?: string; message: string }>;
metadata: { request_id: string; timestamp: string };
};
type Project = { id: number; name: string; description: string | null };
const response = await fetch("http://localhost:8000/api/v1/projects");
const body = (await response.json()) as ApiResponse<Project[]>;
if (!body.success) throw new Error(`${body.dev_code}: ${body.message}`);
console.log(body.data);API.mdfor response envelope and endpoint contract details.TESTING.mdfor fixture and assertion patterns.COMMANDS.mdfor all availablemaketargets.