> ## Documentation Index
> Fetch the complete documentation index at: https://partners.usehammock.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotent requests

> Retry a POST safely by sending an idempotency key.

A network failure can leave you unsure whether a request went through. Send an `Idempotency-Key`
header with any `POST`, and you can retry it without doing the work twice.

```bash theme={null}
curl https://api.usehammock.co/payrolls \
  -H "Authorization: Bearer hmk_live_..." \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 9a8b7c6d-5e4f-4a3b-2c1d-0e9f8a7b6c5d" \
  -d '{ ... }'
```

## How it works

* The key is any unique string of up to 255 characters. A UUID works well.
* Hammock saves the status and body of the first response for a key, and returns the same response
  to every retry with that key. That includes `500` errors.
* Keys are kept for 24 hours. After that, a reused key is treated as a new request.
* A response is saved once the request starts running. A request that fails validation, or
  collides with another still in progress, isn't saved and can be retried.

## Mismatches

A key belongs to one request. Reusing it with a different endpoint or different parameters
returns an `idempotency_error` with the code `idempotency_key_reused`, so a bug that recycles keys
can't silently return the wrong response.

Two requests with the same key at the same time don't both run. The second returns `409` with
`idempotency_request_in_progress`.

## Where it applies

Send a key with every `POST`: creating, updating, voiding and rolling a secret. `GET` and
`DELETE` are idempotent already and ignore the header.

That includes employer links and enrollment links. A retry returns the same single-use URL, which is
what you want: you only retry when the first response never reached you, so the URL is still
unused.

The sandbox [test helpers](https://partners.usehammock.co/guides/testing.md) are the one exception. They don't take the header.

## External IDs are a second guard

`external_id` protects you beyond the 24 hours a key lasts. Reporting a payroll whose
`external_id` already exists returns `external_id_taken`, however long ago the first report was.
If you get that error after a timeout, retrieve the payroll with
`GET /payrolls?employer=...&external_id=...` to see what was recorded.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.