ADR-018: Public API Contract — Versioning, Error Model & Rate-Limit Semantics
Context
The backend API on Cloud Run is consumed cross-origin by the SPA and is publicly reachable, yet its contract existed only implicitly in server code:
1. No machine-readable contract — endpoint shapes, App Check security, and SSE streaming semantics were discoverable only by reading source.
2. Ad-hoc error envelopes — every failure returned a bespoke { "error" } body, indistinguishable from success payloads by media type and carrying no stable problem classification.
3. Undocumented rate-limit semantics and no stated compatibility policy for consumers.
Decision
Adopt a formal, published API contract with four pillars:
1. OpenAPI 3.1 as the source of truth, served at /api/openapi.json with a human-readable reference at /api/docs rendered by a self-hosted, version-pinned Scalar bundle — fully same-origin, so it works under the strict site-wide CSP with no third-party script execution.
2. RFC 9457 Problem Details (application/problem+json) for every error, with a stable type URI per problem class (validation-error, unauthorized, rate-limited, etc.); unknown /api routes and malformed JSON bodies honor the same contract.
3. IETF draft-7 RateLimit / RateLimit-Policy headers so clients can self-throttle, with Retry-After on 429s.
4. An explicit compatibility policy embedded in the contract: additive-only changes within a major version; breaking changes ship under a new versioned path with Deprecation + Sunset headers and a minimum 90-day overlap.
Amended 2026-10-16: 429 policies differ by route (per IP per 15 minutes: AI, certificates and Terraform 30, CV 5, contact 5, share 60, telemetry 100), plus a site-wide daily AI cap whose 429 carries Retry-After until midnight UTC. Every /api/* 429 (including the telemetry limiter) returns an RFC 9457 rate-limited problem document.
Amended 2026-11-01: request body schemas are derived directly from the runtime Zod route validators via zodToOpenApiSchema (z.toJSONSchema), eliminating manual schema duplication and drift without requiring a framework migration.
Consequences
Benefits:
* The API is self-describing — any consumer can discover the full surface, auth, and error semantics from /api/docs without reading server code.
* Uniform, machine-readable errors: the SPA branches on one shape everywhere, and new endpoints inherit the contract by construction.
* Backend tests pin the contract (problem media type, problem fields, spec availability), turning accidental breaking changes into test failures.
Trade-offs:
* The OpenAPI document is hand-maintained; drift must be guarded by contract tests rather than code generation.
* Migrating to problem+json required updating every frontend error-parsing path.
Alternatives considered
1. GraphQL: Rejected — the API is a small set of task-shaped RPC endpoints with streaming; a schema/resolver layer adds weight without solving a real over-/under-fetching problem.
2. gRPC / Connect: Rejected — browser-first consumption with SSE streaming and zero-toolchain clients favors plain HTTP + OpenAPI.
3. Spec generation from code annotations (tsoa, zod-to-openapi): Deferred — attractive for drift-proofing but would force a handler-framework migration; revisit if the surface grows.
4. Keeping ad-hoc { "error" } JSON: Rejected — no media-type distinction, no stable classification, nothing for clients to program against.