# Inbox — media

Images, video, audio, voice notes, stickers and documents in a live chat.

Uploads do not pass through the API. A client describes the file, gets a presigned
URL, and sends the bytes **straight to object storage** — so a 90 MB document
costs the gateway a signature rather than 90 MB of transfer, and a stalled upload
holds the browser's connection rather than one of ours.

Storage is Wasabi, S3-compatible.

## The short version

```ts
const link = await crm.inboxMedia.upload(file, { kind: 'image' });

await inbox.messages.sendMedia({
  channel: channelId,
  contact: { id: chatId, identifier: '919000000000' },
  kind: 'image',
  link,
  caption: 'Your order has shipped',
});
```

Or in one call, if the `Inbox` was given an uploader:

```ts
const inbox = new Inbox({
  url, clientId, token,
  uploadMedia: crm.inboxMedia.upload.bind(crm.inboxMedia),
});

await inbox.messages.sendMedia({ channel, contact, kind: 'image', file, caption: 'Your order' });
```

A socket cannot presign — the ticket comes from the REST API — so `uploadMedia`
is the seam between the two. It is a function rather than a client, so an app that
only ever sends an already-uploaded `link` pulls in none of the REST surface.

## What may be sent

| Kind | Limit | Accepts |
|---|---|---|
| `image` | 5 MB | `image/jpeg` `image/png` `image/webp` |
| `video` | 16 MB | `video/mp4` `video/3gpp` `video/quicktime` |
| `audio` | 16 MB | `audio/aac` `audio/mp4` `audio/mpeg` `audio/amr` `audio/ogg` |
| `voice` | 16 MB | same as `audio` |
| `sticker` | 100 KB | `image/webp` |
| `document` | 100 MB | anything a browser will not execute |

These are WhatsApp's ceilings, checked when the upload is **requested** rather
than when the message is sent. That is the only point where the answer is still
useful: a file rejected here is one the agent can swap, while one that passes and
then fails at the provider has already been uploaded, already drawn on screen, and
comes back as a provider error against a bubble that looks sent.

`text/html`, `image/svg+xml` and the JavaScript types are refused for every kind,
`document` included. They would be served as live pages from the bucket's own
origin.

## upload

```ts
upload(file: Blob | UploadMediaSource, options: UploadMediaOptions): Promise<string>
```

Three steps in one call — ask for a ticket, `PUT` the bytes, return the link.

| Option | Type | Required | Notes |
|---|---|---|---|
| `kind` | `MediaKind` | yes | Decides the size and type limits. |
| `filename` | `string` | for a bare `Blob` | Overrides a `File`'s own name. |
| `contentType` | `string` | for a bare `Blob` | Overrides a `File`'s own type. |
| `signal` | `AbortSignal` | no | Cancels the transfer. |

In Node there is no `File`, so describe the bytes:

```ts
const bytes = await readFile('invoice.pdf');

const link = await crm.inboxMedia.upload(
  { body: bytes, filename: 'invoice.pdf', contentType: 'application/pdf', size: bytes.byteLength },
  { kind: 'document' },
);
```

`size` must be the real byte length. It is signed into the URL, so a mismatch
fails the upload rather than being silently corrected.

### The returned link is permanent

Only `upload_url` expires, and only if it is never used. The link itself does not,
which is what lets a thread still render its media months later.

Objects are `public-read` with an unguessable UUID key — the provider fetches the
media by URL when the message is sent, and the inbox renders it from the stored
link for the life of the conversation. **Anyone holding the URL can read the
object**, so treat the link as the secret it is.

## createUpload

```ts
createUpload(payload: CreateMediaUploadPayload): Promise<MediaUpload>
```

`POST /v1/audience/inbox/media/upload-url` — the ticket on its own, no bytes.
Reach for this when you need the `key`, or when something other than this SDK
performs the upload.

| Field | Type | Notes |
|---|---|---|
| `kind` | `MediaKind` | |
| `filename` | `string` | Only its extension and display name survive. |
| `content_type` | `string` | Signed. Becomes the object's stored type. |
| `size` | `number` | Signed. Exact byte length. |

Answers:

| Field | Notes |
|---|---|
| `upload_url` | `PUT` here. Expires in 15 minutes. |
| `headers` | Send **exactly** these. One extra or missing header fails the signature. |
| `link` | Where the object will be readable. Permanent. |
| `key` | Path in the bucket: `inbox/<ownerId>/<yyyy>/<mm>/<uuid><ext>` |
| `expires_at` / `expires_in` | When `upload_url` stops working. |

Doing the `PUT` yourself:

```ts
const ticket = await crm.inboxMedia.createUpload({
  kind: 'image', filename: 'order.jpg', content_type: 'image/jpeg', size: file.size,
});

await fetch(ticket.upload_url, { method: 'PUT', headers: ticket.headers, body: file });
// ticket.link is now live
```

Send **no** authorization header on that `PUT`. The signature is the
authorisation, and an extra header breaks it — besides handing your bearer token
to a third party.

### Your filename never becomes the key

The key is always a UUID. Two agents sending `photo.jpg` would otherwise collide,
with the second silently replacing the first inside a live conversation, and a
name like `../../other` would escape the account's prefix. The name you gave is
kept as the object's `Content-Disposition`, so a download still offers it.

The owner id in the key comes from the verified token, never from the payload.

## sendMedia

```ts
sendMedia(payload: SendMediaPayload): Promise<Message>
```

`message.send` · scope `messages:write`

Assembles the content and sends it. Everything `send()` does applies — the reply
is an optimistic echo with a queue id, and sending by hand pauses the chat's bot.
See [messages.md](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/inbox/messages.md).

| Field | Type | Required | Notes |
|---|---|---|---|
| `channel` | `string` | yes | |
| `contact` | `{id, identifier}` | yes | |
| `kind` | `MediaKind` | yes | |
| `link` | `string` | one of | Already uploaded. Takes precedence over `file`. |
| `file` | `MediaFile` | one of | Uploaded first. Needs `uploadMedia` on the Inbox. |
| `caption` | `string` | no | Shown under the media. Ignored for a `sticker`. |
| `filename` | `string` | no | What the recipient sees for a `document`. |
| `context` | `JsonObject` | no | `{ message_id }` to reply to a specific message. |
| `agent` | `string` | no | Needs `live_chat.send.AS_OTHER`. |

What goes on the wire:

```ts
{ type: 'image',    image:    { link, caption } }
{ type: 'document', document: { link, caption, filename } }
{ type: 'sticker',  sticker:  { link } }
```

A caption belongs on the media object rather than beside it, and a sticker takes
neither a caption nor a name — both are dropped rather than rejected.

Set `filename` on a document: it is what the recipient sees in their chat, where a
bare UUID would otherwise appear.

## Errors

| Status | Code | Means |
|---|---|---|
| 422 | `validation_failed` | Too large, or the type is not allowed for the kind. `details` names the field. |
| 503 | `media_unavailable` | The deployment has no storage configured. Every other inbox action still works. |
| — | `media_upload_failed` | The `PUT` to storage failed. Almost always the bytes not matching the declared `size` or `content_type`. |

## Inbound media

Media a contact sends arrives already downloaded, with a `link` under the content
key. When that first download failed, `messages.retryDownload()` re-fetches it —
inbound only, since an outbound attachment was ours to begin with.
