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; keyOf derives a stable value from the
input (here the order ref) so the same logical
write always maps to the same key — which collapses two separate submissions
(a double-click) even with no retry in play.
A derived keyOf 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
Cancel an in-flight call
Pass an AbortSignal in the call input — aborting severs the request and stops retries, waits, and pagination with it.
Catch a breaking API change before your users do
Wrap output with drift to validate every response against the declared schema and catch a dropped required field, a type coercion, or an undeclared new key.