# Idempotency

> Canonical URL: https://docs.mountaya.com/en/apis/conventions/idempotency/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Send an Idempotency-Key with every Map Studio API write, so a retry after a lost answer never makes the write twice.

Every write on the Map Studio API takes an `Idempotency-Key` header. Pick one value per write, a UUID, and send it again unchanged on every retry. The first request runs; a repeat gets the first answer back, and nothing runs twice.

The samples below create a collection, retry it with the same key, and delete it. They read a session token minted for a member from `MOUNTAYA_MEMBER_TOKEN`, as [act as a member](https://docs.mountaya.com/en/authentication.md#acting-as-a-member) shows.

**cURL**

```bash
IDEMPOTENCY_KEY=$(uuidgen)

# Send the write twice with one key, as a retry after a lost answer would.
for attempt in 1 2; do
  RESPONSE=$(curl -s -i -X POST https://api.mountaya.com/v1/collections \
    -H "X-API-Key: $MOUNTAYA_PUBLISHABLE_KEY" \
    -H "X-Session-Token: $MOUNTAYA_MEMBER_TOKEN" \
    -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name": "Docs retries", "is_private": true}')
  echo "$RESPONSE" | grep -iE '^(HTTP|location|idempotent-replayed)'
done

# A delete takes a key of its own.
curl -s -X DELETE "https://api.mountaya.com/v1/collections/$(echo "$RESPONSE" | tail -n 1 | jq -r .data.id)" \
  -H "X-API-Key: $MOUNTAYA_PUBLISHABLE_KEY" \
  -H "X-Session-Token: $MOUNTAYA_MEMBER_TOKEN" \
  -H "Idempotency-Key: $(uuidgen)"
```

**TypeScript**

```typescript
import { Mountaya } from "@mountaya/client";

const mountaya = new Mountaya({
  credential: {
    publishableKey: process.env.MOUNTAYA_PUBLISHABLE_KEY ?? "",
    sessionToken: process.env.MOUNTAYA_MEMBER_TOKEN ?? "",
  },
});

// The client sends a key on every write by itself. Pass your own to retry
// a write after a restart, from a queue for instance.
const idempotencyKey = crypto.randomUUID();
const collection = { is_private: true, name: "Docs retries" };

const first = await mountaya.studio.collections.postCollection({ body: collection, idempotencyKey });
const retry = await mountaya.studio.collections.postCollection({ body: collection, idempotencyKey });

console.log(first.data.id === retry.data.id); // true: one collection

await mountaya.studio.collections.deleteCollection({ collection_id: first.data.id });
```

**Python**

```python
import os
import uuid

import requests

idempotency_key = str(uuid.uuid4())

# Send the write twice with one key, as a retry after a lost answer would.
for attempt in range(2):
    created = requests.post(
        "https://api.mountaya.com/v1/collections",
        headers={
            "X-API-Key": os.environ["MOUNTAYA_PUBLISHABLE_KEY"],
            "X-Session-Token": os.environ["MOUNTAYA_MEMBER_TOKEN"],
            "Idempotency-Key": idempotency_key,
        },
        json={"name": "Docs retries", "is_private": True},
        timeout=30,
    )
    print(created.status_code, created.headers.get("Idempotent-Replayed"))

# A delete takes a key of its own.
requests.delete(
    f"https://api.mountaya.com/v1/collections/{created.json()['data']['id']}",
    headers={
        "X-API-Key": os.environ["MOUNTAYA_PUBLISHABLE_KEY"],
        "X-Session-Token": os.environ["MOUNTAYA_MEMBER_TOKEN"],
        "Idempotency-Key": str(uuid.uuid4()),
    },
    timeout=30,
).raise_for_status()
```

**Response**

```http
HTTP/1.1 201 Created
Location: /v1/collections/e63bf5f0-40d7-445a-9a87-6cc839c66385
HTTP/1.1 201 Created
Idempotent-Replayed: true
Location: /v1/collections/e63bf5f0-40d7-445a-9a87-6cc839c66385
```

The second answer is the first one, replayed: the same status, body, and `Location`, marked `Idempotent-Replayed: true`.

- Every `POST`, `PATCH` and `DELETE` on the Map Studio API takes an `Idempotency-Key` header, except `POST /v1/sessions`, `POST /v1/organizations/lookup`, and the two collaboration signals, `POST /v1/itineraries/{itinerary_id}/collab` and `POST /v1/zones/{zone_id}/collab`, which restate where their sender is and are safe to send again without one.
- A UUID is the usual choice; any 1 to 255 printable ASCII characters without a space are accepted.
- The Tile API and the Data API only read, so they take no key.
- Each operation also states `x-idempotent` in the reference: whether repeating it **without** a key can have a second effect. A key makes a write that takes one safe to repeat, which is why the TypeScript client mints one for every such write and reuses it on every retry.

## What a repeat gets

| You send the key again | The answer |
| --- | --- |
| With a different request | `400 Bad Request` at the `Idempotency-Key` header. A key names one write. |
| With the same request, after the first finished | The first answer, replayed, with `Idempotent-Replayed: true`. |
| With the same request, while the first still runs | `409` coded `IDEMPOTENCY_KEY_IN_USE`, with `Retry-After: 1`. Wait, then resend. |
| After a write whose answer was lost | `409` coded `IDEMPOTENCY_ANSWER_UNAVAILABLE`. The write was made: read the resource instead. |

A key is kept for 12 hours. Never reuse one for a different write.

- With a different request: `400 Bad Request`, at `["request", "headers", "Idempotency-Key"]`, whether or not the first one still runs. A key names one write, and this is checked first.
- With the same request, after the first one finished: the first answer, replayed: its status, its body, its `Location` and `ETag`, with `Idempotent-Replayed: true`. Nothing runs twice, and no limit counts it again. A first answer that cannot be given again is the one exception, below.
- With the same request, while the first one still runs: `409 Conflict`, coded `IDEMPOTENCY_KEY_IN_USE`, with `Retry-After: 1`. Wait the second and send it again.
- With the same request, after a first one whose write was made and whose answer cannot be given again: `409 Conflict`, coded `IDEMPOTENCY_ANSWER_UNAVAILABLE`, on every repeat. The first answer was too large to keep whole, or was a `5xx` answered after the write was made. `extensions.metadata` carries the `status` it was answered with, from `200` to `599`, and its `location` and `etag` where it had them. Read the resource again rather than sending the write once more.
- Two requests are the same when their method, path, query, and body are, and the media type of their `Content-Type` with its `charset`. `If-Match` is not compared, so a write sent again with a fresh tag after a `412` takes a new key: under the old one, it is answered the `412` again.
- A key is held for 12 hours, for the credential that sent it and for that operation. Every answer below `500` other than a `429` is stored, a refusal included, so a retried request that was refused is refused again in the same words. A `429` stores nothing.
- A `5xx` answered before anything was written releases the key, so a retry runs the write again.
- A `5xx` answered after the write was made keeps the key, without the answer. Every repeat is the `409` coded `IDEMPOTENCY_ANSWER_UNAVAILABLE`, whose `status` names the `5xx`: the write landed, so read the resource rather than make it twice. Nine creates name what they made at `location` all the same, so carry on with that one: an organization, an invitation, a publishable key, an itinerary, an itinerary imported from a file, a copy of an itinerary, a zone, a collection and a webhook endpoint. A webhook endpoint made so answered its signing secret in the answer that was lost: an owner or an admin reveals it in the Mountaya app, or you delete the endpoint and create another.
- An answer too large to keep whole is kept the same way, without its body. The TypeScript client raises that `409` as `MountayaIdempotentAnswerUnavailableError` and never asks again; its `answer` carries the first answer's `status`, `location` and `etag`.
- Keys are best-effort: while Mountaya cannot record one, the write runs as if it carried none.
- The key is what makes a retried delete safe to read. A `DELETE` that removed a resource, repeated after the first one landed, finds nothing left and answers `404` (a revoked key and a removed share link answer `200` again). Sent with the first one's key, it replays the first `200` instead.
- In the step order of a request (see [errors](https://docs.mountaya.com/en/apis/conventions/errors.md#which-refusal-you-get-first)), the key is checked after the credential, the body, the product, the scope and the role, and before the limits; none of its answers counts toward a limit.

## Next steps

- [Conditional writes](https://docs.mountaya.com/en/apis/conventions/conditional-writes.md): Write only if nobody changed the resource since you read it.
- [Errors](https://docs.mountaya.com/en/apis/conventions/errors.md): Which failures a retry fixes.
