Skip to content

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 ​

  1. Inspect the target project. Record its language, framework, database and ORM, runtime platform, and API consumers before loading implementation rules.
  2. Always start with the framework-independent API references:
  3. 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.
  4. 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.
  5. Load platform or project rules only when the target project uses that context, such as Kubernetes deployment guidance.
  6. 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.
  7. 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 ​

Shared technology: PostgreSQL ​

NestJS/TypeScript implementation ​

You own what ships.