Backend API changes with agents and contract tests

Backend API work with agents should start from contracts, not handlers. Agents invent fields, rename enums, and “simplify” response shapes.

Published 2026-11-09 · Translation: VI

Backend API work with agents should start from contracts, not handlers. Agents invent fields, rename enums, and “simplify” response shapes. Consumers break quietly.

Define or update OpenAPI (or your equivalent) and contract tests first, then implement slices that satisfy them. That keeps the public surface intentional.

Contract first, or handler first?

Contract

Contract

Start from the contract when:

  • The change is written as a schema or OpenAPI diff first.
  • Contract tests fail first, then pass.
  • Authz cases are listed, including “internal” endpoints.
  • Additive vs breaking is named explicitly.
  • A client migration note is drafted when consumers will feel it.

Do not start from handlers when the schema is reverse-engineered afterward, errors are ad-hoc per endpoint, authz is skipped because “it is internal”, or field types change silently.

Agents can propose API shapes. API owners accept naming, pagination, and error standards. Experimental endpoints need a clear mark, stay out of stable SDKs, and have an expiry or promotion plan.

Workflow: contract → tests → implementation

  1. Write the API change as a contract diff. New routes/fields, deprecations, error semantics, auth requirements.
  2. Update contract tests and consumer-driven checks. They should fail first.
  3. Implement handlers to satisfy tests. Keep business logic behind clear service boundaries.
  4. Run compatibility and authz suites against previous consumers when available.
  5. Publish changelog entries that classify additive vs breaking. Add client notes and feature flags if rollout must be staged.
  6. Version client SDKs deliberately when contracts change. A server update without a client note creates distributed outages that look like “random mobile bugs.”

Keep a shared error catalog. Agents love inventing new error shapes per endpoint. Put one problem-details format (or your equivalent) in rules with an example.

Stop gates

  • Handlers first, schema later.
  • Ad-hoc error JSON that differs per endpoint.
  • Field type breaks without a versioning plan.
  • Authz tests skipped for “internal” APIs.

Checklist before merge

  • Contract diff reviewed before implementation.
  • Contract tests failing first, then passing.
  • Authz cases included.
  • Client migration notes written when needed.
  • Changelog classifies additive vs breaking.

Three traps consumers feel first

Failure modes to catch early

Failure modes to catch early

  • Schema last. Handlers ship first; schema is reverse-engineered later. Fix: review the contract diff before implementation starts.
  • Error zoo. Each endpoint invents its own error JSON. Fix: shared error catalog in rules; reject one-off shapes.
  • Skip authz. “Internal” becomes an excuse. Fix: authz cases are required in the contract pack.

Bottom line

When this fits vs when it does not

When this fits vs when it does not

The contract is the product. Handlers are disposable relative to compatibility promises. Pair this with API versioning rules agents must not invent when a change is breaking, and with test-first repair loops so contract tests stay red until the surface is intentional.

Share

X Facebook LinkedIn
Educational content only. Rankings reflect stated criteria at the update date—not investment advice or permanent performance guarantees.