remagicaDocs home

Idempotency

Retry requests safely with the Idempotency-Key header, without paying twice.

A request can time out after the server got it. If you retry a generation blindly, you might start it twice and pay twice. Send an Idempotency-Key header and retries are safe.

curl https://api.remagica.com/api/v1/generations/images \
  -H "x-api-key: $REMAGICA_API_KEY" \
  -H "Idempotency-Key: 5f0c7e1a-2b8d-4c3e-9a61-7d2f4b8e0c19" \
  -H "Content-Type: application/json" \
  -d '{ "model": "google/nano-banana-pro", "prompt": "A red kite over the sea" }'

Where it works

Every POST that spends credits or starts work takes the header:

  • Generations: POST /generations/images, /videos, /music, /speech, /sound-effects, /background-removals, and POST /characters.
  • Clips: POST /clip-jobs and POST /clips/{clipId}/exports.
  • Runs: POST /workflows/{workflowId}/runs and POST /apps/{appId}/runs.
  • Uploads: POST /uploads and POST /uploads/from-url.
  • Brand kits: POST /brand-kits/from-website and POST /brand-kits/from-file.

How it behaves

You sendYou get
No headerA normal request.
A new keyA normal request. The key and its response are kept for 24 hours.
The same key and the same body againThe first response, replayed. Nothing new runs and nothing is charged.
The same key with a different body or endpoint409 CONFLICT: "This Idempotency-Key was already used with a different request".
The same key while the first request is still being handled409 CONFLICT: "A request with this Idempotency-Key is still in progress". Wait, then retry.
A key that isn't 1–255 printable ASCII characters400 BAD_REQUEST.
  • Keys belong to your workspace. Two workspaces can use the same key without clashing.
  • If a request fails with an error, its key is freed, so you can retry with the same key.
  • After 24 hours a key can be used again for a new request.

Choosing keys

Use a new random value, such as a UUID, for each thing you mean to do, and reuse it only when retrying that same thing:

const key = crypto.randomUUID();

async function createImage(body: object) {
  for (let attempt = 0; attempt < 3; attempt++) {
    const response = await fetch(
      "https://api.remagica.com/api/v1/generations/images",
      {
        body: JSON.stringify(body),
        headers: {
          "Content-Type": "application/json",
          "Idempotency-Key": key,
          "x-api-key": process.env.REMAGICA_API_KEY,
        },
        method: "POST",
      }
    );
    if (response.status !== 409 && response.status < 500) {
      return response.json();
    }
    await new Promise((resolve) => setTimeout(resolve, 1000 * 2 ** attempt));
  }
  throw new Error("Couldn't create the image");
}

The generation and workflow run bodies also take an idempotencyKey field. It still works, but prefer the header, which covers every endpoint above.

On this page