Appearance
API Best Practices
Use the Engineering Cookbook as the source of truth for API decisions. This guide routes work to the relevant decision and rule set; it does not replace the detailed cookbook documents. Start with the scope index to distinguish generic API rules from technology, implementation, and platform-specific guidance.
Workflow
- Inspect the target project. Record its language, framework, database and ORM, runtime platform, and API consumers before loading implementation rules.
- Always start with the framework-independent API references:
- Load technology guidance for the project's stack. PostgreSQL is the default database: the PostgreSQL rules apply unless a documented exception selects another database. Select and load an ORM profile separately.
- Load implementation rules only for the matching stack. The current NestJS/TypeScript profile includes Nest runtime, DTO, Prisma, RxJS, testing, and observability conventions. Do not apply these conventions to another framework.
- Load platform or project rules only when the target project uses that context, such as Kubernetes deployment guidance.
- Treat decision status as meaningful. Do not turn a proposed recommendation into a mandatory rule, silently override a validated or accepted decision, or hide a project-specific exception.
- Verify at the smallest boundary that proves the change, using the project's actual test and operational tools.
Generic Defaults To Preserve
- Design endpoints around consumer use cases, not persistence tables.
- Keep transport handlers focused on input/output translation; let the application or use-case boundary own business orchestration.
- Keep public response contracts independent of persistence models and expose only fields that are useful and safe for consumers.
- Let the API own aggregation when it represents an API business capability; do not move shared business rules into frontend clients.
- Give documented errors a stable machine-readable code and a safe, human-readable message. Consumers must not branch on message text.
NestJS/TypeScript Defaults
- Keep NestJS controllers thin and put business orchestration in application services.
- Use PostgreSQL as the default database, accessed through Prisma.
- Keep request and response contracts in project-owned DTOs; do not expose Prisma or other persistence entities directly.
- Keep feature behavior separate from Nest application composition, startup, listener, and liveness concerns.
- Use the repository's Observable-based patterns for asynchronous flows only when the target project uses the NestJS/RxJS profile.
- Translate known failures at the NestJS transport boundary and keep unknown failures generic and safe.
When a documented project context requires a different choice, make the deviation explicit and limit it to the affected boundary.
Rule Sets By Scope
Generic API rules
- Business responsibility and boundaries
- Business-oriented API design
- Stable HTTP error contract
- Portable contract sections also exist for REST resource views, OpenAPI contracts, API-owned aggregation, REST design conventions, and cross-resource access. Their NestJS implementations remain stack-specific.
- The pagination contract, testing principles, and scheduled-job principles are reusable where applicable; select tools and platform behavior from the matching profile.
Shared technology: PostgreSQL
- PostgreSQL schema rules apply to the default database. They are reusable across languages and do not prescribe an ORM.
- Database observability applies to every deployed database, whatever the observability provider.
NestJS/TypeScript implementation
- NestJS architecture and runtime and process boundaries
- Prisma access, transactions, and migrations, transaction management, and schema migrations
- API design implementations, error translation and service observability, pagination, scheduled work, and testing
- Reactive programming with RxJS
Platform rules and recommended defaults
- Kubernetes worker capacity applies only to Kubernetes deployments.
- The 80% coverage gate is the recommended default threshold.