BackendIntermediate14 min
API routes that fail well
Validate input, return boring status codes, and never leak stack traces to clients.
By Sam Okonkwo · 2026-04-18
Contract first
Decide the JSON shape and status codes before writing handlers. Document invalid vs missing vs server failure.
Validation
- Reject unknown fields when it matters
- Normalize email and trim names
- Escape anything that lands in HTML email
Ops without drama
Log details server-side. Clients get short, safe messages. Missing env should not crash next build.