API contracts
On this page
Rule#
The server owns the contract. It emits an OpenAPI document for its REST surface and an AsyncAPI document for its real-time surface; both are committed to the repo, and every client is generated from them with a CI drift check. Clients never hand-write types the server already describes.
This is the "contract = api/openapi.json" half of the product's three sources of truth
(archetypes/full-stack-product.md).
Entities exposed in user-facing URLs and public API surfaces are addressed by their public
short id, not their internal Id<T> (UUIDv7). See public-ids.md.
REST: OpenAPI#
- poem-openapi derives the spec from the
…Apistructs. Expose it two ways:- an
openapisubcommand that prints the JSON (<bin> openapi > api/openapi.json), - the live
/openapi.jsonroute + Scalar docs at/docs.
- an
- Commit
api/openapi.json. It's the artifact clients build against. - Generate typed clients from it, per platform:
- Web:
openapi-typescript→schema.d.ts, consumed byopenapi-fetch. - Apple:
swift-openapi-generator→Types.swift+Client.swift. - Windows: Kiota → generated client.
- Web:
- Drift check in CI (ci-cd.md): regenerate and
git diff --exit-code. A diff means the committed client is stale — fail the build. This keeps every client honest against the server.
Real-time: AsyncAPI + the envelope#
WebSocket/SSE frames are server-to-client JSON with a uniform envelope, documented in
api/asyncapi.md:
{ "kind": "<dotted.event.name>", "data": { /* DTO or delta */ } }
// some products also thread a correlation id: { "kind": "...", "order_id": "<uuid|null>", "data": {} }
kindis a stable dotted name (order.created,job.started,assignment.updated).datareuses the same DTOs as the REST API (so generated types apply).- Clients filter by
kind+ ids and apply idempotently; on a gap they refetch via REST. - SSE tags each frame with an
event_typestring mirroringkind; keep-alive ping ~30s.
Transport: broadcast hub#
Real-time fan-out is a tokio::broadcast channel with a bounded backlog (e.g. 256). The
server publish()es serialized events to all subscribers; slow clients that lag past the
backlog are dropped and expected to resync over REST. This WsHub lives in AppState.
Inter-service contract: protobuf#
Service-to-service RPC over a persistent TCP connection uses protobuf (prost) with
length-delimited framing and a oneof result { ok; error } envelope. This is the auth SDK's
wire format — see auth-integration.md. REST/JSON is for clients;
protobuf is for the hot internal auth path.
Checklist#
- Server emits OpenAPI via an
openapisubcommand and/openapi.json+/docs. api/openapi.jsonis committed.- Every client is generated from it (openapi-typescript / swift-openapi-generator / Kiota).
- CI drift check (
git diff --exit-code) on every generated client. - Real-time frames use the
{kind, data}envelope, documented inapi/asyncapi.md. - Fan-out via a bounded
broadcasthub; clients resync over REST on gaps.