API Design
API strategy and conventions
API Design
Strategy
- REST-first - All APIs follow REST conventions
- No GraphQL - Keep it simple
- JSON only - Request and response bodies
- Versioned -
/api/v1/prefix for all endpoints
URL Patterns
| Operation | Method | URL Pattern |
|---|---|---|
| List | GET | /api/v1/{resource} |
| Get one | GET | /api/v1/{resource}/:id |
| Create | POST | /api/v1/{resource} |
| Update | PATCH | /api/v1/{resource}/:id |
| Delete | DELETE | /api/v1/{resource}/:id |
Response Format
All responses follow consistent structure:
// Success
{ "data": { ... }, "meta": { ... }, "error": null }
// Error
{ "data": null, "error": { "code": "ERROR_CODE", "message": "..." } }Status Codes
| Code | Usage |
|---|---|
| 200 | Successful GET, PATCH |
| 201 | Successful POST (created) |
| 204 | Successful DELETE |
| 400 | Validation error |
| 401 | Authentication required |
| 403 | Permission denied |
| 404 | Not found |
| 422 | Business logic error |
| 500 | Server error |
Authentication
- MVP: Auth.js database sessions (Google SSO via Next.js)
- API: Tenant context passed via
x-tenant-idheader from authenticated Next.js frontend - All API endpoints require TenantGuard validation
Authentication Flow (MVP)
Authentication is handled by Auth.js in Next.js, not by the NestJS API directly:
- User clicks "Sign in with Google" on Next.js frontend
- Auth.js handles OAuth flow, creates user/tenant via
createUserevent - Session stored in PostgreSQL via PrismaAdapter
- Frontend passes
x-tenant-idandx-system-roleheaders to API - NestJS TenantGuard and SystemRoleGuard validate requests
API Headers (from authenticated frontend)
// Headers sent by Next.js frontend to NestJS API
{
'x-tenant-id': string // From session.user.tenantId
'x-system-role': string // SYSTEM_ADMIN | HR_ADMIN | MANAGER | EMPLOYEE
}Tenant Endpoint
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/tenant | Get current tenant info (requires TenantGuard) |
Note: User registration/login is handled by Auth.js in Next.js, not the NestJS API. See Phase 01 for details.
Query Parameters
- Pagination:
?page=1&limit=20 - Filtering:
?status=active&departmentId=... - Sorting:
?sort=-createdAt(prefix-for descending) - Search:
?search=john
Key Rules
- Tenant isolation - All endpoints scoped to tenant from
x-tenant-idheader - Consistent naming - Plural nouns for resources (
/employees, not/employee) - Validation - All inputs validated with DTOs
- Error details - Return field-level validation errors
See Phase 01 for auth implementation details