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:
| Call | Endpoint |
|---|---|
| Create a payment | POST /api/v1/checkout/payments |
| Request a refund | POST /api/v1/checkout/payments/{paymentId}/refunds |
| Send a withdrawal | POST /api/v1/checkout/withdrawals |
| Request an on-demand payout | POST /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 send | BillPay answers |
|---|---|
| A new key | 201, and the payment is created. |
| The same key with the same body | The original payment, with the message Payment already created. Nothing new is created. Treat it like a normal success. |
| The same key with a different body | 409. The key is already used for something else. |
| The same key while the first request is still running | 409. Wait a moment and retry. |
Choosing a key
Use an ID from your own system, so a retry always sends the same key:
| Call | Good key |
|---|---|
| Create a payment | Your order ID, such as order-10231 |
| Refund | refund-order-10231-1 |
| Withdrawal | Your batch ID, such as payroll-2026-10 |
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 happened | Retry? |
|---|---|
| Timeout or network error | Yes, with the same key and body. |
429, 503, or 504 | Yes, with the same key. For 429, wait for Retry-After. |
Any other 4xx | No. Fix the request first. Sending it unchanged gives the same error. |
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-Keyprotects one request. It stops a retry from being processed twice.merchantReferenceidentifies one payment, forever. Use it to look the payment up later. Reusing it on another payment returns409.
For a payment, using your order ID for both is the simplest choice.