> ## 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.

# Types and IDs

> How to use the SDK types, the branded IDs and the functions that validate and convert identifiers.

The SDK is written in strict TypeScript and ships type declarations for ESM and CommonJS. Every request, response, error and event is typed.

## *Branded* IDs

A resource ID is a `string` at runtime, but the compiler treats it as a distinct type. This turns a mistake that would show up as a `404` in production into a compile error.

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

const messageId = toMessageId('msg_01J8ZK9M3Q7XABCDEFGHJKMNPQ')
const channelId = toChannelId('chan_01J8ZK9M3Q7XABCDEFGHJKMNPQ')

await routa.messages.retrieve(messageId) // ok
await routa.messages.retrieve(channelId) // compile error
await routa.messages.retrieve('msg_01J8ZK9M3Q7XABCDEFGHJKMNPQ') // error: a plain string is not a MessageId
```

The IDs that `send()` and the list methods return are already *branded*, so you only need to convert IDs that came from outside, such as one stored in your database.

### `toXId`: convert without validating

`toMessageId` only casts. Use it when the value is already trusted, such as a field read from an API response or stored by you.

### `isXId`: validate before you use

`isMessageId` checks the format (`msg_` followed by 26 characters) and narrows the type. Use it for untrusted input, such as a URL parameter:

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

function lookUp(id: string) {
  if (!isMessageId(id)) {
    throw new Error(`Not a message id: ${id}`)
  }
  return routa.messages.retrieve(id) // id is a MessageId here
}
```

### Available ID types

| Type | Prefix | Validate | Convert |
| - | - | - | - |
| `MessageId` | `msg_` | `isMessageId` | `toMessageId` |
| `ChannelId` | `chan_` | `isChannelId` | `toChannelId` |
| `EventId` | `evt_` | `isEventId` | `toEventId` |
| `TemplateId` | `tmpl_` | `isTemplateId` | `toTemplateId` |
| `MediaId` | `med_` | `isMediaId` | `toMediaId` |
| `TemplateMediaId` | `tmedia_` | `isTemplateMediaId` | `toTemplateMediaId` |
| `WebhookId` | `whe_` | `isWebhookId` | `toWebhookId` |
| `WebhookDeliveryId` | `whd_` | `isWebhookDeliveryId` | `toWebhookDeliveryId` |

`TemplateMediaId` is different from `MediaId`: the first identifies the file of a template header, and the second, the media of a message. One is not assignable to the other. The [Partner API](https://docs.routa.chat/en/sdk/partner.md) IDs, such as `PartnerProjectId` (`proj_`) and `ApiKeyId` (`key_`), come from `@routa-chat/sdk/partner`.

For other prefixes, there are the generics `Id<Prefix>`, `isPrefixedId` and `toId`.

> [!NOTE]
>
> The `channel` field of `messages.send()` and `templates.submit()` accepts a plain `string`. Resource IDs in `retrieve`, however, require the *branded* type. The two `mediaId` fields require it too: a `TemplateMediaId` in a `templates.submit()` header and a `MediaId` in the `headerParameter` of `messages.send()`.

## Exported types

You can import the main types from the package:

```ts theme={null}
import type {
  Message,
  MessageContent,
  MessageOrigin,
  MessageStatus,
  RoutaEvent,
  EventType,
  Template,
  TemplateStatus,
  Webhook,
  WebhookDelivery,
  Media,
  Usage,
  Whoami,
  ApiKeyScope,
  Page,
} from '@routa-chat/sdk'
```

The template and media types, and the errors, are exported too:

```ts theme={null}
import type {
  // What you read from a template
  TemplateCategory,
  TemplateComponents,
  TemplateHeader,
  TemplateFooter,
  TemplateButton,
  TemplateHeaderMediaFormat,
  TemplateFailureCode,
  TemplateOrigin,
  TemplateUnsupportedComponent,
  // What you send in messages.send() and read from the content of a template
  SendMessageTemplateParams,
  SendMessageTemplateHeaderParameter,
  SendMessageTemplateButtonParameter,
  MessageTemplateHeaderParameter,
  MessageTemplateButtonParameter,
  // What you send in templates.submit()
  SubmitTemplateParams,
  TemplateComponentsInput,
  TemplateHeaderInput,
  TemplateFooterInput,
  TemplateButtonInput,
  // Files
  TemplateMedia,
  UploadTemplateMediaParams,
  UploadMediaParams,
  UploadMediaFileParams,
  UploadMediaUrlParams,
  // Error details
  RoutaErrorDetailValues,
} from '@routa-chat/sdk'
```

The types that describe a template you **read** (`TemplateHeader`, `TemplateFooter`, `TemplateButton`, `TemplateComponents`) keep the API's *snake\_case* (`media_id`, `phone_number`). The **input** types (`TemplateHeaderInput` and the other `*Input` types) use *camelCase* (`mediaId`, `phoneNumber`). See [Templates](https://docs.routa.chat/en/sdk/templates.md).

### Message content

`MessageContent` is a union discriminated by `type`. Narrow on that field. From version 0.2.0, `message.content` can also be `null`, when retention removed the content. Check for that before the `switch`:

```ts theme={null}
function describe(message: Message): string {
  const { content } = message
  if (content === null) {
    return '(content removed by retention)'
  }
  switch (content.type) {
    case 'text':
      return content.body
    case 'template':
      return `template ${content.template_id}`
    case 'image':
    case 'video':
    case 'audio':
    case 'document':
    case 'sticker':
      return `media ${content.media_id}`
    case 'location':
      return `${content.latitude}, ${content.longitude}`
    case 'reaction':
      return content.emoji
    case 'unsupported':
      return '(unsupported content)'
  }
}
```

### Events

`RoutaEvent` is a union on `type`, with a last generic member for types your SDK version does not know yet. `EventType` has 17 members on 0.3.0 and every known event has `externalRef`. See [Webhooks in the SDK](https://docs.routa.chat/en/sdk/webhooks.md).

## Naming conventions

The SDK exposes the API fields in *camelCase* (`deliveredAt`, `nextCursor`), except in three places that keep the API format: message `content` (`media_id`, `template_id`, `body_parameters`, `header_parameter`, `button_parameters`), event `data` and the `components` of a template you read (`media_id`, `phone_number`).


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