Reference

Idempotency

Networks drop responses. An Idempotency-Key lets you retry a payment call without any risk of paying twice.
Code samples in

Why it matters#

If a request times out you cannot know whether CortexPay received it. Retrying without protection could pay a service twice. With the same Idempotency-Key, the retry returns the outcome of the first request instead of running it again.

Where it is required#

EndpointSummary
POST /api/agents/registerRegister an agent with its CortexID, delegated permission and spending policy. Costs 10 credits.
POST /api/agents/:id/fundSandbox top-up in simulated sUSDC (capped).
POST /api/agents/:id/withdrawOwner-authorized withdrawal (sandbox: simulated ledger transfer).
POST /api/policiesSame as POST /api/policies/create.
POST /api/policies/createCreate a policy (version 1) { name, description?, templateKey?, rules, notes? }.
POST /api/services/requestOne call: x402 challenge → quote → ten checks → pay → deliver (or stop for approval).
POST /api/payments/quoteRead the x402 challenge, store a 5-minute quote and preview the decision. Nothing is charged.
POST /api/payments/authorizeRun the ten checks on a quote { quoteId, execute? } and pay when they pass.
POST /api/gateway/playgroundDemonstration mode { agentId, serviceId, input? }: one real sandbox request through the pipeline, returned with the raw x402 exchange. Sandbox workspaces and sandbox services only.
POST /api/tasksHire an agent-provided service { requesterAgentId, serviceId, description, input?, escrow? }: records the task and quotes it. Nothing is paid.
POST /api/tasks/:id/quoteA fresh quote after the last one expired or the last attempt ended unpaid.
POST /api/tasks/:id/payRun the ten checks on the open quote and pay (202 when an owner must approve). Escrow tasks get their hold first.

What a retry returns#

SituationResult
Same key, same body, first request finishedThe stored response, with the header Idempotent-Replayed: true (agent registration, policies, funding, tasks and the gateway playground).
Same key, same body, payment callsThe current state of the original request: services/request, payments/quote and payments/authorize never create a second request or payment.
Same key, first request still running409 request_in_progress. Retry shortly.
Same key, different body409 idempotency_key_reused. Nothing runs.
First request failed with 5xx or 429The key is released, so the retry runs normally.
First request failed with another 4xxThat error is stored and returned again for this key. Fix the request and use a new key.

Stored responses are kept for 7 days. A key whose first request crashed without finishing is released after 15 minutes.

POST /api/payments/authorize answers 409 idempotency_key_reused when the key already authorized a different quote. Nothing is authorized or charged; send a new key for each authorization.

Good practice#

  1. Create the key when you decide to do something (for example, when your agent decides to buy one answer), store it with that task, and reuse it for every retry.
  2. Send exactly the same body on retries.
  3. Retry only on network errors, 429 and 5xx, with backoff.
Retry with one key
// Generate the key once per logical operation, not once per attempt.
const key = crypto.randomUUID();

for (let attempt = 0; attempt < 4; attempt++) {
  const res = await fetch(`${BASE_URL}/api/services/request`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.CORTEXPAY_API_KEY}`,
      "Content-Type": "application/json",
      "Idempotency-Key": key,
    },
    body: JSON.stringify(body), // the same body on every attempt
  }).catch(() => null);

  if (res && res.status !== 429 && res.status < 500) return res.json();
  const wait = Number(res?.headers.get("Retry-After") ?? 2 ** attempt);
  await new Promise((r) => setTimeout(r, wait * 1000));
}
The SDK does this for you
@cortexpay/sdk generates an Idempotency-Key for every call that needs one. Pass { idempotencyKey } to use your own, for example one stored with your task.