> ## Documentation Index
> Fetch the complete documentation index at: https://docs.routa.chat/llms.txt
> Use this file to discover all available pages before exploring further.

# Events, templates, media and usage

> The other SDK resources: routa.events, routa.templates, routa.media, routa.usage and routa.whoami.

## Events

`routa.events.list()` lists the log of normalized events. It is the reconciliation path after your server was unavailable. See [Reconcile events](https://docs.routa.chat/en/webhooks/reconciliation.md).

```ts theme={null}
for await (const event of routa.events.list({ type: 'message.failed', after: lastEventId })) {
  console.log(event.type, event.data)
}
```

| Parameter | Description |
| - | - |
| `type` | Filters by an event type. |
| `after` | Resumes after this event ID. |
| `limit` | Items per page, from 1 to 100. |

Each item is a `RoutaEvent`, the same type `webhooks.constructEvent()` returns: `id`, `type`, `apiVersion`, `schemaVersion`, `projectId`, `externalRef`, `sequence`, `occurredAt`, `recordedAt` and `data`.

`externalRef` is the `external_ref` of the project, or `null` when the project has none. Only projects created by a [partner](https://docs.routa.chat/en/partners/overview.md) have this value.

The SDK knows 17 event types. See the list in [Webhooks](https://docs.routa.chat/en/sdk/webhooks.md#event-types).

## Templates

`routa.templates` creates templates, which then go for approval, uploads the file of a media header and retrieves the result. See [Templates](https://docs.routa.chat/en/sdk/templates.md) for components and errors, and [Templates](https://docs.routa.chat/en/concepts/templates.md) for the lifecycle.

```ts theme={null}
const template = await routa.templates.submit({
  channel: 'chan_...',
  name: 'order_confirmation',
  language: 'en_US',
  category: 'utility',
  bodyText: 'Hi {{1}}! Your order {{2}} is confirmed.',
  variableExamples: ['Ana', '1234'],
})

console.log(template.id, template.status) // "tmpl_...", "submitted"
```

Every variable needs an example, and `variableExamples` exists from version 0.5.0. Use made-up values: no real personal data or tokens. See [Variables and examples](https://docs.routa.chat/en/sdk/templates.md#variables-and-examples).

| Method | Description |
| - | - |
| `submit(params)` | Creates the template and sends it for approval. Accepts `variableExamples` and `components`. |
| `uploadMedia({ file, contentType })` | Uploads the file of an image, video or document header. |
| `retrieve(id)` | Retrieves a template and its approval status. |
| `list({ channel?, limit?, cursor? })` | Lists templates, with automatic pagination. |

```ts theme={null}
import { toTemplateId } from '@routa-chat/sdk'

const template = await routa.templates.retrieve(toTemplateId('tmpl_...'))

switch (template.status) {
  case 'approved':
    // ready to be sent
    break
  case 'rejected':
    console.log(template.rejectionReason)
    break
  case 'failed':
    console.error(template.failureCode, template.failureDetail)
    break
}
```

The SDK **does not edit or delete** templates. Those operations exist only in the dashboard. A template with `deletionRequested: true` is being deleted and cannot be sent.

## Media

`routa.media` uploads a file to Routa and retrieves the media object. See [Media](https://docs.routa.chat/en/sdk/media.md) for uploading and [Media](https://docs.routa.chat/en/concepts/media.md) for the concept.

```ts theme={null}
import { toMediaId } from '@routa-chat/sdk'

const media = await routa.media.retrieve(toMediaId('med_...'))

if (media.status === 'ready' && media.url) {
  const file = await fetch(media.url)
}
```

`retrieve()` returns a fresh signed URL, valid for 15 minutes, when the status is `ready`.

## Usage

`routa.usage.retrieve()` returns the metered usage of the project over a period of up to 92 days. See [Usage](https://docs.routa.chat/en/billing/usage.md).

```ts theme={null}
const usage = await routa.usage.retrieve({ from: '2026-09-01', to: '2026-10-01' })

for (const record of usage.data) {
  console.log(record.metric, record.channelType, record.quantity, record.unit)
}
```

`from` is inclusive and `to` is exclusive, as `YYYY-MM-DD` UTC dates.

## Key identity

`routa.whoami()` resolves the API key in use, with no side effects:

```ts theme={null}
const { organizationId, projectId, apiKeyId, scopes } = await routa.whoami()
```

`scopes` is a list of `ApiKeyScope`: `messages:read`, `messages:write`, `channels:read`, `channels:write`, `events:read`, `webhooks:write`, `usage:read`, `templates:read`, `templates:write`, `media:read` and `media:write`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.