# Idempotent requests

Send an `Idempotency-Key` header, 1 to 255 visible ASCII characters, with every `POST`, `PUT` and `DELETE`, so a retry after a timeout never runs twice.

## How it works

A repeat of the same request with the same key within 24 hours returns the first answer without running again, and the answer carries `Idempotent-Replayed: true`. A repeated `DELETE` answers the stored `204`. The same key with a different request is refused with `422` `invalid_request`. A key is per API key.

## Retrying

A retry after a timeout is therefore safe: send the same key again. The official [SDKs](https://developer.watchtower15.com/sdks/) do it for you, for every write method.

## Answers that hold a secret

The rotation of a log source’s token answers the new `ingest_token` once. The same `Idempotency-Key` again answers the stored receipt with the token `null`, because WatchTower15 never keeps a secret for a replay. A lost answer therefore needs a new rotation, with a new key, and the same holds for `metricSources.rotateToken`.

**POST /v1/domains**

curl:

```bash
curl -X POST https://rest.watchtower15.com/v1/domains \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: add-example-com-1" \
  -d '{"name": "example.com"}'
```

PHP:

```php
$wt15 = WatchTower15\Client::fromEnvironment();

// Without one, the SDK sends a random key, and its retries resend it.
$domain = $wt15->domains->create(
    ['name' => 'example.com'],
    ['idempotency_key' => 'add-example-com-1'],
);
$replayed = $wt15->lastResponse()->replayed;
```

Node.js:

```ts
const wt15 = WatchTower15.fromEnvironment();

// Without one, the SDK sends a random key, and its retries resend it.
const { data: domain, response } = await wt15.domains
  .create({ name: 'example.com' }, { idempotencyKey: 'add-example-com-1' })
  .withResponse();
const replayed = response.replayed;
```

Python:

```python
client = Client.from_environment()

# Without one, the SDK sends a random key, and its retries resend it.
raw = client.domains.with_raw_response.create(
    name='example.com',
    request_options={'idempotency_key': 'add-example-com-1'},
)
domain = raw.parse()
replayed = raw.replayed
```

**The same request again (201 Created)**

```http
HTTP/1.1 201 Created
Content-Type: application/json
Idempotent-Replayed: true

{
  "data": { "uuid": "0f8c7a52-3c1e-4b9e-9d2a-6c41e5b7a913", "name": "example.com", … }
}
```

Source: https://developer.watchtower15.com/api/idempotent-requests/
