Include Loyalty/Communication/Sports Center backends and registry updates alongside production nginx and compose wiring. Co-authored-by: Cursor <cursoragent@cursor.com>
2.8 KiB
2.8 KiB
API Template
REST / DTO / validation / versioning / error standards for service APIs.
Short contract form: templates/api-template.md.
REST Standards
| Rule | Detail |
|---|---|
| Prefix | /api/v1 for public service APIs |
| Resources | Nouns; plural collections (/programs, /leads) |
| Methods | GET read · POST create/actions · PATCH/PUT update · DELETE remove/archive as designed |
| Auth | JWT (and documented internal tokens); deps in api/deps.py |
| Tenant | X-Tenant-ID (and documented alternatives); reject when missing on tenant routes |
| Pagination | Shared page meta pattern |
| Thin routers | Validate → authorize → service call → map response |
Health endpoints may live at /health (and /capabilities when applicable) outside /api/v1.
DTO Rules
- Pydantic schemas in
app/schemas/—Create,Update,Read,Listsuffixes. - Never expose password hashes, provider secrets, or internal tokens.
- Do not return ORM models directly from routers.
- External refs are explicit fields (
crm_contact_ref, etc.). - Document breaking field changes in phase handover.
Validation Rules
- Request body/query validated by Pydantic.
- Domain rules enforced in services/validators — not only at the edge.
tenant_idin body does not override resolved tenant context.- Path IDs are UUIDs/strings as per service; always combined with tenant scope server-side.
Versioning
- Current public version:
v1. - Additive changes preferred within
v1. - Breaking changes require a new version path (
/api/v2) or a documented migration window in the phase. - Deprecated fields remain until the compatibility gate allows removal.
Error Responses
- Use shared exception types / stable
error_codevalues. - Map to appropriate HTTP status: 400 validation · 401 auth · 403 forbidden · 404 not found · 409 conflict · 422 domain · 429 rate limit · 500 unexpected.
- Do not leak stack traces or secrets in production payloads.
- Document new error codes in the phase API section.
Endpoint Documentation Block
| Method | Path | Description | Auth / Permission |
| --- | --- | --- | --- |
| POST | /api/v1/example | Create example | JWT + `service.example.create` |
Also update api-reference.md / services-contracts.md when the phase publishes stable contracts.