How To: Add an API Endpoint¶
Steps¶
- Route in
backend/app/routes/-- define the HTTP handler - Schema in
backend/app/schemas/-- Pydantic request/response models - Service in
backend/app/services/-- business logic (if non-trivial) - Register the router in
backend/app/main.py - Tests in
backend/tests/api/ - Run tests:
make test-backend - Sync types:
npm run types:generate(backend must be running) - Verify frontend:
npm run typecheck
Critical Patterns¶
Always use dependency injection for database sessions:
Always normalize plate registrations:
from app.utils.registration import normalize_registration
reg = normalize_registration(user_input) # Uppercases, strips spaces/dashes
Auth dependencies:
from app.utils.auth import get_current_user, require_admin
@router.get("/protected")
def protected(user=Depends(get_current_user)):
...
@router.get("/admin-only")
def admin_only(user=Depends(require_admin)):
...
Always set response_model so the endpoint appears correctly in OpenAPI and TypeScript types are generated.
After Adding¶
| What you changed | Run this |
|---|---|
| Route or schema | make test-backend + npm run types:generate |
| Model (database) | make test-backend + make test-backend-pg + create migration |
| Frontend consuming new types | npm run typecheck + npm run build |