Idempotency
Retry POST and PATCH requests safely, without duplicating invoices, payments or customers, using the Idempotency-Key header.
Networks fail: a request can reach Billora and be processed even though your client never gets the response (timeout, dropped connection). If you repeat it, you could create two invoices or two payments. The Idempotency-Key header prevents that.
How it works
Send an Idempotency-Key header with a value unique to that operation on any POST or PATCH:
curl https://api.billora.app/api/v1/payments/ \
-H "Authorization: Bearer $BILLORA_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 7c9e6679-7425-40de-944b-e07fc1f90ae7" \
-d '{ "invoice": 5120, "amount": "500.00", "date": "2026-11-15" }'- The first request with that key is processed normally and Billora stores the response status and body.
- If you repeat the same request (same method, path, query string, body and credential) with the same key, you get the stored response and the operation doesn't run again. Replayed responses carry the
Idempotent-Replayed: trueheader.
Rules
| Rule | Detail |
|---|---|
| Methods | POST and PATCH. Ignored on GET, PUT and DELETE. |
| Format | 1 to 255 printable characters (otherwise 400 idempotency_key_invalid). We recommend a UUID v4 or an id from your system (order-2026-1042). |
| Scope | Per organization: the same key in two organizations doesn't collide. |
| Lifetime | Keys are kept for 24 hours. After that, the same key is treated as new. |
| Same key, different request | If the method, path or body changes, the API answers 422 idempotency_key_reused. |
| Request in progress | If the first request is still being processed, the second gets 409 idempotency_key_in_use. Wait and retry with the same key. |
| What is stored | Only successful responses (2xx/3xx), with their status, body and relevant headers (Content-Type, Location). |
4xx and 5xx errors | Not stored, and they release the key: fix the request (or wait) and retry with the same key. |
multipart uploads | The body size is compared instead of its content. |
Use keys with business meaning
If every invoice in your system comes from an order, use something like invoice-order-1042 as key. Even if your process restarts and tries again from scratch, it will never create a second invoice for the same order (within 24 h).
Where it matters
POST /invoices/,/quotes/,/proformas/: creating documents.POST /invoices/{id}/send/: issuing (issuing twice already fails, but with the key you get the original response instead of an error).POST /payments/and/expense-payments/: recording payments.POST /customers/,/suppliers/,/products/,/expenses/.POST /invoices/{id}/email/: avoid sending the same email twice.
Recipe: safe retries
Generate the key once per logical operation and reuse it on every retry of that operation.
import { randomUUID } from "node:crypto";
import { setTimeout as sleep } from "node:timers/promises";
export async function postWithRetry(path, body, { key = randomUUID(), attempts = 5 } = {}) {
for (let attempt = 1; ; attempt++) {
try {
const res = await fetch(`https://api.billora.app/api/v1${path}`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.BILLORA_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": key, // the same on every attempt
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(30_000),
});
const retryable = res.status === 429 || res.status === 409 || res.status >= 500;
if (!retryable || attempt === attempts) return res;
const retryAfter = Number(res.headers.get("Retry-After"));
await sleep(retryAfter ? retryAfter * 1000 : backoff(attempt));
} catch (err) {
if (attempt === attempts) throw err; // network error or timeout: retry with the same key
await sleep(backoff(attempt));
}
}
}
const backoff = (n) => Math.min(30_000, 500 * 2 ** n) * (0.5 + Math.random() / 2);
const res = await postWithRetry("/payments/", { invoice: 5120, amount: "500.00", date: "2026-11-15" }, { key: "cobro-5120-tr-88231" });
console.log(res.status, res.headers.get("Idempotent-Replayed"));import os, random, time, uuid
import requests
def post_with_retry(path, body, key=None, attempts=5):
key = key or str(uuid.uuid4()) # the same on every attempt
headers = {
"Authorization": f"Bearer {os.environ['BILLORA_API_KEY']}",
"Idempotency-Key": key,
}
for attempt in range(1, attempts + 1):
try:
res = requests.post(f"https://api.billora.app/api/v1{path}", json=body,
headers=headers, timeout=30)
except requests.RequestException:
if attempt == attempts:
raise
time.sleep(backoff(attempt))
continue
retryable = res.status_code in (409, 429) or res.status_code >= 500
if not retryable or attempt == attempts:
return res
time.sleep(int(res.headers.get("Retry-After", 0)) or backoff(attempt))
def backoff(n):
return min(30, 0.5 * 2 ** n) * random.uniform(0.5, 1)
res = post_with_retry("/payments/", {"invoice": 5120, "amount": "500.00", "date": "2026-11-15"},
key="cobro-5120-tr-88231")
print(res.status_code, res.headers.get("Idempotent-Replayed"))For brevity the example retries any 409. In production, read the body code and retry only idempotency_key_in_use: a business 409 conflict won't be fixed by retrying.
