---
title: "Idempotency"
description: "Sending a request twice without doing the thing twice: the Idempotency-Key header on a send, what a repeat answers, how each SDK, the command line and the MCP server pass the key, and which other writes are safe to repeat."
url: https://doorman.fyi/docs/idempotency
markdown: https://doorman.fyi/docs/idempotency.md
area: "Guides"
section: "Conventions"
---

# Idempotency

Sending a request twice without doing the thing twice: the Idempotency-Key header on a send, what a repeat answers, how each SDK, the command line and the MCP server pass the key, and which other writes are safe to repeat.

Source: https://doorman.fyi/docs/idempotency

## Why a key

A request can be handled and its answer lost on the way back. The program that sent it cannot tell that from a request that never arrived, and for most calls it does not matter: reading twice is harmless. Sending mail twice is not. An idempotency key is a name you give one intended send, so that the API can recognize the second attempt as the first one again.

One operation takes the `Idempotency-Key` header: [`POST /v1/inboxes/{inboxId}/send`](https://doorman.fyi/docs/reference/sending.md#inboxes-send). Mail that has left cannot be taken back, so the send is the call that takes a key.

## The header

`Idempotency-Key` is 1 to 255 printable ASCII characters (`^[!-~]{1,255}$`). The API remembers a key for 24 hours, for the inbox it was used on.

curl:
```sh
curl -s -X POST "https://api.doorman.fyi/v1/inboxes/inb_01jc4q7w9k2m5n8p3r6t1v4x7z/send" \
  -H "authorization: Bearer $DOORMAN_API_KEY" \
  -H "idempotency-key: order-2847-reply" \
  -H "content-type: application/json" \
  -d '{
    "threadId": "thr_01jc5a1b2c3d4e5f6g7h8j9k0m",
    "text": "Thanks, confirming for Thursday."
  }'
```

- The same key, the same request: The original answer again, with the header `Idempotent-Replayed: true`. Nothing is sent a second time.
- The same key, a different request: `422 idempotency_key_reused`. A key names one send: a new send needs a new key.
- The same key, while the first attempt is still running: `409 idempotency_in_progress`. Wait a moment and send again.
- The same key, after the first attempt failed: The send is tried again. A failed send releases its key, so the key can be used once the cause is fixed.

| Status | Error | Meaning |
| --- | --- | --- |
| 422 | `idempotency_key_reused` | This Idempotency-Key was already used for a different request. |
| 409 | `idempotency_in_progress` | A request with this Idempotency-Key is still being processed. |

## Choosing a key

- Derive the key from what the send is about: the order and the step, the thread and the turn, the event being answered. The same intention then gives the same key on every attempt, from every process.
- Do not derive it from the time or from a random number made at the moment of the attempt: a retry would make a new key and send again.
- Keys are kept apart by inbox. The same key on two inboxes is two sends.
- After 24 hours a key is forgotten, and the same key sends again. Something that may be retried later than that needs a record of its own that it was done.

## The key on each surface

TypeScript:
```ts
const sent = await client.inboxes.reply("inb_01jc4q7w9k2m5n8p3r6t1v4x7z", "thr_01jc5a1b2c3d4e5f6g7h8j9k0m",
  { text: "Thanks, confirming for Thursday." },
  { idempotencyKey: "order-2847-reply" },
);
if (sent.replayed) console.log("This reply had already been sent.");
```

Python:
```python
sent = client.inboxes.send(
    "inb_01jc4q7w9k2m5n8p3r6t1v4x7z",
    {"threadId": "thr_01jc5a1b2c3d4e5f6g7h8j9k0m", "text": "Thanks, confirming for Thursday."},
    idempotency_key="order-2847-reply",
)
```

Command line:
```sh
doorman inboxes send inb_01jc4q7w9k2m5n8p3r6t1v4x7z --thread-id thr_01jc5a1b2c3d4e5f6g7h8j9k0m \
  --text "Thanks, confirming for Thursday." --idempotency-key order-2847-reply
```

MCP tool call:
```json
{
  "name": "inboxes_send",
  "arguments": {
    "inboxId": "inb_01jc4q7w9k2m5n8p3r6t1v4x7z",
    "threadId": "thr_01jc5a1b2c3d4e5f6g7h8j9k0m",
    "text": "Thanks, confirming for Thursday.",
    "idempotencyKey": "order-2847-reply"
  }
}
```

- TypeScript SDK: `send` and `reply` take `{ idempotencyKey }` as their last argument. The answer carries `replayed`, true when the API answered an earlier send again.
- Python SDK: `send` takes `idempotency_key`. The answer is the API's own JSON, without a `replayed` field.
- Command line: `--idempotency-key`.
- MCP server: The `idempotencyKey` input of `inboxes_send`.
- Agent runtime: `ctx.reply` and `ctx.send` use the id of the event being handled as the key unless you give another, so a handler that runs twice for one event sends once. See [Replies that do not loop](https://doorman.fyi/docs/agent.md#replies).

## The other direction: handling an event once

What the API sends you can arrive more than once too. A webhook delivery that is not acknowledged is retried, the stream replays on reconnect, and a poll can overlap both. Every event has an `event_id` that stays the same across all of them: record the id once the work for it is done, and skip an id you have recorded. [Timeouts, retries and duplicates](https://doorman.fyi/docs/webhooks.md#retries) has the detail, and [the agent runtime](https://doorman.fyi/docs/agent.md#dedupe) does it for you.

## Writes that are safe to repeat anyway

Most writes other than a send need no key, because doing them twice leaves things as doing them once did: setting a value that is already set, deleting what is already gone. The contract marks them, and the MCP server passes the mark to a model as `idempotentHint`.

| Operation | Tool |
| --- | --- |
| [`DELETE /v1/inboxes/{inboxId}/api-keys/{keyId}`](https://doorman.fyi/docs/reference/api-keys.md#inboxes-keys-revoke) | [`inboxes_keys_revoke`](https://doorman.fyi/docs/mcp/tools/api-keys.md#inboxes-keys-revoke) |
| [`PATCH /v1/inboxes/{inboxId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-update) | [`inboxes_update`](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-update) |
| [`DELETE /v1/inboxes/{inboxId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-delete) | [`inboxes_delete`](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-delete) |
| [`PATCH /v1/inboxes/{inboxId}/rules/{ruleId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-update) | [`inboxes_rules_update`](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-update) |
| [`DELETE /v1/inboxes/{inboxId}/rules/{ruleId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-delete) | [`inboxes_rules_delete`](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-delete) |
| [`DELETE /v1/inboxes/{inboxId}/webhook`](https://doorman.fyi/docs/reference/webhooks.md#inboxes-webhook-delete) | [`inboxes_webhook_delete`](https://doorman.fyi/docs/mcp/tools/webhooks.md#inboxes-webhook-delete) |
| [`PATCH /v1/threads/{threadId}`](https://doorman.fyi/docs/reference/mail.md#threads-update) | [`threads_update`](https://doorman.fyi/docs/mcp/tools/mail.md#threads-update) |
| [`DELETE /v1/threads/{threadId}`](https://doorman.fyi/docs/reference/mail.md#threads-trash) | [`threads_trash`](https://doorman.fyi/docs/mcp/tools/mail.md#threads-trash) |
| [`PATCH /v1/webhooks/{endpointId}`](https://doorman.fyi/docs/reference/webhooks.md#webhooks-update) | [`webhooks_update`](https://doorman.fyi/docs/mcp/tools/webhooks.md#webhooks-update) |
| [`DELETE /v1/webhooks/{endpointId}`](https://doorman.fyi/docs/reference/webhooks.md#webhooks-delete) | [`webhooks_delete`](https://doorman.fyi/docs/mcp/tools/webhooks.md#webhooks-delete) |
| [`DELETE /v1/domains/{domainId}`](https://doorman.fyi/docs/reference/domains.md#domains-delete) | [`domains_delete`](https://doorman.fyi/docs/mcp/tools/domains.md#domains-delete) |
| [`PATCH /v1/passport`](https://doorman.fyi/docs/reference/passport.md#passport-update) | [`passport_update`](https://doorman.fyi/docs/mcp/tools/passport.md#passport-update) |
| [`PATCH /v1/services/{serviceId}`](https://doorman.fyi/docs/reference/passport.md#services-update) | [`services_update`](https://doorman.fyi/docs/mcp/tools/passport.md#services-update) |
| [`DELETE /v1/services/{serviceId}`](https://doorman.fyi/docs/reference/passport.md#services-delete) | [`services_delete`](https://doorman.fyi/docs/mcp/tools/passport.md#services-delete) |
| [`PATCH /v1/security`](https://doorman.fyi/docs/reference/security.md#security-update) | [`security_update`](https://doorman.fyi/docs/mcp/tools/security.md#security-update) |
| [`PATCH /v1/inboxes/{inboxId}/security`](https://doorman.fyi/docs/reference/security.md#inboxes-security-update) | [`inboxes_security_update`](https://doorman.fyi/docs/mcp/tools/security.md#inboxes-security-update) |
| [`DELETE /v1/inboxes/{inboxId}/security`](https://doorman.fyi/docs/reference/security.md#inboxes-security-reset) | [`inboxes_security_reset`](https://doorman.fyi/docs/mcp/tools/security.md#inboxes-security-reset) |
| [`DELETE /v1/sender-rules/{senderRuleId}`](https://doorman.fyi/docs/reference/security.md#senderRules-delete) | [`senderRules_delete`](https://doorman.fyi/docs/mcp/tools/security.md#senderRules-delete) |
| [`DELETE /v1/drafts/{heldSendId}`](https://doorman.fyi/docs/reference/sending.md#drafts-delete) | [`drafts_delete`](https://doorman.fyi/docs/mcp/tools/sending.md#drafts-delete) |

> Note: A write that creates something is not among them: creating an inbox, a key, an endpoint or a rule twice makes two. After a create whose answer was lost, list before creating again. An inbox can be found by the `externalId` you gave it.
