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

# Upgrade the SDK

> What changes when you upgrade @routa-chat/sdk, from 0.2.0 to 0.5.0: what stops working or compiling and how to migrate.

The SDK is 0.x. A change to a public type ships as a minor version. Nothing was removed or renamed in 0.3.0. The full list is in the [CHANGELOG](https://github.com/routa-chat/routa-nodejs-sdk/blob/master/CHANGELOG.md).

## From 0.4.0 to 0.5.0

0.5.0 adds the examples of template variables and the header and button parameters of a send. The new fields are optional in the types.

> [!WARNING]
>
> The API requires an example for each variable of a template. A version earlier than 0.5.0 does not have the field and drops a value passed anyway, so `templates.submit()` of a template with a variable gets `422 template_variables_invalid`. Upgrade the SDK and send the examples.

```ts theme={null}
await routa.templates.submit({
  channel: 'chan_...',
  name: 'order_shipped',
  language: 'en_US',
  category: 'utility',
  bodyText: 'Your order {{1}} has shipped.',
  variableExamples: ['1234'], // new, one per body variable
})
```

### What is new

* **Examples.** `variableExamples` in [`templates.submit()`](https://docs.routa.chat/en/sdk/templates#variables-and-examples) and `example` on the text header and on the URL button.
* **Template send.** `headerParameter` and `buttonParameters` in [`messages.send()`](https://docs.routa.chat/en/sdk/messages#send-a-template).
* **Reading.** `header_parameter` and `button_parameters` in the content of a message, and `example` in `template.variables`, on the text header and on the URL button.
* **New exported types.** `SendMessageTemplateHeaderParameter`, `SendMessageTemplateButtonParameter`, `MessageTemplateHeaderParameter` and `MessageTemplateButtonParameter`.

## From 0.3.0 to 0.4.0

The experimental [Partner API](https://docs.routa.chat/en/sdk/partner.md#receive-the-events-of-every-customer) gains `partner.webhook.constructEvent()`, which verifies an organization webhook delivery without a `Routa` instance.

## From 0.2.0 to 0.3.0

0.3.0 aligns the SDK types with the current API contract and brings three new resources: [`media.upload()`](https://docs.routa.chat/en/sdk/media), [`components` and `templates.uploadMedia()`](https://docs.routa.chat/en/sdk/templates) and the [Partner API](https://docs.routa.chat/en/sdk/partner.md), which is experimental.

### What can stop compiling

**`TemplateStatus` has a new member, `'failed'`.** Routa could not send the template to the provider. A `switch` on the status that ends in a `never` check needs the new case. `failureCode` and `failureDetail` say why.

**`EventType` goes from 13 to 17 members.** `template.failed`, `template.category_changed`, `template.updated` and `template.deleted` are new, and `RoutaEvent` has a member for each. An exhaustive `switch (event.type)` needs to handle them. See [Webhooks](https://docs.routa.chat/en/sdk/webhooks.md#event-types).

**New required fields in the types you read.** Reading a `Template`, a `Message` or an event works as before. Code that **builds** one of these values, such as test data or a *mock* of the SDK, stops compiling until it provides the fields:

| Type | New fields |
| - | - |
| `Template` | `bodyText` (`string \| null`), `failureCode`, `failureDetail`, `deletionRequested`, `deletionFailureDetail`, `origin`, `readOnly` and `unsupportedComponents`. `components` is optional and absent on a body-only template. |
| `Message` | `origin` (`'api'`, `'business_app'` or `null` on an inbound message) and `failureCode`. |
| Every known event | `externalRef`, the `external_ref` of the project or `null`. |

**`RoutaApiError` gains `type` and `details`.** `ErrorEnvelope` and `RoutaApiErrorDetails` accept them, and both are optional: a response from a proxy has neither.

**`402` and `409` have their own classes.** `RoutaBillingError` (with `limit`, `used` and `resetsAt`) and `RoutaConflictError` (with `reason`) extend `RoutaApiError`. A `catch` on `RoutaApiError` keeps working. Only code that compared `error.constructor === RoutaApiError` for those statuses notices the difference. The retry policy does not change: a `402` is never retried, and a `409` only when it is `idempotency_in_progress`. See [Error handling](https://docs.routa.chat/en/sdk/error-handling.md).

**`HttpResponse` has `status`.** Whoever implements the internal `HttpClient` interface, such as a transport *test double*, must return `status` from `requestWithHeaders()`. The interface is not exported, so code that only calls `routa.*` is not affected.

**`new Routa('rt_partner_...')` throws.** A partner key is only accepted by the partner routes, so a `Routa` built with one answered `401` to every call. Nothing that worked was affected. Use [`RoutaPartner`](https://docs.routa.chat/en/sdk/partner).

### How to migrate

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

// 1. Handle the new status.
function report(template: Template) {
  switch (template.status) {
    // ...the cases you already have...
    case 'failed':
      console.error(template.failureCode, template.failureDetail)
      break
  }

  // 2. `components` can be absent: guard the read.
  if (template.components?.buttons) {
    console.log(template.components.buttons.length)
  }
}

// 3. Tell a billing error apart.
try {
  await routa.messages.send(params)
} catch (error) {
  if (!(error instanceof RoutaBillingError)) throw error

  // The free allowance or the plan allowance is used up. Stop sending.
  console.error(error.code, error.used, error.limit)
}
```

### What is new

* **Media.** [`routa.media.upload()`](https://docs.routa.chat/en/sdk/media) stores a file, from bytes or from a URL. If you called `POST /v1/media` directly, you can migrate.
* **Templates with components.** [`templates.submit()`](https://docs.routa.chat/en/sdk/templates) accepts `components`, with header, footer and up to 10 buttons. [`templates.uploadMedia()`](https://docs.routa.chat/en/sdk/templates#image-video-or-document-header) uploads the file of a media header.
* **New fields.** `Template` (`bodyText`, `components` and the others), `Message` (`origin`, `failureCode`) and the events (`externalRef`).
* **New exported types.** `MessageOrigin`, `TemplateFailureCode`, `TemplateOrigin`, `TemplateUnsupportedComponent`, `TemplateComponents`, `TemplateHeader`, `TemplateFooter`, `TemplateButton`, `TemplateHeaderMediaFormat` and `RoutaErrorDetailValues`, plus `TemplateMediaId`, `TemplateMedia` and the `*Input` types of the components. See [Types and IDs](https://docs.routa.chat/en/sdk/typescript.md).
* **Partner API.** [`RoutaPartner`](https://docs.routa.chat/en/sdk/partner), in `@routa-chat/sdk/partner`. Experimental.

## From 0.1.0 to 0.2.0

`Message['content']` became `MessageContent | null`, and `Message` gained `contentRedactedAt: string | null`.

Routa removes the content of a message after the retention period of your plan and returns `content: null` with `content_redacted_at`. On 0.1.0, `messages.retrieve()` and `messages.list()` threw `TypeError: Cannot read properties of null` when they met one of those messages, and a single one broke the whole listing. Now they are returned with the rest of the message intact.

This is a type change, so code that reads `message.content` without checking stops compiling. Guard the read:

```ts theme={null}
if (message.content) {
  console.log(message.content.type)
}
```

See [Messages](https://docs.routa.chat/en/sdk/messages.md#messages-with-removed-content) and [Content removed by retention](https://docs.routa.chat/en/concepts/messages.md#content-removed-by-retention).


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