API contract design
Rules
- Contract first: the specification is written and reviewed before implementation.
- Backward compatible by default. A breaking change needs a new version or an agreed deprecation window.
- Consistency with the existing API's conventions beats preference. Read the existing specification first.
Procedure
- Consumers and use cases. Who calls it, for what, how often; latency and volume expectations.
- Model. REST: resources as nouns with standard methods. Events: past-tense facts (
order.paid) with an owner domain. - Schemas. Types, required vs optional, formats:
- Timestamps in ISO 8601 UTC.
- Money as an integer of minor units or a decimal string, always with an ISO 4217 currency code; never floating point.
- IDs as opaque strings.
- Enumerations documented as extensible; consumers must tolerate unknown values.
- Errors. One error envelope across the API (for example RFC 9457 problem details), stable machine-readable codes, correct status codes, no internal details.
- Collections. Pagination from the start (cursor-based for large or changing sets), filters, sort, and a maximum page size.
- Idempotency and concurrency. Idempotency keys for non-idempotent creates and payments; ETag with If-Match or a version field for updates. Event consumers must tolerate duplicates and out-of-order delivery.
- Security. Authentication method; authorisation per operation and resource; scopes; rate limits; size and length limits; sensitive fields marked and minimised.
- Versioning and evolution. State the strategy (path, header, or schema evolution) and keep changes additive within a version.
- Breaking-change check against the current specification with breaking-changes.md.
- Deprecation plan for any break: announcement, dual-run period, usage telemetry on the old version, removal date, consumer sign-off.
- Examples for success and the main error cases.
- Write the specification and validate it with the project's linter if one exists (read-only run).
Output
-
The specification change (file diff).
-
Change summary:
Operation / message Change Backward compatible? Consumer impact -
Open questions with options and one Recommendation.
Quality checklist
- Existing conventions followed; deviations justified.
- Every operation has authorisation, errors, and limits defined.
- Collections are paginated; money and time formats are safe.
- The breaking-change check is recorded.
- Examples exist for success and main errors.
Pitfalls
- Exposing the database structure as the API.
- A boolean that later needs a third state.
- Adding pagination after release — that is a breaking change.