# Idempotency

Retry a call safely. A creating `POST` takes an `Idempotency-Key`; some records also take your own external reference or a path.

## Idempotency-Key

Every `POST` that creates a record or runs a command needs the header `Idempotency-Key`. Make one key per intent and send the same key on each retry of that intent.

*A create with a key*

```bash
KEY=$(uuidgen)   # one key per intent; keep it for the retries
curl -s -X POST "$YOUNITON_API/projects/ORB/tickets" \
  -H "Authorization: Bearer $YOUNITON_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $KEY" \
  --data '{"title": "Secure document links"}'
```

| Situation | Answer |
|---|---|
| no key on an operation that needs one | `400 idempotency.key_required` |
| the same key and the same request, after the first call committed | the original status with `Idempotent-Replayed: true`. Youniton builds the answer again with your current access. |
| the same key with a different request | `422 idempotency.key_reused` |
| the same key while the first call still runs | `409 idempotency.in_flight` with `Retry-After: 1` |
| the first call failed (4xx, 5xx, 401, 428, 429) | nothing is stored; a retry with the same key is decided again |

- A key has 8–128 characters from `A–Z a–z 0–9 . _ : -`. A UUID is a good key.
- A key is scoped to the tenant, the caller and the operation. Two callers with one key make two unrelated requests.
- Youniton keeps a receipt for at least 24 hours after the call. Retry within that time.
- The receipt is saved in the same transaction as the change, so a crash cannot run the command two times.

The reference shows an *Idempotency-Key* chip on each operation that needs it. `PUT` and `DELETE` are idempotent by their nature and take no key.

## External references

A help-desk create can carry your own id, `externalReference` (1–200 characters of `A–Z a–z 0–9 . _ : / # -`). It is unique per connection for ever, so a retry days later still finds the first request.

| Situation | Answer |
|---|---|
| a new reference | `201` with the new request |
| a known reference and the same body | `200` with the existing request and `Idempotent-Replayed: true` |
| a known reference and another body | `409 help_desk_request.external_reference_exists` with the existing `requestKey` |

[`POST /help-desk/requests`](https://docs.youniton.com/api/reference/help-desk.md#createIntakeRequest)

## PUT by path

A memory entry has a path in its project. [`PUT /projects/{projectKey}/memory/entries/by-path`](https://docs.youniton.com/api/reference/memory.md#upsertMemoryEntryByPath) creates the entry at the path or replaces its body. It is safe to repeat:

| Call | Answer | `meta.outcome` |
|---|---|---|
| the path is new | `201` | `created` |
| the same body again | `200` | `unchanged` — nothing changes |
| a new body | `200` | `updated` |

`If-Match` is optional on this call. When you send it, the stored version must match. Read [Write project memory](https://docs.youniton.com/api/guides/project-memory.md).

## Webhooks

A webhook delivery from Youniton keeps one `webhook-id` over every retry. Store the ids that you processed and skip a repeat. Read [Receive Youniton webhooks](https://docs.youniton.com/api/guides/receive-webhooks.md).
