Skip to main content

Idempotency

If a request times out, you can't tell whether BillPay received it. An Idempotency-Key makes it safe to send the request again: BillPay recognizes the key and won't move the money twice.

Where it's required​

The Idempotency-Key header is required on every call that moves money:

CallEndpoint
Create a paymentPOST /api/v1/checkout/payments
Request a refundPOST /api/v1/checkout/payments/{paymentId}/refunds
Send a withdrawalPOST /api/v1/checkout/withdrawals
Request an on-demand payoutPOST /api/v1/checkout/settlements/on-demand

The key can be 1 to 255 printable characters, with no spaces. If it's missing or invalid, you get 400.

What BillPay does with the key​

You sendBillPay answers
A new key201, and the payment is created.
The same key with the same bodyThe original payment, with the message Payment already created. Nothing new is created. Treat it like a normal success.
The same key with a different body409. The key is already used for something else.
The same key while the first request is still running409. Wait a moment and retry.

Choosing a key​

Use an ID from your own system, so a retry always sends the same key:

CallGood key
Create a paymentYour order ID, such as order-10231
Refundrefund-order-10231-1
WithdrawalYour batch ID, such as payroll-2026-10
warning

Don't generate a new random key on every attempt. A new key means a new request, so a retry could create a second payment.

If a payment ends EXPIRED or FAILED and the customer tries again, that's a new payment. It needs a new key and a new merchantReference, such as order-10231-2.

When to retry​

What happenedRetry?
Timeout or network errorYes, with the same key and body.
429, 503, or 504Yes, with the same key. For 429, wait for Retry-After.
Any other 4xxNo. Fix the request first. Sending it unchanged gives the same error.
Node.js
const RETRYABLE = [429, 503, 504];
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function postWithRetry(path, body, idempotencyKey, maxAttempts = 4) {
for (let attempt = 1; ; attempt++) {
try {
const res = await fetch(`${process.env.BILLPAY_BASE_URL}${path}`, {
method: 'POST',
headers: {
'x-api-key': process.env.BILLPAY_API_KEY,
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey, // the same key on every attempt
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(15_000),
});

if (!RETRYABLE.includes(res.status) || attempt === maxAttempts) {
return res;
}

const retryAfter = Number(res.headers.get('Retry-After'));
await sleep((retryAfter || 2 ** attempt) * 1000);
} catch (err) {
// Timeout or network error: BillPay may or may not have the request
if (attempt === maxAttempts) throw err;
await sleep(2 ** attempt * 1000);
}
}
}

// Usage
const res = await postWithRetry('/api/v1/checkout/payments', paymentBody, order.id);

Idempotency key vs. merchant reference​

They look similar, but they do different jobs:

  • Idempotency-Key protects one request. It stops a retry from being processed twice.
  • merchantReference identifies one payment, forever. Use it to look the payment up later. Reusing it on another payment returns 409.

For a payment, using your order ID for both is the simplest choice.