DEV Community

Cover image for Agree the API contract before you build the screen
Shireff Nady
Shireff Nady

Posted on Fully Autonomous

Agree the API contract before you build the screen

The sprint was going fine until integration day. The screen was built, the endpoint was built, and they did not fit.

The frontend expected { results: [...] }. The backend returned { data: { items: [...] } }. The date was a string in one and a number in the other. The error case returned a 200 with success: false, which the UI treated as success. None of these were anyone's fault, and all of them cost two days.

This happened often enough that we changed how we start features, not how we finish them.

What a contract actually is

One page, agreed before implementation. Not a spec document, not a wiki nobody opens. One page that answers:

  • Endpoint and method. The exact path, including version.
  • Request shape. Fields, types, which are required, which are nullable.
  • Response shape. The full object, nested, with types. Not "returns the order" - the actual fields.
  • Error shapes. Status codes, the error body, and what each one means. Validation errors are their own shape, and the UI needs to render them field by field.
  • Pagination and filtering. Cursor or offset, page size, sort order, what happens at the last page.
  • Auth. Who can call it, what happens on expiry.
  • Ownership. Who changes it, and how a change is announced.

That is twenty minutes of conversation. It replaces two days of rework, and it replaces the Slack thread where someone asks "what does this field mean" three weeks later.

How we made it stick

Write it before the ticket moves. The feature ticket does not enter the sprint until the contract is on it. That single rule did most of the work.

Mock against the contract, not against the implementation. Once the shape is agreed, the frontend builds against a mock that follows it exactly. The UI work is not blocked on the backend, and the mock is the first test of whether the contract is complete: if you cannot build the screen from it, it is missing something.

Generate types where you can. If the backend can publish an OpenAPI schema, generate the client types from it. Then a mismatch is a compile error instead of a bug report.

Agree the unhappy paths first. Most integration bugs live in the empty state, the error state, and the loading state. If the contract does not say what a 422 looks like, the UI will invent something and be wrong.

The trade-offs, honestly

  • It is overhead for small changes. A one-field tweak does not need a ceremony. We used a light version: the change and its shape in the pull request description, reviewed by both sides.
  • Contracts drift. A document that lives outside the repo goes stale in a month. Ours lived next to the code and changed in the same pull request as the endpoint, which is the only version that survives.
  • It does not remove the need for integration tests. It removes the silly mismatches, so the tests you do write are about behavior.

What it changed

Mid-sprint integration surprises mostly stopped. The frontend could start on day one instead of waiting for the backend to be done, because the shape was already agreed. It became the standard way new features started, without anyone mandating it - the team had felt the cost of the alternative.

The cheapest bug is the one you never wrote, and the cheapest integration bug is the one you agreed not to have.

Top comments (0)