Guide

Idempotency keys: how to retry a billing call without charging twice

You send a request to create an invoice or refund a payment and the connection drops. Did it happen? If you retry, do you get two? An idempotency key turns that guess into a guarantee.

5 min read · Last checked

Key points

  • Send a unique key per operation and repeat the exact same request if you are unsure.
  • Build keys from your own stable identifiers, such as an order id, not from a random value made at send time.
  • The same key with a different request is an error, which protects you from reuse bugs.
  • Keys are remembered for a limited time. Do not rely on them for very late retries.

The problem

Networks fail in the middle. A request can arrive and be processed while the response is lost on the way back. A client that retries blindly then creates two invoices, or refunds twice. A client that does not retry may leave an order unbilled.

How TenaBill handles it

Send an Idempotency-Key header with a value unique to the operation, such as a UUID or your own order id. The first request runs and its answer is kept for 24 hours. Repeating the same request with the same key returns the same status and body with an Idempotent-Replayed: true header, and does nothing else. This works on creating an invoice, refunding an invoice, charging a saved payment method and starting a cart checkout.

If you reuse a key for a different request, TenaBill answers 422 with idempotency_key_reused. If the first request is still running, the repeat gets 409 with idempotency_request_in_progress and Retry-After: 1, so wait and send it again. A key is private to your account and can be up to 255 characters.

POST /api/v1/merchant/invoices/<invoice id>/refund
X-TenaBill-Api-Key: <your merchant API key>
Idempotency-Key: refund-order-8841
Content-Type: application/json

{ "amountCents": 2500, "reason": "requested_by_customer" }

Events and credits have their own ids

Reporting metered usage uses an eventId in the body: the same id is recognised and counted once, and the response says when it was a duplicate. Spending prepaid credits takes an idempotencyKey in the body for the same reason. In all three cases the id should come from something that is stable across retries.

What makes a good key

  • It identifies the business operation, for example refund-order-8841, not the attempt.
  • It is the same on every retry of that operation, so build it from data you already have.
  • It never contains sensitive data such as an email address.
  • It is different for a genuinely new operation, even if the amount is the same.

Stripe works the same way, with its own rules

Stripe describes idempotency keys in the same terms: up to 255 characters, saved results returned on retry, an error if the parameters differ, and pruning of keys at least 24 hours old. Where TenaBill and Stripe both sit in a flow, each has its own key space, so keep the two separate.

What to know before you rely on this

  • A 5xx answer or an error on TenaBill’s side is not kept, so repeating the request with the same key runs it again. Check the result with a read call when you must be sure.
  • Requests that use a key are limited to 1 MB.
  • Keys are kept for 24 hours.

Questions people ask

Do I need a key on every call?

Use one on the four calls that move money or create billing records: create invoice, refund, charge a saved method and cart checkout.

Is a key a substitute for checking webhooks?

No. A key makes a retry safe. A paid-invoice webhook is how you learn that payment cleared.

Sources

Keep reading

Try it with test cards