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

The wire format every queue-style sink publishes (`Ankusa.Sink.RabbitMQ`,
`Ankusa.Sink.Kafka`, `Ankusa.Sink.NATS`, `Ankusa.Sink.Redis`), and the bridge
from one of those messages to an `Ankusa.SDK.Hook`.

The SDK ships **no** broker client: the app brings its own Broadway/AMQP/
brod/gnat/Redix consumer, hands each message body to `decode/1`, and calls
`to_hook/2` — which redeems a claim check when the message carries one — before
running the same handler the HTTP receiver runs.

A body of at most the sink's `inline_max_bytes` (default 64 KiB) rides inline,
base64-encoded, and a larger body travels as a claim ref:

    {"v": 1, "id": "01a0...", "source_id": "stripe", "tenant_id": "acme",
     "received_at": 1737500000000, "content_type": "application/json", "size": 245,
     "sha256": "2cf24dba5fb0a30e...", "dedupe_key": "evt_1", "replay_id": null,
     "idempotency_key": "acme:stripe:evt_1",
     "headers": {"x-github-event": "push"}, "body_base64": "eyJpZCI6..."}

    {"v": 1, "id": "01a0...", "source_id": "stripe", "tenant_id": "acme",
     "received_at": 1737500000000, "content_type": "application/json", "size": 3145728,
     "sha256": "3bea8a9a07c1e8dc...", "dedupe_key": null, "replay_id": null,
     "idempotency_key": "01a0...",
     "headers": {}, "claim": "urn:ankusa:claim:v1:acme:01M39VMD8RA3C5HR4RBV67Y002"}

`sha256` is the lowercase hex SHA-256 of the body; `dedupe_key` is the
provider's event key when the source extracted one; `replay_id` names the
replay job when this delivery is a replay of an older one; `idempotency_key`
is the tenant-scoped key Ankusa computed for the hook (read it through
`Ankusa.SDK.Idempotency.key/2`); `headers` are the
provider request headers the sink forwarded (lowercased, `""` joined).

`v` is the wire format's major version: every `v` from
`min_supported_version/0` to `max_supported_version/0` (1 and 2) decodes
into this one struct, with `v` as the wire said it. A `v: 2` message is a
`v: 1` message plus additive fields, so consumers must ignore keys they
don't know — `decode/1` accepts a message with extra keys and rejects one
whose `v` it does not speak (`unsupported_version`, permanent: dead-letter
it, never retry). `instance` and `region` name the Ankusa instance that
published the message and the region it is configured with (`nil` when the
message says `null` or predates the fields).

## Integrity

`decode/1` checks what it can without the claim store: an inline body's
length against `size` and its bytes against `sha256`, and a claim ref's tenant
against `tenant_id`. A failure is an `Ankusa.SDK.InvalidMessageError` with a
`code` (`invalid_json`, `not_an_object`, `unsupported_version`,
`invalid_field`, `ambiguous_body`, `missing_body`, `invalid_body_base64`,
`size_mismatch`, `integrity`, `tenant_mismatch`) and, for `invalid_field`, the
offending `field`.

Kafka/NATS headers and the RabbitMQ routing key are not read: the JSON body
carries everything.

# `t`

```elixir
@type t() :: %Ankusa.SDK.Message{
  body: binary() | nil,
  claim: String.t() | nil,
  content_type: String.t() | nil,
  dedupe_key: String.t() | nil,
  headers: %{required(String.t()) =&gt; String.t()},
  id: String.t(),
  idempotency_key: String.t() | nil,
  instance: String.t() | nil,
  received_at: integer() | nil,
  region: String.t() | nil,
  replay_id: String.t() | nil,
  sha256: String.t() | nil,
  size: non_neg_integer(),
  source_id: String.t() | nil,
  tenant_id: String.t() | nil,
  traceparent: String.t() | nil,
  tracestate: String.t() | nil,
  v: pos_integer(),
  verification: %{status: String.t(), flagged: boolean()} | nil
}
```

# `decode`

```elixir
@spec decode(binary()) :: {:ok, t()} | {:error, Ankusa.SDK.InvalidMessageError.t()}
```

Decode one message body.

Returns `{:error, %Ankusa.SDK.InvalidMessageError{}}` for anything that isn't
a valid v1 message; `error.code` and `error.field` name exactly what was
wrong (`error.reason` carries the same as an Elixir term).

# `max_supported_version`

```elixir
@spec max_supported_version() :: pos_integer()
```

The highest message `v` `decode/1` accepts.

# `min_supported_version`

```elixir
@spec min_supported_version() :: pos_integer()
```

The lowest message `v` `decode/1` accepts.

# `to_hook`

```elixir
@spec to_hook(t(), Ankusa.SDK.ClaimCheck.t()) ::
  {:ok, Ankusa.SDK.Hook.t()} | {:error, Exception.t()}
```

Turn a decoded message into the `Ankusa.SDK.Hook` a handler takes, redeeming
the claim check when the message carries one.

A `Ankusa.SDK.ClaimCheck` client is required even for an inline message, so a
consumer cannot forget to supply one and then fail the day a payload exceeds
the sink's threshold. A redemption failure returns the claim-check error
as-is: its `retryable` field is the ack/requeue decision.

Delivery is at-least-once — the same hook id can arrive twice — so a handler
still dedupes on the key in `Ankusa.SDK.Idempotency.key/2`.

---

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