# Idempotency and retries

> How to make Sofia Space API creates safe to retry with the Idempotency-Key header, which endpoints honour it, how long a key lasts, and what a replayed answer looks like.

Source: https://docs.sofiaspace.uz/api/idempotency/

A network hiccup can leave you not knowing whether a create went through. If you simply send it again, you may get two tasks. The **`Idempotency-Key`** header prevents that: send the same key with the retry, and Sofia Space answers with the first request's result instead of creating a second one.

**Who this is for:** developers whose integration creates tasks, pages, projects or spaces — for example a CRM that opens a task for every won deal.

## Send a key with a create

Pick one key per **intent** — "create the task for deal 1042" — and reuse it for every retry of that intent. A UUID works, or a key built from your own record, such as `crm-deal-1042-onboarding`.

```bash
curl -X POST https://acme.sofiaspace.uz/api/v1/projects/9d2e7a10-4b6c-4e8f-a1d3-5c7e9b2f4a68/tasks \
  -H "Authorization: Bearer $SOFIA_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: crm-deal-1042-onboarding" \
  -d '{"taskTypeId":"1a3c5e7f-9b2d-4f6a-8c1e-3d5f7a9b2c4e","title":"Onboard new customer (deal #1042)"}'
```

- The first request with a key runs normally.
- A repeat with the same key, by the same token owner, to the same path, within **10 minutes**, gets the **first answer** again — same status, same body — with the header `Idempotency-Replayed: true`. Nothing new is created.
- The body isn't compared: a repeat with the same key and path but a different body still gets the first answer. Use a new key for a new intent.
- A repeat that arrives while the first is still running waits for it (up to about 8 seconds) and then gets its answer. If the first is still running after that, the repeat gets `409` with `{"code":"REQUEST_IN_PROGRESS","message":"The same request is still being processed"}` — retry a second later.
- Only **successful** (`2xx`) answers are remembered. If the first request failed (a `400`, `403`, `409`, `429` or `500`), the key is freed and a retry runs for real.
- A replayed answer doesn't count against the per-action caps (see [Rate limits](https://docs.sofiaspace.uz/api/rate-limits.md#per-action-caps)).

Key format: 8–128 characters from `A–Z a–z 0–9 . _ : -`. A key in any other format is ignored, as if you sent none.

## Endpoints that honour the key

All of them are `POST` creates:

| Endpoint | Creates |
| --- | --- |
| `POST /api/v1/projects/{projectId}/tasks` | A task |
| `POST /api/v1/orgs/{orgId}/projects/by-key/{projectKey}/tasks` | A task, [by project key](https://docs.sofiaspace.uz/api/integrations.md#create-a-task-by-project-key) |
| `POST /api/v1/spaces/{spaceId}/pages` | A page |
| `POST /api/v1/orgs/{orgId}/projects` | A project |
| `POST /api/v1/orgs/{orgId}/spaces` | A space |

The key also works when creating calendar events and dashboards, which this reference doesn't cover yet.

On any other endpoint the header is ignored. A key is remembered for 10 minutes; for retries that stay safe for good on task creates, also send an `externalRef` naming your record: if a task you can open already has that record, you get that task back with `200` and the header `X-Sofia-Existing: true` instead of a second task. Most other writes are naturally safe to repeat: setting a status, a parent, a field value or a label twice leaves the same result. **Comments and worklogs are not** — a retried comment posts twice. For those, check before retrying (for example list the task's comments) if the first attempt's outcome is unknown.

## Keep your own link too

For an integration that syncs records, also store the link between the two: keep the Sofia Space ID you got back next to your record (for example the task `id` next to the CRM deal), or put your record on the task as a [linked record](https://docs.sofiaspace.uz/api/integrations.md) and look it up later. The idempotency key protects you for 10 minutes; your stored link protects you for good — a nightly job can then update the existing task instead of creating a new one.

## What you can't do

- You can't look up the result of a key after 10 minutes; the key is forgotten.
- A key is per token owner: tokens of two different people (or of a person and an integration account) with the same key create two things. Two tokens of the same owner share keys.
- You can't use a key to make comments, worklogs or uploads idempotent.
- An answer bigger than about 1 MB isn't remembered; a repeat then runs again.

## Related

- [Errors](https://docs.sofiaspace.uz/api/errors.md#retrying-safely) — which errors to retry
- [Tasks](https://docs.sofiaspace.uz/api/tasks.md#create-a-task)
- [Integration examples](https://docs.sofiaspace.uz/api/examples.md)
