Skip to main content
App basics: HTTP, auth, dataLesson 2 of 11

Designing HTTP APIs

An API is a promise to every client that calls it. Follow an orders API as it grows from one endpoint to a public contract, and see which choices keep clients working when things go wrong or change.

RequestGET /v1/orders/42
Status200 OK
Contract—
Server sideClient appweb · mobileOld clientbuilt for v1OpenAPI specthe written contractOrders API/v1/ordersIdempotency keyskey → saved responseorders DBrowsinventorygRPC · protobufGET /v1/orders/42Read200 + JSON
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Resources as nouns, methods as verbs, and what safe and idempotent mean
  • Status codes that let a client decide what to do next
  • Idempotency keys so a retried payment is charged once
  • Cursor pagination and one consistent error body
  • Changing an API without breaking old clients, and REST vs gRPC vs GraphQL

Methods and status codes at a glance​

MethodUseSafeIdempotentTypical success
GETRead a resource or listYesYes200
POSTCreate in a collection, or run an actionNoNo (use an idempotency key)201 with Location, or 200 / 202
PUTReplace the whole resourceNoYes200 or 204
PATCHChange part of a resourceNoNot guaranteed200 or 204
DELETERemove a resourceNoYes204
StatusMeansClient should
400 Bad RequestMalformed request: bad JSON, wrong typeFix the request, do not retry as is
404 Not FoundNo such resourceStop, or check the id
409 ConflictConflicts with current state, such as a stale versionRe-read, then retry the change
422 Unprocessable ContentWell formed but breaks a ruleShow the field errors to the user
429 Too Many RequestsRate limit hitWait for Retry-After, then retry
500 / 502 / 503 / 504Server or upstream failedRetry idempotent requests with backoff
REST + JSONgRPCGraphQL
ContractOpenAPI (optional).proto file (required)Schema (required)
Wire formatJSON textProtobuf binaryJSON
TransportHTTP/1.1, 2 or 3HTTP/2Usually HTTP POST to one endpoint
StreamingServer-sent events, WebSocket on the sideBuilt in, both directionsSubscriptions
Best forPublic APIs, browsers, cachingService-to-service, low latencyVaried front ends that need different fields

Try it yourself​

The Kubernetes API is itself a well-designed HTTP API. Look at it directly:

kubectl proxy --port=8001 & # local, authenticated tunnel to the API server

curl -s localhost:8001/apis/apps | jq '.versions' # versions side by side in the path
curl -i localhost:8001/api/v1/namespaces/default/pods/nope # 404 with a Status object as the error body

# Pagination with limit and continue (a cursor)
curl -s 'localhost:8001/api/v1/pods?limit=2' | jq -r '.metadata.continue'

curl -s localhost:8001/openapi/v3 | jq '.paths | keys | .[:5]' # the published OpenAPI spec
kubectl get pods -A -v=6 2>&1 | grep -E 'GET|200' # kubectl asks in chunks: ?limit=500
kill %1 # stop the proxy

Check yourself

  1. A client retries PUT /v1/orders/42 with the same full body after a timeout. Why is that safe, while retrying POST /v1/payments is not?
  2. An orders list uses ?offset=40&limit=20. New orders arrive while a user pages through. What goes wrong, and what fixes it?
  3. The team wants to rename the response field total to amount. Which approach keeps old mobile apps working?

Further reading: HTTP Semantics (RFC 9110) · Problem Details for HTTP APIs (RFC 9457) · Idempotency-Key header (IETF draft) · OpenAPI Specification · gRPC core concepts · Kubernetes API concepts · HTTP/1.1, HTTP/2 and HTTP/3 · When You Type an Address · Making Service Calls Survive Failures