Books by finzkoDevelopers

Idempotency

Safe retries for writes. Repeating the same key within 24 hours replays the original response — even across crashes, network blips, or webhook re-deliveries. Always send one on mutating calls; on 13 routes the API refuses the request without it.

Required on these 13 routes

These endpoints reject a request without an Idempotency-Key header with 400 idempotency.key_required:

  • POST /invoices
  • POST /bills
  • POST /payments and POST /payments/{payment_id}/apply
  • POST /credit_notes, POST /credit_notes/{credit_note_id}/apply-to-bill and .../apply-to-invoice
  • POST /debit_notes
  • POST /bank_statements/upload
  • POST /ai/documents
  • POST /backups/restore
  • POST /partner/organizations and POST /provision/organization

On every other POST, PUT, PATCH and DELETE the header is optional but still honoured: when you send one, the response is cached and replayed on a retry exactly as above. Without it, a retry runs the operation again.

How to use it

Generate a unique key per logical operation and send it in the Idempotency-Key header. The server caches the response keyed on (idempotency_key, method, path, auth_subject) so the same key is safe to reuse across resources without collisions.

POST /api/public/v1/invoices
Authorization: Bearer aibk_pat_…
Idempotency-Key: bf3e1b08-9a4d-4f12-b9d3-8c8a52d1fa01
Content-Type: application/json

{
  "customer_id":     "ct_acme",
  "date":            "2026-05-19",
  "place_of_supply": "IN-KA",
  "line_items":      [{ "item_id": "it_widget", "quantity": 2, "rate": 1000 }]
}

If the request times out and you retry with the same key, the server returns the original response — same status, same body — without creating a duplicate.

Response caching rules

  • 2xx / 3xx / 4xx: cached for 24 hours. Retries replay the same status and body, with an extra Idempotency-Replay: true response header. A cached 4xx means: if your first attempt failed validation, fix the request and send it with a new key, or you will get the same error back.
  • 5xx: NOT cached. Server errors must run for real on retry.
  • Concurrent in-flight requests with the same key: the second caller gets 409 idempotency.in_flight immediately, with Retry-After: 2. Back off and retry once the first completes. That 409 is not cached.
  • Same key, different body: the cache key is built from the key, method, path and your token. The request body is not compared, so a reused key with a changed body replays the first response instead of being rejected.
  • Query string: not part of the cache key. If one token works across organizations (?organization_id=), use a different key per organization.

Error codes

StatusCodeWhen
400idempotency.key_requiredA route in the list above was called without the header (or with a blank one).
400idempotency.invalid_keyThe key is longer than 200 characters or contains anything other than letters, digits, -, _, :.
409idempotency.in_flightAnother request with the same key is still running. Retry after the Retry-After seconds.
409conflict.idempotency_key_reusedListed in the error catalog as “same key reused with a different body”. The idempotency layer does not emit it (see above). It is also the fallback code for a 409 raised with a plain-text message, so you can see it on an unrelated conflict.

The 400 idempotency.invalid_key and 409 idempotency.in_flight responses are flat { code, message } bodies without a request_id.

Key format

  • 1 – 200 characters
  • Alphanumeric plus -, _, or : (Stripe-style customer:create:281832 is fine)
  • UUID v4 is a good default

TTL

Keys live for 24 hours. Retry within that window and you'll hit the cached response. After 24 hours the key is forgotten — sending the same key again will create a new resource.

When to use it

Always, for every POST/PUT/PATCH/DELETE that mutates state, whether or not the route requires it. Especially on:

  • Background jobs that retry on failure (cron, Celery, BullMQ, etc.)
  • Mobile clients with flaky networks
  • Bulk imports where one row may fail mid-batch
  • Webhook receivers — payment-provider webhooks are deliberately re-delivered

Server-side safety nets

We layer a few server-side checks on top so a retry with a freshkey doesn't silently produce duplicates. These are safety nets, not contracts — always reuse the same key on a retry.

Payments — duplicate guard

POST /payments rejects a request that matches a recent payment on all four of:

  • contact_id
  • date (within a 7-day look-back window)
  • amount
  • reference_number (non-empty; with no reference number the guard does not run)

Response is 409 validation.duplicate_resource with the existing payment in details. A different Idempotency-Key does not bypass this guard: it runs for every new key. To record a legitimate same-amount payment from the same customer, change the reference_number, or turn the guard off for the organization with the strictPaymentDedupe: false org preference. Default is on.

Example rejection:

HTTP/1.1 409 Conflict

{
  "code": "validation.duplicate_resource",
  "message": "A payment with the same contact, date, amount, and reference_number already exists (e8bf7b71-…). To allow a duplicate, change the reference_number or set the org preference strictPaymentDedupe = false.",
  "details": {
    "existing_payment_id": "e8bf7b71-7437-4248-9b3e-cbb4ec48e945",
    "existing_date":       "2026-05-19T00:00:00",
    "existing_amount":     11800.00,
    "existing_reference":  "UTR-25051209"
  }
}

Invoices — duplicate number and reference

POST /invoices runs two checks, both switched off by the strictInvoiceDedupe: false org preference (default on):

  • An explicit invoice_number that already exists in the branch returns 409 validation.duplicate_resource.
  • With an auto-generated number, a reference_number that matches an invoice for the same customer within ±7 days of the invoice date returns 409 validation.duplicate_resource.

(CGST Rule 46 also keeps (branch, number) unique at the database level, which is the final backstop.)

Recommended client pattern

const idemKey = crypto.randomUUID();

for (let attempt = 0; attempt < 3; attempt++) {
  try {
    const res = await fetch("https://api.books.finzko.com/api/public/v1/payments", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${pat}`,
        "Content-Type": "application/json",
        "Idempotency-Key": idemKey,           // ← same key across retries
      },
      body: JSON.stringify(payload),
    });
    if (res.status < 500) return res;          // 2xx/3xx/4xx → final
    await sleep(1000 * 2 ** attempt);          // 5xx → exponential backoff
  } catch (networkErr) {
    await sleep(1000 * 2 ** attempt);
  }
}