Retry writes safely

Last checked:

A network failure can leave you not knowing whether an action ran. For actions that take an Idempotency-Key, retry with the same key and the same body. While the first run is still going, a retry is refused; once it has finished, a retry gets the first run’s answer instead of running the action again. Two limits apply, both described below: a run that takes longer than its claim on the key (5 minutes unless the operation sets another time) can be run again by a retry, and once the key’s answer has expired the same key starts a new run.

Which operations take it

The reference lists the Idempotency-Key header on every operation that takes it. Send it only there.

How to use it

  1. Make a new key per run, for example a UUID. A key is 1–128 characters of A–Z a–z 0–9 . _ : -.
  2. Keep the key with the request until you have the answer.
  3. On a retry, send the same key and the same body.

curl

KEY="$(uuidgen)" # a new key per run; send the same one to retry this run
curl -X POST "$API_BASE/action/add_order/run" \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Idempotency-Key: $KEY" \
  -H "Content-Type: application/json" \
  -d '{"customer_name": "Asha Rao", "note": "Deliver after 5 pm"}'

Node.js

import { randomUUID } from "node:crypto";

const key = randomUUID(); // keep it for every retry of this run
const response = await fetch(`${process.env.API_BASE}/action/add_order/run`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.API_TOKEN}`,
    "Content-Type": "application/json",
    "Idempotency-Key": key,
  },
  body: JSON.stringify({ customer_name: "Asha Rao", note: "Deliver after 5 pm" }),
});
const replayed = response.headers.get("Idempotent-Replayed") === "true";

Python

import os
import uuid
import requests

key = str(uuid.uuid4())  # keep it for every retry of this run
response = requests.post(
    f"{os.environ['API_BASE']}/action/add_order/run",
    json={"customer_name": "Asha Rao", "note": "Deliver after 5 pm"},
    headers={"Authorization": f"Bearer {os.environ['API_TOKEN']}", "Idempotency-Key": key},
    timeout=30,
)
replayed = response.headers.get("Idempotent-Replayed") == "true"

PHP

<?php
$key = bin2hex(random_bytes(16)); // keep it for every retry of this run
$ch = curl_init(getenv('API_BASE') . '/action/add_order/run');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode(['customer_name' => 'Asha Rao', 'note' => 'Deliver after 5 pm']),
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('API_TOKEN'),
        'Content-Type: application/json',
        'Idempotency-Key: ' . $key,
    ],
    CURLOPT_RETURNTRANSFER => true,
]);

The answers

Answer Meaning What to do
200 The run finished. On a retry of a finished run, this is the first run’s answer, with the header Idempotent-Replayed: true. Done.
409 IDEMPOTENCY_IN_PROGRESS The first run with this key is still going. Wait a moment, then retry with the same key.
409 IDEMPOTENCY_RESULT_UNAVAILABLE The run happened, but its answer couldn’t be kept. Don’t run it again: read the record instead.
422 IDEMPOTENCY_KEY_REUSED This key was already used with a different body. Use a new key for a new request.
400 IDEMPOTENCY_KEY_INVALID The key’s format is wrong. Fix the key.

A replayed answer repeats success, records and some of meta. Values that are shown only once, such as a file’s content, are never repeated; meta.replay_withheld names the keys left out.

How long a key lasts

  • While the run is going. The first request holds the key for 5 minutes unless the operation sets another time (between 30 seconds and 1 hour). A retry in that time answers 409 IDEMPOTENCY_IN_PROGRESS. A run still going after that loses its hold, so a retry may then run the action a second time.
  • After the run. A finished run’s answer is kept for 24 hours unless the operation sets another time (between 1 minute and 7 days). After that, the same key starts a new run.
  • A failed run keeps nothing: a retry with the same key runs the action again.

Retry within minutes, not days, and read the records before retrying a run that may have outlasted its hold.