Skip to main content

Idempotency

Networks fail. When a purchase request times out you cannot tell whether CELITECH received it, and sending it again could buy a second eSIM. Idempotency keys make these retries safe: send the same key with the same request and CELITECH performs the action once, then returns the original result to every repeat.

Using Idempotency Keys​

Generate a unique key (a UUID works well) for each purchase you intend to make, and pass it with the request. Keys are supported on every request that creates or changes something (the API Reference marks them with the Idempotency-Key parameter): creating a purchase (V2 and the original endpoint), topping up an eSIM, editing a purchase, and suspending an eSIM. They are optional: without a key, requests behave exactly as before.

import { Celitech } from 'celitech-sdk';
import { randomUUID } from 'crypto';

(async () => {
const celitech = new Celitech({
clientId: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
});

const createPurchaseV2Request = {
destination: 'FRA',
dataLimitInGb: 1,
duration: 7,
quantity: 1,
};

const idempotencyKey = randomUUID(); // one key per purchase

const { data } = await celitech.purchases.createPurchaseV2(createPurchaseV2Request, {
idempotencyKey,
});
console.log(data);
})();

Calling the REST API directly? Send the same value in an Idempotency-Key request header. See the API Reference.

What Happens When You Retry​

SituationResult
First request with a keyExecuted normally.
Same key and same request, original succeededThe original result is returned (same ICCID and activation details). Nothing is purchased again. Direct REST callers also see the response header Idempotent-Replayed: true.
Same key, original still being processed409 Conflict with a Retry-After header. Wait that many seconds and retry.
Same key, different request422 Unprocessable Entity.
Malformed key400 Bad Request.
Idempotency service temporarily unavailable503 Service Unavailable. The request was not processed; retry it.

Rules for Keys​

  • Use 1 to 255 printable ASCII characters, without spaces. A random UUID is a good choice.
  • Generate one key per purchase you intend to make. Reuse a key only to retry that exact request. A new purchase needs a new key.
  • A request is "the same" when the operation and its parameters match. The order of fields does not matter.
  • Keys belong to your credentials, so other accounts can never collide with yours.
  • Keys are remembered for 3 days. After that, the key can be used again.

Retrying Safely​

Reuse the same key for every attempt of the same purchase, including attempts you make yourself after a timeout, a connection error, or a 5xx/503 response. On a 409, wait the number of seconds in Retry-After first.

note

Only successful responses are remembered. If a request returns an error such as 400, retrying it with the same key runs it again, so you can fix the problem and resend. If you need to be certain that nothing was created by a failed attempt, check your purchases (for example by the referenceId you sent) before starting over with a new key.

tip

A 422 means the same key was used for two different requests. This is almost always a bug in how keys are generated: create a fresh key for each new purchase.