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

The headers every receiver of Ankusa's HTTP sink needs, parsed off a request.

`Ankusa.Sink.Http` sends these headers; the hook id is the only one that is
always there:

| Header | Field | Default |
| --- | --- | --- |
| `x-ankusa-id` | `id` | required — its absence means the request isn't a delivery |
| `x-ankusa-source` | `source` | `""` |
| `x-ankusa-tenant` | `tenant` | `nil` |
| `content-type` | `content_type` | `nil` |
| `x-ankusa-dedupe-key` | `dedupe_key` | `nil` (also when empty) |
| `x-ankusa-replay-id` | `replay_id` | `nil` (also when empty) |
| `x-ankusa-idempotency-key` | `idempotency_key` | `nil` (also when empty) |
| `x-ankusa-received-at` | `received_at` | `nil` (absent, empty, or not 1 to 15 decimal digits); unix milliseconds the edge accepted the hook |
| `x-ankusa-body-sha256` | `body_sha256` | `nil` (absent, or not exactly 64 lowercase hex characters); the SHA-256 of the body sent |

The parsed struct also carries every request header in `headers`
(lowercased), so a receiver can read the provider's own headers the sink
forwarded.

Names are matched case-insensitively, so anything from a Plug `req_headers`
list to a hand-built map works. In a list the *first* occurrence of a name
wins, matching HTTP's rule for repeated headers.

`receive_delivery/3` is the receiver-side decision on top of them: it checks
the delivery's signature (HTTP sinks are signed by default) and then parses
the headers. `Ankusa.SDK.Receiver` is a Plug around it.

# `parse_headers`

```elixir
@spec parse_headers(term()) ::
  {:ok, Ankusa.SDK.Webhook.Headers.t()}
  | {:error, Ankusa.SDK.MissingHookIdError.t()}
```

Parse the Ankusa headers out of `headers`, a map or a list of
`{name, value}` pairs (`conn.req_headers` is already the latter).

Returns `{:error, %Ankusa.SDK.MissingHookIdError{}}` when `x-ankusa-id` is
absent or empty.

# `receive_delivery`

```elixir
@spec receive_delivery(term(), binary(), keyword()) ::
  {:ok, %{headers: Ankusa.SDK.Webhook.Headers.t(), verified: boolean()}}
  | {:error,
     Ankusa.SDK.InvalidSignatureError.t() | Ankusa.SDK.MissingHookIdError.t()}
```

Decide whether to accept one HTTP-sink delivery, and parse its headers.

`Ankusa.Sink.Http` signs every delivery unless it is configured
`allow_unsigned: true`, so a receiver is closed unless it is told
otherwise. Options:

  * `:secrets` — the sink's secret (`whsec_` + base64, or any other string
    used as its own bytes), or a list of them during a rotation. With any,
    the delivery MUST verify (`Ankusa.SDK.Signature.verify/4`, same errors,
    `:tolerance_seconds` and `:now`); `:allow_unsigned` does not weaken it.
  * `:allow_unsigned` — `true` accepts a delivery with no secret configured,
    unverified. Default `false`: with no `:secrets` every delivery is
    refused with an `invalid_secret` `Ankusa.SDK.InvalidSignatureError`.
  * `:tolerance_seconds`, `:now` — see `Ankusa.SDK.Signature.verify/4`.

Returns `{:ok, %{headers: headers, verified: verified?}}` (`verified` is
`false` only for an allowed unsigned delivery),
`{:error, %Ankusa.SDK.InvalidSignatureError{}}`
(answer `401`), or `{:error, %Ankusa.SDK.MissingHookIdError{}}` (`400`). The
signature is judged first, so a delivery that fails it is a `401` even when
it also lacks `x-ankusa-id`. `body` must be the raw bytes received.

---

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