Skip to content

Free guide

Write an API contract that survives retries

Define how systems exchange JSON, verify the caller, map fields, acknowledge events and recover when delivery is uncertain.

Why it matters

A product manager needs a clear promise for “order submitted.” A developer needs a stable way to recognize the same logical request after a timeout.

Core concepts

Requests and responses

A REST-style HTTP API exposes resources such as orders. GET reads a representation. POST often creates work. Specify the allowed fields, statuses, authentication, error meanings, pagination and version. JSON carries values but does not define their business meaning.

Authentication and authorization

Authentication establishes who or what is calling. Authorization determines which action that identity may take on this particular record. Put the decision on the server for each request. A hidden UI button cannot grant or revoke access.

Two ways to hear news

Polling asks the provider for changes. A webhook sends an event when something happens. Document signed delivery requirements, ordering guarantees and replay behavior from your particular provider. Validate an incoming event before recording it.

A timeout creates uncertainty

The provider may have accepted a write even when its reply never arrived. Use a stable operation key when supported. Keep a durable local record and reconcile by the external identifier. A unique event key deduplicates an event, while two different events can still refer to the same business change.

Try it step by step

  1. Map one input, one reply and one error to a written business meaning.
  2. Agree on units, time zones, identifier formats, permissions and the ownership of each field.
  3. Walk through timeout after acceptance, duplicate delivery, out-of-order arrival and a permanent validation error. Assign each a recovery action.

Use a contract matrix: operation, identity, input, normal reply, retryable failures, durable key and reconciliation method.

Worked example

Fictional example: An order submission times out. Before sending another creation request, look up the original operation key. If an order exists, attach the existing ID. If the provider cannot resolve it safely, route the case for review.

SQL
CREATE TEMP TABLE received_event (event_id text PRIMARY KEY);
INSERT INTO received_event VALUES ('42'), ('42'), ('43')
ON CONFLICT (event_id) DO NOTHING;
SELECT COUNT(*) FROM received_event;
-- 2

Common mistake

Retrying every 400 response or assuming a timeout means no change occurred.

Practice exercise

Run the SQL in a disposable PostgreSQL session. Make event 42 arrive twice and event 43 arrive once. Check that two event identities remain. Explain why this alone cannot prevent a duplicate external order.

Check your understanding

A write times out after leaving your server. What do you know?

  1. Its final state is uncertain
  2. The provider rejected it
  3. It is safe to create another record with a new ID
Show answer

Its final state is uncertain. The missing response tells you about the connection. Reconcile the actual business state.

Takeaway

Every integration needs a plan for “we sent it and never heard back.”

Further reading

Browse more lessons