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.
The header#
Idempotency-Key: <8–128 characters of A–Z a–z 0–9 . _ : ->. A UUID is ideal.- Keys are scoped to your workspace. Use a new key for every new operation:
services/requestandpayments/quoteshare one set of keys, and every authorization needs its own. - Missing or malformed keys are refused with
400 bad_requestbefore anything happens.
Where it is required#
| Endpoint | Summary |
|---|---|
POST /api/agents/register | Register an agent with its CortexID, delegated permission and spending policy. Costs 10 credits. |
POST /api/agents/:id/fund | Sandbox top-up in simulated sUSDC (capped). |
POST /api/agents/:id/withdraw | Owner-authorized withdrawal (sandbox: simulated ledger transfer). |
POST /api/policies | Same as POST /api/policies/create. |
POST /api/policies/create | Create a policy (version 1) { name, description?, templateKey?, rules, notes? }. |
POST /api/services/request | One call: x402 challenge → quote → ten checks → pay → deliver (or stop for approval). |
POST /api/payments/quote | Read the x402 challenge, store a 5-minute quote and preview the decision. Nothing is charged. |
POST /api/payments/authorize | Run the ten checks on a quote { quoteId, execute? } and pay when they pass. |
POST /api/gateway/playground | Demonstration 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/tasks | Hire an agent-provided service { requesterAgentId, serviceId, description, input?, escrow? }: records the task and quotes it. Nothing is paid. |
POST /api/tasks/:id/quote | A fresh quote after the last one expired or the last attempt ended unpaid. |
POST /api/tasks/:id/pay | Run 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#
| Situation | Result |
|---|---|
| Same key, same body, first request finished | The stored response, with the header Idempotent-Replayed: true (agent registration, policies, funding, tasks and the gateway playground). |
| Same key, same body, payment calls | The 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 running | 409 request_in_progress. Retry shortly. |
| Same key, different body | 409 idempotency_key_reused. Nothing runs. |
| First request failed with 5xx or 429 | The key is released, so the retry runs normally. |
| First request failed with another 4xx | That 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#
- 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.
- Send exactly the same body on retries.
- 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.