API design¶
Design interfaces between systems that are consistent, evolvable and safe to call.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
Pick one style guide per organisation and follow it. Don't mix them.
- 9 Google API Improvement Proposals (AIPs): detailed, resource-oriented rules for HTTP/JSON and gRPC APIs. (Google Cloud API design guide is the older companion.)
- 9 Zalando RESTful API Guidelines: a complete, opinionated guide that is easy to adopt as-is.
- 9 OpenAPI Specification and AsyncAPI Specification: contract formats for synchronous and event-driven APIs.
- 8 Microsoft REST API Guidelines: Azure and Microsoft Graph conventions.
- 8 OWASP API Security Top 10: the most common API security risks.
- 7 Azure: RESTful web API design: a shorter introduction to the basics.
Key rules¶
- 9
API-1Make operations safe to retry, using idempotency keys where the method isn't naturally idempotent. (AWS Builders' Library, Making retries safe with idempotent APIs, IETF draft, Idempotency-Key header) - 9
API-2Evolve APIs in backward-compatible ways. Treat breaking changes as a new major version. (AIP-180, Backwards compatibility, Semantic Versioning) - 8
API-11Limit what each client can consume: cap the size of request bodies, strings, arrays and uploads, the number of records (API-6) and operations one request can ask for, and the execution time of every request, and rate-limit each client or user, with stricter limits on costly or sensitive operations. Without these limits, one client can exhaust the service's CPU, memory or connections, or run up its costs. Platform examples:GO-8,NODE-12,GQL-8andGAMESERVER-15; for sign-in attempts, seeAUTH-14. (OWASP API Security Top 10, API4:2023 Unrestricted Resource Consumption) - 8
API-3Use HTTP methods and status codes as their specification defines them. (RFC 9110, HTTP Semantics) - 7
API-4Design the API contract first and review it before implementing it. (Zalando, API First) - 7
API-5Model HTTP APIs around resources and standard methods, not ad-hoc RPC verbs. (AIP-121, Resource-oriented design) - 7
API-6Paginate list endpoints from day one. (AIP-158, Pagination) - 6
API-7Return machine-readable errors in the Problem Details format. (RFC 9457, Problem Details for HTTP APIs) - 6
API-8With enough users, every observable behaviour will be depended on by someone, whether documented or not (Hyrum's law). (Hyrum's Law) - 6
API-9Verify compatibility between providers and consumers with consumer-driven contract tests. (Robinson, Consumer-Driven Contracts, Pact) - 5
API-10For gRPC and Protocol Buffers, follow the official dos and don'ts on field numbering and evolution. (Protocol Buffers, dos and don'ts, gRPC guides)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 buf breaking: compares Protobuf schemas with a previous version in CI and reports breaking changes, including deleted fields whose numbers weren't reserved. Checks
API-2andAPI-10. - 7 Google API Linter: lints Protobuf-defined APIs against the AIPs, for example
page_sizeon List methods and Get support for every resource. ChecksAPI-6, and partlyAPI-5. - 7 Pact can-i-deploy: blocks a deployment unless contract tests show the new version is compatible with the versions already deployed. Checks
API-9. - 6 oasdiff, breaking changes: reports breaking changes between two OpenAPI versions, and fails CI with
--fail-on ERR. ChecksAPI-2. - 6 Spectral OpenAPI rules: lints OpenAPI documents in CI. The built-in ruleset checks basics, such as at least one 2xx or 3xx response per operation, and custom rulesets can encode your chosen style guide. Partly checks
API-3.