Retries
Send a request again safely, so a person never gets the same message twice
A connection drops, a 503 comes back, your process restarts mid-send. Send the
request again with the same Idempotency-Key, and Mapier answers with the first
command instead of sending a second one.
| What happened | Send again with |
|---|---|
| No answer, a dropped connection, a restart | The same key |
409 conflict, idempotency_key_in_flight | The same key, after a short wait |
429, or any other 503 | The same key, after the wait it gives |
A message.send refused with 503 upstream_unavailable | A new key: that key is spent |
A result you saw: failed or expired | A new key, once you fix the cause |
A result you saw: ambiguous | Nothing yet. Check the conversation first |
400 or 422 | Nothing. Fix the request |
Send with a key
Make the key from what you are doing, so the same work always gives the same
key: order-4821-photo, ticket-1043-first-reply. A random key made at each
attempt protects nothing.
curl -X POST https://api.mapier.ai/v1/commands/message.send \
-H "Authorization: Bearer $MAPIER_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-8842-confirmation" \
-H "x-mapier-version: 2026-12-01" \
-d '{
"target": { "conversationId": "a0000000-0000-4000-8000-000000000001" },
"payload": { "kind": "text", "text": "Your order is on its way." }
}'In code, the client below sends with a key and retries safely.
A key is any string of up to 200 characters: no spaces at either end. It belongs to one project. If you send no key, Mapier makes one for that request alone, so a retry without a key is a new message.
{
"commandId": "0192b3e1-8a4c-7d2e-9f10-3c5b7a9d1e42",
"status": "queued",
"repeated": true,
"lineId": "0192a7b0-3e4f-7051-8c6d-7e8f9a0b1c2d",
"lineSelection": "reused_active_chat",
"channel": "imessage"
}repeated: true means the key matched an earlier send. commandId is the first
send's, and nothing new was queued. The result is still the first command's
command event; reconnect with Last-Event-ID if you missed it.
Every command honors the key on 2026-12-01, the version these pages use.
Older versions lists what differs before it.
When a key is refused
When Mapier cannot answer with the first command, a repeat is refused with
409 conflict, and nothing is sent. details.reason says why:
details.reason | Means | What to do |
|---|---|---|
idempotency_key_in_flight | The first request with this key is still running | Wait, then send again with the same key |
idempotency_key_failed | The first send was refused and nothing went out | Send again with a new key. If details.sentParts lists parts, send only the rest |
idempotency_key_unresolved | Mapier could not confirm whether the first send went out | Check the conversation first, or the person may get it twice |
idempotency_key_reused | The key already named a different command | Use a new key |
Send again after a result
A key stays with its command for good. Once you have seen a failed or
expired result and want the message sent, that is a new command:
- Fix what
failureClassnamed. - Make a new key from the old one, such as
order-8842-confirmation-2. - Send with the new key.
Only make a new key for a result you have seen. For a 202 you lost track of, or
an ambiguous result, a new key can send the message twice.
A chat_background.set and a chat_background.remove need a key each: one key
reused across a set and a remove is answered with the first command and changes
nothing more. The same goes for two different messages. Give each its own key.
A client that retries for you
This client reads each refusal and waits as long as Retry-After says. It tries
a 500 once more, waits out a 409 idempotency_key_in_flight, and
caps every loop at five retries. It sends the same Idempotency-Key on every attempt, so a
repeat answers with the first command. It does not retry a message.send
refused with upstream_unavailable: that key is spent, so the error goes back
to the caller, and sending again needs a new key.
const BASE = "https://api.mapier.ai";
export class MapierError extends Error {
constructor(
readonly status: number,
readonly code: string
) {
super(`${status} ${code}`);
}
}
export async function command(type: string, body: unknown, idempotencyKey: string) {
let serverErrorRetries = 0;
for (let attempt = 0; ; attempt++) {
const res = await fetch(`${BASE}/v1/commands/${type}`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.MAPIER_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": idempotencyKey,
"x-mapier-version": "2026-12-01",
},
body: JSON.stringify(body),
});
if (res.status === 202) return res.json(); // queued, NOT delivered
const answer = (await res.json().catch(() => ({}))) as {
error?: { code?: string; details?: { retryAfter?: number; reason?: string } };
};
const code = answer.error?.code ?? "unknown";
// A 429 and a 503 service_busy both say how long to wait. Wait that long,
// then send the same request.
if ((res.status === 429 || code === "service_busy") && attempt < 5) {
const wait = Number(res.headers.get("retry-after") ?? answer.error?.details?.retryAfter ?? 1);
await sleep(wait * 1000);
continue;
}
// A message.send the line did not take is recorded against this key: the
// key is spent, and a retry under it sends nothing. Hand it back — sending
// again needs a new key.
const spent = type === "message.send" && code === "upstream_unavailable";
// A 409 whose reason is idempotency_key_in_flight: the first attempt under
// this key is still running. Wait, then send it again under the same key.
// The other 409 reasons have no first command to return; hand them back.
const inFlight =
res.status === 409 && answer.error?.details?.reason === "idempotency_key_in_flight";
// Any other 503 is transient; a 500 gets exactly one retry.
const transient =
!spent &&
(res.status === 503 || inFlight || (res.status === 500 && serverErrorRetries++ < 1));
if (transient && attempt < 5) {
await sleep(Math.min(2 ** attempt * 500, 8_000));
continue;
}
throw new MapierError(res.status, code);
}
}
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));Webhook retries
When your endpoint does not answer, Mapier tries again for just under three
days. Each attempt has the same event id, so store events by id and skip one
you already have. Receive events
has the schedule.