API Design
Purpose
Design service interfaces that stay stable as the product evolves: resource-oriented
HTTP (or proto-first gRPC), explicit versioning and compatibility rules, uniform
pagination, idempotency for unsafe methods, and a machine-readable error model
(RFC 9457 problem details) that clients can program against.
When to Use / When NOT to Use
Use when:
- Designing or reviewing REST endpoints, gRPC services, or their contracts.
- Deciding if a change is breaking and needs a new version.
- Standardizing pagination, filtering, error payloads, or idempotency keys.
Do NOT use when:
- The task is documenting an existing API — use
api-documentation (design first,
then document).
- The interface is purely internal to one binary (function signatures) — use
design/design-pattern and the language skills.
Prerequisites & Assumptions
- HTTP APIs use JSON; the error model is RFC 9457
application/problem+json.
- gRPC uses proto3 with package versioning and buf lint/breaking checks.
- Contracts are versioned in the repo (
api/openapi.yaml, proto/) and enforced
by contract tests (testing/test-strategy).
Step-by-Step Workflow
- Model resources, not actions — nouns with stable identities
(
/v1/orders/{id}); verbs are HTTP methods; exceptions (search, cancel) get
documented subresources or controller resources.
- Define the compatibility contract — additive changes (new optional fields,
new endpoints) are non-breaking; removing/renaming fields, narrowing types,
or tightening validation is breaking → new major version (URI major:
/v2/...).
- Specify the error model — every non-2xx returns problem details with a
stable
type URI, human title, and a service-specific code (see example);
never leak internals.
- Design collections — cursor pagination (
page[size], page[cursor]),
bounded page size (default 50, max 200), stable sort, and filtering by
explicit query params.
- Make unsafe methods idempotent — POST-with-idempotency-key (clients send
Idempotency-Key), PUT is fully idempotent; retries are safe by design.
- Validate the contract — run contract tests and
buf breaking/oasdiff in
CI so compatibility is machine-checked, not remembered.
Enterprise Rules & Standards (MUST / MUST NOT)
- MUST use plural resource nouns, HTTP-method semantics (GET is safe,
PUT/DELETE idempotent, POST creates), and correct status codes
(201+Location for creation, 204 for deletes, 4xx for caller faults, 5xx never
for validation failures).
- MUST version only in the path major (
/v1); everything else evolves
additively; deprecations are announced with a Deprecation/Sunset header and
a removal date before breaking changes.
- MUST return RFC 9457 problem details with a stable, documented
code for
every error.
- MUST make POSTs retry-safe via idempotency keys on any endpoint that moves
money or state.
- MUST bound every list endpoint with cursor pagination and a max page size.
- SHOULD accept and return timestamps as RFC 3339 UTC; identifiers as opaque
strings (never expose internal DB sequences).
- SHOULD use
PUT for full replace, PATCH (JSON Merge Patch) for partial
updates.
- MUST NOT change a field's type or semantics in place, remove response
fields, or add required request fields within a major version.
- MUST NOT return
200 OK with an error body — status codes are the contract.
Examples
Good — error model (problem details, compiles as JSON):
{
"type": "https://api.example.com/problems/order-not-cancellable",
"title": "Order cannot be cancelled",
"status": 409,
"code": "ORDER_NOT_CANCELLABLE",
"detail": "Order o-123 has already shipped and cannot be cancelled.",
"instance": "/v1/orders/o-123"
}
Bad — stringly-typed failure:
{ "success": false, "error": "Error: duplicate key value violates
constraint \"orders_pkey\" on line 42 of orders_repo.go" }
Why it fails: leaks internals (table/constraint/file), no stable code to program
against, and the implicit 200 hides the failure from every standard HTTP client.
Good — pagination contract:
GET /v1/orders?page[size]=50&page[cursor]=eyJpZCI6Im8tMTIzIn0
→ { "data": [...], "next_cursor": "eyJpZCI6Im8tMTczIn0", "has_more": true }
Checklist
Common Pitfalls
- Breaking "just a little" — tightening validation or removing a field for a
"minor cleanup" breaks clients silently; additive-only within a major.
- Chatty APIs — one endpoint per UI screen; model resources, compose on the
client (or add a documented BFF explicitly).
- Verb URLs —
/getOrderById fights HTTP caching, semantics, and tooling.
- Cursor amnesia — page numbers over mutable collections skip/duplicate
rows; cursors are the default for anything concurrent.
References
1---2name: api-design3description: 'Designs HTTP and gRPC APIs — resource modeling, versioning, pagination, idempotency, and a RFC 9457 error model. Trigger with "design this endpoint", "is this API breaking", "how should we version this", or "review this API contract".'4metadata:5 source: https://skillmd.com/skills/pnguyen215/api-design6---78# API Design910## Purpose1112Design service interfaces that stay stable as the product evolves: resource-oriented13HTTP (or proto-first gRPC), explicit versioning and compatibility rules, uniform14pagination, idempotency for unsafe methods, and a machine-readable error model15(RFC 9457 problem details) that clients can program against.1617## When to Use / When NOT to Use1819Use when:2021- Designing or reviewing REST endpoints, gRPC services, or their contracts.22- Deciding if a change is breaking and needs a new version.23- Standardizing pagination, filtering, error payloads, or idempotency keys.2425Do NOT use when:2627- The task is documenting an existing API — use `api-documentation` (design first,28 then document).29- The interface is purely internal to one binary (function signatures) — use30 `design/design-pattern` and the language skills.3132## Prerequisites & Assumptions3334- HTTP APIs use JSON; the error model is RFC 9457 `application/problem+json`.35- gRPC uses proto3 with package versioning and buf lint/breaking checks.36- Contracts are versioned in the repo (`api/openapi.yaml`, `proto/`) and enforced37 by contract tests (`testing/test-strategy`).3839## Step-by-Step Workflow40411. **Model resources, not actions** — nouns with stable identities42 (`/v1/orders/{id}`); verbs are HTTP methods; exceptions (search, cancel) get43 documented subresources or controller resources.442. **Define the compatibility contract** — additive changes (new optional fields,45 new endpoints) are non-breaking; removing/renaming fields, narrowing types,46 or tightening validation is breaking → new major version (URI major:47 `/v2/...`).483. **Specify the error model** — every non-2xx returns problem details with a49 stable `type` URI, human `title`, and a service-specific `code` (see example);50 never leak internals.514. **Design collections** — cursor pagination (`page[size]`, `page[cursor]`),52 bounded page size (default 50, max 200), stable sort, and filtering by53 explicit query params.545. **Make unsafe methods idempotent** — POST-with-idempotency-key (clients send55 `Idempotency-Key`), PUT is fully idempotent; retries are safe by design.566. **Validate the contract** — run contract tests and `buf breaking`/oasdiff in57 CI so compatibility is machine-checked, not remembered.5859## Enterprise Rules & Standards (MUST / MUST NOT)6061- **MUST** use plural resource nouns, HTTP-method semantics (GET is safe,62 PUT/DELETE idempotent, POST creates), and correct status codes63 (201+Location for creation, 204 for deletes, 4xx for caller faults, 5xx never64 for validation failures).65- **MUST** version only in the path major (`/v1`); everything else evolves66 additively; deprecations are announced with a `Deprecation`/`Sunset` header and67 a removal date before breaking changes.68- **MUST** return RFC 9457 problem details with a stable, documented `code` for69 every error.70- **MUST** make POSTs retry-safe via idempotency keys on any endpoint that moves71 money or state.72- **MUST** bound every list endpoint with cursor pagination and a max page size.73- **SHOULD** accept and return timestamps as RFC 3339 UTC; identifiers as opaque74 strings (never expose internal DB sequences).75- **SHOULD** use `PUT` for full replace, `PATCH` (JSON Merge Patch) for partial76 updates.77- **MUST NOT** change a field's type or semantics in place, remove response78 fields, or add required request fields within a major version.79- **MUST NOT** return `200 OK` with an error body — status codes are the contract.8081## Examples8283Good — error model (problem details, compiles as JSON):8485```json86{87 "type": "https://api.example.com/problems/order-not-cancellable",88 "title": "Order cannot be cancelled",89 "status": 409,90 "code": "ORDER_NOT_CANCELLABLE",91 "detail": "Order o-123 has already shipped and cannot be cancelled.",92 "instance": "/v1/orders/o-123"93}94```9596Bad — stringly-typed failure:9798```json99{ "success": false, "error": "Error: duplicate key value violates100constraint \"orders_pkey\" on line 42 of orders_repo.go" }101```102103Why it fails: leaks internals (table/constraint/file), no stable code to program104against, and the implicit 200 hides the failure from every standard HTTP client.105106Good — pagination contract:107108```text109GET /v1/orders?page[size]=50&page[cursor]=eyJpZCI6Im8tMTIzIn0110→ { "data": [...], "next_cursor": "eyJpZCI6Im8tMTczIn0", "has_more": true }111```112113## Checklist114115- [ ] Endpoints are resource-oriented with correct methods and status codes.116- [ ] Compatibility rules applied: the change is additive or a new major version.117- [ ] Every error path returns problem details with a stable `code`.118- [ ] Collections paginate with cursors and a bounded page size.119- [ ] Unsafe methods accept idempotency keys; retries tested.120- [ ] `buf breaking` / oasdiff runs in CI against the last released contract.121- [ ] Deprecations have `Sunset` headers and a documented removal date.122123## Common Pitfalls124125- **Breaking "just a little"** — tightening validation or removing a field for a126 "minor cleanup" breaks clients silently; additive-only within a major.127- **Chatty APIs** — one endpoint per UI screen; model resources, compose on the128 client (or add a documented BFF explicitly).129- **Verb URLs** — `/getOrderById` fights HTTP caching, semantics, and tooling.130- **Cursor amnesia** — page numbers over mutable collections skip/duplicate131 rows; cursors are the default for anything concurrent.132133## References134135- RFC 9457 — Problem Details for HTTP APIs: https://www.rfc-editor.org/rfc/rfc9457136- Microsoft REST API Guidelines: https://github.com/microsoft/api-guidelines137- Google API Design Guide (AIP): https://google.aip.dev/general138- buf breaking (Protobuf): https://buf.build/docs/breaking/overview/