Skip to content

Recipes

Practical solutions for common API development tasks using ergo. Each recipe follows a Problem → Solution → Explanation structure with working code examples for both standalone composition and declarative ergo-router configuration.

RecipeProblem
PaginationOffset and cursor pagination with RFC 8288 Link headers
Testing PatternsReliable HTTP testing with ephemeral ports, auth, validation, rate limiting, conditional requests, and graceful shutdown
Custom HeadersAdding response headers from middleware and execute handlers
Error HandlingCustom error responses, extension members, and correlation IDs
File UploadHandling multipart file uploads with size limits and parsed content
Multi-Auth StrategiesPer-route auth overrides, optional auth, and API key authentication
PATCH SemanticsRFC 7386 merge-patch and RFC 6902 JSON Patch with strict enforcement
Sub-RoutersOrganizing routes into sub-routers with per-group middleware and auth
Graceful ShutdownServer lifecycle with database connections and resource cleanup
Debug TracingFinding which middleware rejected a request using pipeline debug mode
Mixed-Auth CORS & CSRFConfiguring CORS and CSRF together for browser and token-based clients
Custom MiddlewareWriting middleware that fits ergo’s accumulator-based return-value contract
Structured LoggingIntegrating pino or winston with ergo’s logger() and graceful() log surfaces
OpenAPI ServingServing the OpenAPI spec and mounting an interactive API explorer
  • Middleware guides document individual middleware — options, return values, error responses, and per-middleware usage examples.
  • Recipes solve cross-cutting problems that involve multiple middleware or patterns that don’t belong to a single middleware page.