# `Ankusa.SDK.Idempotency`
[🔗](https://github.com/jamescarr/ankusa/blob/sdk-elixir-v0.6.0/packages/sdk-elixir/lib/ankusa/sdk/idempotency.ex#L1)

The idempotency key for one delivery, whichever transport delivered it.

Delivery is at-least-once: a provider retry, a sink retry, or a requeued
message can hand the same hook over more than once. Ankusa computes one
tenant-scoped key per hook and ships it (the message's `idempotency_key`, the
`x-ankusa-idempotency-key` header); this module returns that value. Read it,
don't rebuild it.

For a message or delivery from a node that predates the field, the key is
computed from the hook's own fields, by the same rule core uses:

    tenant <> ":" <> source_id <> ":" <> dedupe_key   when dedupe_key is non-null and non-empty
    id                                                 otherwise

where `tenant` is `"default"` when there is none. A replay of an older
delivery carries a `replay_id`. The key ignores it by default, so a consumer
that already processed the original drops the replay; a consumer that must
reprocess replays passes `include_replay: true`, which appends
`"#replay:" <> replay_id`.

Accepts the decoded `Ankusa.SDK.Message`, the parsed
`Ankusa.SDK.Webhook.Headers` from an HTTP delivery, or an
`Ankusa.SDK.Hook` (where `source_id` already holds the message's
`source_id`/the header's `source`).

```elixir
{:ok, message} = Ankusa.SDK.Message.decode(body)
key = Ankusa.SDK.Idempotency.key(message)
key = Ankusa.SDK.Idempotency.key(message, include_replay: true)
```

# `key`

```elixir
@spec key(
  Ankusa.SDK.Message.t() | Ankusa.SDK.Webhook.Headers.t() | Ankusa.SDK.Hook.t(),
  keyword()
) ::
  String.t()
```

Compute the idempotency key for `message_or_headers`.

Options: `:include_replay` (default `false`) appends the replay marker when
the delivery carries a `replay_id`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
