Specification-led APIs: contracts you can test
Turn product requirements into explicit inputs, permissions, state changes, and acceptance tests before implementing an endpoint.
Key ideas
- Specify behavior, permissions, and failure cases together.
- Use runtime validation alongside TypeScript types.
- Make retries and concurrency part of the acceptance criteria.
On this page
Begin with behavior someone can verify
A requirement such as “let users export a report” leaves important questions unanswered. Who may request it? Which filters are valid? What happens if the request is sent twice? How does the client learn that the export failed?
Specification-led development makes those decisions visible before implementation. The useful output is a small agreement about behavior: inputs, permissions, state changes, responses, and failure cases. That agreement gives the interface, API, and tests something shared to work toward.
This is the same principle behind the bounded tasks and explicit interfaces in Project Chimera. For an HTTP API, the contract must also account for persistence and callers that retry.
Describe one operation precisely
Consider an asynchronous report export. A compact specification might say:
- An authenticated user can request an export for data they may access.
- The request specifies an approved format and a bounded date range.
- A successful request returns a job identifier and a way to inspect progress.
- Repeating an equivalent request with the same idempotency key returns the existing job.
- Reusing that key with different input produces a conflict.
The date range needs a defined timezone and ordering rule. File size and retention limits belong in the specification when they affect the user journey. Leaving them implicit moves the decision into whichever implementation happens to ship first.
Separate types from validation
A TypeScript response type makes expectations visible inside the application:
type ExportJob = {
id: string;
status: "queued" | "running" | "complete" | "failed";
};
type CreateExportResponse = {
job: ExportJob;
statusUrl: string;
};
These types do not validate an incoming HTTP body or an external service response. Runtime schemas must check those boundaries. Authentication identifies the caller; authorization decides whether the requested data and action are available to that caller.
Keep an API description, runtime validation, and application types aligned. A schema-first or generated-client workflow can reduce duplication, but it still needs review when behavior changes.
Make retries safe at the persistence boundary
A disabled submit button improves the interface, but a timeout or interrupted connection can still produce a second request. The server needs a durable rule for recognizing the same operation.
Scope an idempotency key to the actor and operation, associate it with normalized input, and use a database constraint or transaction to prevent competing requests from creating duplicate work. Define how long that association lasts and what a caller receives while the original operation is still running.
An in-memory lookup can help a local demonstration. It cannot coordinate separate server instances or survive a restart. Choose the persistence mechanism according to the guarantee the API promises.
Turn the contract into acceptance tests
Test the behavior callers depend on, including the paths that are easy to overlook:
| Scenario | Expected behavior |
|---|---|
| Valid request from an authorized user | A job is created and a progress URL is returned |
| Invalid range or unsupported format | A stable validation response identifies the problem |
| Request for another user's restricted data | Access is denied before work starts |
| Equivalent retry with the same key | The existing operation is returned |
| Same key with different input | The API reports a conflict |
| Two equivalent requests arrive together | Only one operation is created |
The concurrency case matters: sequential retries can pass even when a race still creates duplicates. Review tests at the boundary that owns the guarantee, rather than checking only a helper function.
Keep the specification useful after release
When the operation changes, update the contract and its examples with the implementation. If an AI agent helps write code, give it the relevant acceptance criteria, existing conventions, and a bounded task. Review the resulting behavior against the same agreement a human contributor would use.
A specification earns its place when it helps a team resolve ambiguity and detect regressions. Its value comes from the decisions it preserves and the checks it enables.