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—
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
| Method | Use | Safe | Idempotent | Typical success |
|---|---|---|---|---|
| GET | Read a resource or list | Yes | Yes | 200 |
| POST | Create in a collection, or run an action | No | No (use an idempotency key) | 201 with Location, or 200 / 202 |
| PUT | Replace the whole resource | No | Yes | 200 or 204 |
| PATCH | Change part of a resource | No | Not guaranteed | 200 or 204 |
| DELETE | Remove a resource | No | Yes | 204 |
| Status | Means | Client should |
|---|---|---|
| 400 Bad Request | Malformed request: bad JSON, wrong type | Fix the request, do not retry as is |
| 404 Not Found | No such resource | Stop, or check the id |
| 409 Conflict | Conflicts with current state, such as a stale version | Re-read, then retry the change |
| 422 Unprocessable Content | Well formed but breaks a rule | Show the field errors to the user |
| 429 Too Many Requests | Rate limit hit | Wait for Retry-After, then retry |
| 500 / 502 / 503 / 504 | Server or upstream failed | Retry idempotent requests with backoff |
| REST + JSON | gRPC | GraphQL | |
|---|---|---|---|
| Contract | OpenAPI (optional) | .proto file (required) | Schema (required) |
| Wire format | JSON text | Protobuf binary | JSON |
| Transport | HTTP/1.1, 2 or 3 | HTTP/2 | Usually HTTP POST to one endpoint |
| Streaming | Server-sent events, WebSocket on the side | Built in, both directions | Subscriptions |
| Best for | Public APIs, browsers, caching | Service-to-service, low latency | Varied 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
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