# Media

> Whitelabel WhatsApp Cloud API keeps its own copy of every WhatsApp file, inbound and outbound, and gives you a signed URL. You never need the business's Meta token.

Scopes: `media:read` for reads, `media:write` for uploads, retries and deletes.

## Receive a file

1. The customer sends a photo, voice note, video, document or sticker.
2. `message.received` arrives at once. It is Meta's change, unchanged: the media object has Meta's `id`, not a file.
3. Whitelabel WhatsApp Cloud API downloads the file from Meta, stores it, and (for audio, when transcription is on) transcribes it.
4. `message.media_ready` arrives with a signed `url`.

```json
{
  "event": "message.media_ready",
  "tenantId": "ck_tenant",
  "customerId": "ck_customer",
  "phoneNumberId": "ck_phone",
  "data": {
    "mediaAssetId": "ck_asset",
    "messageId": "ck_message",
    "wamId": "wamid.HBgM...",
    "conversationId": "ck_conversation",
    "origin": "inbound",
    "direction": "INBOUND",
    "kind": "audio",
    "status": "STORED",
    "waMediaId": "1234567890",
    "mimeType": "audio/ogg",
    "byteSize": 51200,
    "sha256": "69da8b7d…",
    "filename": null,
    "caption": null,
    "transcript": { "text": "Hola, necesito ayuda con mi pedido", "language": "spanish", "provider": "groq:whisper-large-v3-turbo" },
    "url": "https://example.com/whatsapp/…?X-Amz-Signature=…",
    "urlExpiresAt": "2026-10-21T20:00:00.000Z",
    "storedAt": "2026-10-21T19:00:00.000Z",
    "expiresAt": null
  }
}
```

- Join on `wamId` (or `messageId`) to the `message.received` you already stored.
- `url` is time-limited (`MEDIA_SIGNED_URL_TTL_SECS`, default 1 hour). Store `mediaAssetId`, not the URL; ask for a fresh URL when you need one.
- `transcript` is `null` unless the deployment turned transcription on. Imported history audio stays `null` unless the operator sets `HISTORY_SYNC_TRANSCRIBE=true`.
- After the last retry fails you get `message.media_failed` with the same fields, `status: "FAILED"`, `error` and `url: null`. Retry it with `POST /api/v1/media/assets/:id/retry`. Imported history does not send `message.media_failed`.
- Outbound media you send (by `id` or `link`) and files the business sends from the WhatsApp Business app (coexistence echoes) are copied too, with `direction: "OUTBOUND"`.
- Imported history files (`origin` `history_sync`) are copied into storage and do not send `message.media_ready` or `message.media_failed`.
- When the deployment or tenant has no storage configured, no copy is made and `message.media_ready` is not sent. The Meta proxy below still works.

## Read a stored file

```http
GET /api/v1/media/assets/:id
```

Returns the fields above plus a fresh `url` and `urlExpiresAt`.

```http
GET /api/v1/media/assets/:id/content
```

`302` to a 5-minute signed URL. Handy as an `<img src>` behind your own auth proxy.

```http
GET /api/v1/media/assets?messageId=&phoneNumberId=&status=&page=1&limit=50
```

`status` is `PENDING`, `STORED`, `FAILED`, `DELETING` (delete in progress: stored object not yet removed, or a copy still in flight) or `DELETED`. Newest first.

```http
POST /api/v1/media/assets/:id/retry
DELETE /api/v1/media/assets/:id
```

Retry queues a `FAILED` (or stuck `PENDING`) copy again: `202`. Delete removes the stored copy now: `204`. It does not touch Meta's copy. If a copy is still in flight the asset stays `DELETING` until that copy finishes and is removed. Retrying a `DELETING` asset returns `400`.

## Upload a file to send

Meta needs a media `id` (or a public `link`) in the send body. Upload first:

```http
POST /api/v1/media?phoneNumberId=PHONE_ID
Authorization: Bearer $VITRAL_WHATSAPP_API_KEY
Content-Type: multipart/form-data; boundary=…

file=@factura.pdf
```

- Multipart: a `file` part. Optional text fields `type` (MIME type, otherwise the part's type) and `kind` (`image`, `video`, `audio`, `document`, `sticker`).
- Or send the raw bytes with the file's `Content-Type` and optional `?filename=` and `?kind=`.

`201`:

```json
{ "data": { "mediaId": "1234567890", "mimeType": "application/pdf", "byteSize": 48211, "sha256": "…", "filename": "factura.pdf", "kind": "document" } }
```

Then send with `"document": { "id": "1234567890", "filename": "factura.pdf" }`.

Size limits are checked before Meta is called (`400` `VALIDATION_ERROR` with `details.maxBytes`):

| Kind | Limit | Types |
| --- | --- | --- |
| image | 5 MB | `image/jpeg`, `image/png` |
| sticker | 500 KB | `image/webp` |
| audio | 16 MB | `audio/aac`, `audio/amr`, `audio/mpeg`, `audio/mp4`, `audio/ogg` |
| video | 16 MB | `video/mp4`, `video/3gpp` |
| document | 100 MB | anything else |

### From a URL

```http
POST /api/v1/media/from-url
Content-Type: application/json

{ "phoneNumberId": "PHONE_ID", "url": "https://example.com/menu.pdf", "filename": "menu.pdf" }
```

Whitelabel WhatsApp Cloud API fetches the URL once and uploads it. Only public `http(s)` addresses are fetched: loopback, private, link-local and cloud-metadata addresses are refused, including through redirects or DNS that changes between lookups. Pass `mimeType` when the server does not send a useful `Content-Type`.

## Meta media proxy

These call Meta directly and keep nothing:

```http
GET /api/v1/media/:mediaId?phoneNumberId=          (Meta's URL info)
GET /api/v1/media/:mediaId/download?phoneNumberId= (bytes, while Meta still has the file)
DELETE /api/v1/media/:mediaId?phoneNumberId=
```

Prefer the stored copy: Meta removes media after a limited time.

## Storage usage

```http
GET /api/v1/usage/media
```

Scope `usage:read`. Stored files, bytes and transcribed files per phone number with its `customerId`, plus totals. Use it to rebill storage to your businesses.
