Implementing API Design Patterns

1. Implementing RESTful Resource Modeling

PatternExample
CollectionGET /orders, POST /orders
ItemGET /orders/42, PUT, DELETE
Sub-resourceGET /orders/42/items
ActionPOST /orders/42/cancel (when not CRUD)
Plural nounsUse plural; never verbs in path

2. Implementing Pagination

StyleProsCons
Offset/LimitSimple, jump to pageSlow at deep pages; inconsistent with inserts
Cursor (keyset)Fast, consistentNo random page access
Page token (opaque)Hides impl; resilientServer-side state encoding
Time-basedStreams (since=...)Tie-breaker needed

Example: Cursor pagination response

{
  "data": [ /* ...items... */ ],
  "next_cursor": "eyJpZCI6MTIzfQ==",
  "has_more": true
}

3. Implementing Filtering and Sorting

ConventionExample
Equality filter?status=active
Range?created_after=2026-01-01
Sort?sort=-created_at,name (- = desc)
Field selection?fields=id,name
RSQL/JSON-APIStandard query languages

4. Implementing API Versioning

StrategyExampleNotes
URL path/v2/ordersMost visible; simple
HeaderAccept: application/vnd.api+json;v=2Cleaner URLs
Query param?api-version=2Easy to test
Date-based2026-01-01 (Stripe)Per-customer pinning

5. Implementing Rate Limiting Headers

HeaderMeaning
X-RateLimit-LimitWindow quota
X-RateLimit-RemainingRemaining in window
X-RateLimit-ResetUnix ts of reset
Retry-AfterSeconds to wait (on 429)
RateLimit-* (RFC 9239)Standardized variants

6. Implementing HATEOAS

Example: HAL-style response

{
  "id": 42,
  "status": "pending",
  "_links": {
    "self":   { "href": "/orders/42" },
    "cancel": { "href": "/orders/42/cancel", "method": "POST" },
    "items":  { "href": "/orders/42/items" }
  }
}
FormatNotes
HAL_links object
JSON:APITop-level links + relationships
SirenIncludes actions with method/fields

7. Implementing Idempotency Keys in APIs

AspectDetail
HeaderIdempotency-Key: <uuid>
ScopePer endpoint + per API key
ReplaySame key returns identical response
TTL24h (Stripe) to 7 days

8. Implementing Bulk Operations

PatternDetail
Batch endpointPOST /orders/batch with array
Per-item status207 Multi-Status with sub-codes
All-or-nothingTransaction-like; reject all on any error
ChunkingServer limits batch size (100-1000)

9. Implementing Long-Running Operations

Async LRO Pattern

  1. Client POSTs request → server returns 202 Accepted + operation id
  2. Server sets Location: /operations/{id}
  3. Client polls GET /operations/{id} for status
  4. Or server fires webhook on completion
  5. Final response includes result or error

10. Implementing API Error Handling and Status Codes

CodeMeaning
200/201/204Success / Created / No Content
400Validation / malformed
401 / 403Unauthenticated / Forbidden
404 / 409Not Found / Conflict
422Unprocessable entity (semantic)
429Rate limited
500 / 502 / 503 / 504Server error / Bad gateway / Unavailable / Timeout
RFC 7807application/problem+json error format

Example: Problem Details (RFC 7807)

{
  "type": "https://api.example.com/errors/insufficient-funds",
  "title": "Insufficient funds",
  "status": 422,
  "detail": "Account balance 30.00 USD is below required 100.00 USD",
  "instance": "/transactions/abc-123",
  "trace_id": "9f2e1c..."
}