Retry a write without double-charging
Attach an idempotency key so a retried POST settles once, not twice.
Task
You want to retry a POST that creates a charge or an order — but a retry that
actually succeeded server-side the first time would double-charge. You need the
server to recognize the replay as the same logical write.
Example
One way: pair retry with an idempotency key derived from the request.
import { } from 'stitchapi';
const = ({
: 'POST',
: 'https://api.example.com',
: '/orders',
: { : 3, : [429, 503] },
: {
// body is unknown, so narrow it before reading a property.
: () => `order-${(. as { : string }).}`,
},
});
const = await ({ : { : 'inv-1001' } });How it works
Each logical call carries a stable Idempotency-Key header. When
retry replays the write, the server sees the
same key and dedupes the side effect instead of running it twice. The default
key is a random uuid generated once per call — already retry-safe; a custom
key derives a stable value from the
input (here the order ref) so the same logical
write always maps to the same key. Idempotency only pays off alongside retries.
A derived key must be stable for a given logical call and unique across
distinct ones — colliding keys make the server dedupe two different writes
into one.
See also
Keep a failing dependency from taking you down
Compose timeout, retry, and a circuit breaker so a degraded upstream fails fast instead of hanging every caller.
Catch a breaking API change before your users do
Wrap output with drift to compare every response against a committed snapshot and flag dropped fields, type flips, and new keys by severity.