LEARN. PRACTICE. EXPLAIN.
Learning paths & interview practiceP
Full-stack foundations
LESSON 1 OF 3 · ABOUT 14 MINUTES + EXERCISE

What is an API contract?

THE IDEA

What it is

An API contract specifies what a client sends and what the server returns, including errors and compatibility rules. The browser displays the result, while the server validates input, identity, permissions, and business constraints.

Why you need it

Two components can compile successfully and still disagree about a field name, a null value, or what a success response means. The contract makes those assumptions explicit.

How it works

  1. The form collects input and gives immediate feedback.
  2. The client sends a documented request shape.
  3. The server validates and persists the allowed operation.
  4. The response lets the UI distinguish saved, rejected, and uncertain outcomes.
HTTP

A small profile-update contract

PATCH /profile
Content-Type: application/json

{"displayName":"Amina","version":7}

200: {"displayName":"Amina","version":8}
422: {"errors":{"displayName":"Enter a shorter name"}}
409: {"code":"VERSION_CONFLICT"}

These are example application semantics. Define them with both client and server implementers. A timeout is not one of these known responses: the write may have happened even if its reply was lost.

Good practices and why

  • Validate on both sides for their different purposes: usability in the client and correctness at the server.
  • Evolve responses compatibly while older clients may still be running.

Common mistakes

  • Trusting client validation as the authoritative rule.
  • Showing saved before deciding how a failed or conflicting request will be reconciled.
GUIDED IMPLEMENTATION

Try it yourself

Write request and response examples for a task-creation form, including invalid title, expired login, and network uncertainty.

What a successful result looks like

The UI has a deliberate behavior for every response and does not create duplicates blindly after a timeout.

0 / 6000 characters · Saved on this device. Do not include passwords or private data.

Check your understanding

Where must a business constraint be enforced if the browser already validates it?

Answer the understanding check correctly to record exercise completion.

PUT THE IDEA UNDER PRESSURE

Related interview practice

These scenarios add constraints and follow-up questions to the concepts you just learned.

Further reading

Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.

AlifBay Prep