HELM
Ch 07 · APIs
Chapter 07 · The contract

APIs — pick the protocol, get the rules right

REST, GraphQL, gRPC, tRPC, JSON-RPC, WebSockets, SSE — six protocols, one decision matrix. Plus the API hygiene that separates "ships" from "scales".

7.1 · The protocol family at a glance

ProtocolTransportSchemaBest for
RESTHTTP/JSONOpenAPIPublic APIs, broad client compatibility.
GraphQLHTTP / WSSDLMany heterogeneous clients; flexible field selection.
gRPCHTTP/2 + protobuf.protoInternal service-to-service, polyglot.
Connect / ConnectRPCHTTP/JSON or gRPC.protogRPC's ergonomics with browser-friendly transport.
tRPCHTTP/JSONTS typesFull-stack TS monorepo; instant typed RPC.
JSON-RPCHTTP / WSJSON SchemaCrypto, MCP, lightweight RPC.
WebSocketWSyour choiceBidirectional real-time.
SSEHTTP—Server-to-client streaming (LLM tokens, notifications).
WebhooksHTTPJSON SchemaServer-to-server async events.

7.2 · REST — done well

  • Resources as nouns: /orders/{id}/refunds — not RPC-ish verbs.
  • Verbs: GET (safe), POST (create), PUT (replace), PATCH (partial), DELETE.
  • Status codes: 2xx success, 3xx redirect, 4xx client error, 5xx server. Use 409 / 422 thoughtfully.
  • Pagination: cursor (?after=…) beats offset at scale.
  • Filtering: ?status=paid&created_at[gte]=… — document and validate.
  • Errors: stable shape ({ "error": { "code": "…", "message": "…", "request_id": "…" } }).
  • OpenAPI 3.1 spec is the source of truth; generate clients + docs from it.

7.3 · GraphQL — fields on demand

  • Schema-first; clients request only what they need.
  • Solves "N round-trips per screen" for mobile.
  • Watch out: query cost, N+1 (use DataLoader), caching is harder than REST.
  • 2026 trends: federation (Apollo, Hive) for multi-team graphs; persisted queries for security + caching.

7.4 · gRPC & Connect

  • Binary protobuf over HTTP/2; bi-directional streaming; auto-generated clients in 11 languages.
  • Best for internal service mesh.
  • Connect / ConnectRPC (Buf) — same .proto, but speaks HTTP/JSON too so browsers and curl work.
  • Pair with Buf for schema management, breaking-change detection, lint.

7.5 · tRPC

Inside a TypeScript monorepo, define a router on the server; the client gets typed function calls — no schema duplication, no client gen. Powerful for full-stack TS; useless outside it.

7.6 · WebSockets & SSE

WebSocketSSE (EventSource)
DirectionFull-duplexServer → client only
TransportWS upgradePlain HTTP
ReconnectYou implementBuilt-in
Browser supportUniversalUniversal (incl. mobile)
Sweet spotChat, collaboration, multiplayerLLM streaming, notifications, progress

Rule of thumb: if the client doesn't need to send much, SSE is simpler and survives proxies better.

7.7 · Webhooks — done right

  • Sign payloads (HMAC) and document the verification.
  • Retry with exponential backoff; expose a delivery log + replay tool.
  • Idempotent handlers — receivers will get duplicates.
  • Provide event types, versioning, and a sandbox.
  • WebSub / WebTransport are real but niche — webhooks remain the default.

7.8 · API design hygiene — the non-negotiables

  1. Idempotency on every write (Idempotency-Key header).
  2. Pagination — never return unbounded collections.
  3. Rate limits + clear 429 + Retry-After.
  4. Tracing headers (traceparent) propagated end-to-end.
  5. Auth — OAuth 2.1 / OIDC + scopes; never hand-rolled tokens.
  6. Errors with stable codes; never raw stack traces.
  7. Backwards compatibility by default; deprecation policy in writing.

7.9 · Versioning & deprecation

  • URI versioning (/v1/...) — explicit; easy to route.
  • Header versioning (Accept: application/vnd.acme.v2+json) — cleaner URLs, harder for clients.
  • Date versioning (Stripe-style) — clients pin a date, you ship breaking changes monthly.
  • Deprecate via Sunset + Deprecation headers; communicate; provide a migration window.

7.10 · Self-check

Q1 · You stream LLM tokens from server → browser. Best protocol?
WebSocket
SSE
gRPC
REST long-poll