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

# Configuration

> Options of the Routa client: timeout, retries, baseURL, logger and fetch, plus the idempotency behavior.

The `Routa` constructor validates the configuration synchronously and builds the transport every resource uses.

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

const routa = new Routa({
  apiKey: process.env['ROUTA_API_KEY']!,
  timeout: 10_000, // ms per attempt
  maxRetries: 2, // retries after the first attempt
})
```

## Options

| Option | Default | Description |
| - | - | - |
| `apiKey` | required | `Bearer` credential: a project key (`rt_live_...` or `rt_test_...`). A partner key (`rt_partner_...`) throws on construction. |
| `baseURL` | Routa production API | Origin of the requests. Use it to point the client at another host, such as staging or a local instance: `baseURL: 'http://localhost:3000'`. |
| `timeout` | `30000` | Timeout of each attempt, in milliseconds. |
| `maxRetries` | `3` | Retries after the first attempt. `0` turns retries off. |
| `logger` | no-op | Object with `debug`, `warn` and `error` methods. Receives request diagnostics, such as retries. It never receives the API key or request and response bodies, and the SDK never writes to `console` on its own. |
| `fetch` | global `fetch` | Replaces the `fetch` used on every request, for example to inject a proxy agent or a *test double*. |

## Retries

The client retries:

* network failures and timeouts;
* `429` responses;
* `5xx` responses;
* a `409` that means "an identical request is still in progress" (`idempotency_in_progress`).

The wait uses exponential *backoff* with full jitter, capped at 8 seconds, and respects the `Retry-After` header when the server sends it. `4xx` responses such as `401`, `402`, `403`, `404` and `422` are **never** retried, because repeating them would only repeat the same failure. A `402` lasts until someone changes the plan or the payment. A `409` is not retried either, except `idempotency_in_progress`: `idempotency_key_reuse` and the other conflicts are client errors.

## Idempotency

`messages.send()` sends an `Idempotency-Key` automatically and **reuses it on every retry**, so a retry never creates a second message. Pass your own key with `send({ ..., idempotencyKey })`.

```ts theme={null}
await routa.messages.send({
  channel: 'chan_...',
  to: '+5581999999999',
  text: 'Hi!',
  idempotencyKey: 'order-1234-confirmation',
})
```

When a call returns the result of an earlier attempt, `message._replayed` is `true`.

> [!WARNING]
>
> The server only deduplicates `messages.send()`. If you need to avoid retries on the other write operations, set `maxRetries: 0`. That applies to `templates.submit()`, `templates.uploadMedia()` and `media.upload()`: a retry after a lost response can create a second template or store the file twice. See [Idempotency](https://docs.routa.chat/en/reliability/idempotency.md).

## Project key and partner key

`Routa` accepts only project keys. Passing a partner key throws on construction, with a message that points to `RoutaPartner`. A partner key is only accepted by the [partner](https://docs.routa.chat/en/partners/partner-keys.md) routes, and never sends messages.

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

new Routa('rt_partner_...') // throws: use RoutaPartner, from '@routa-chat/sdk/partner'
```

`RoutaPartner` accepts the same transport options (`baseURL`, `timeout`, `maxRetries`, `logger` and `fetch`). See [Partners](https://docs.routa.chat/en/sdk/partner.md).

## Logger

```ts theme={null}
const routa = new Routa({
  apiKey: process.env['ROUTA_API_KEY']!,
  logger: {
    debug: (message, context) => console.debug(message, context),
    warn: (message, context) => console.warn(message, context),
    error: (message, context) => console.error(message, context),
  },
})
```

## Custom fetch

Replace `fetch` to inject a proxy agent or, in tests, to respond without a network:

```ts theme={null}
const routa = new Routa({
  apiKey: 'rt_test_...',
  fetch: async () =>
    new Response(
      JSON.stringify({
        organization_id: 'org_01J8ZK9M3Q7XABCDEFGHJKMNPQ',
        project_id: 'proj_01J8ZK9M3Q7XABCDEFGHJKMNPQ',
        api_key_id: 'key_01J8ZK9M3Q7XABCDEFGHJKMNPQ',
        scopes: ['messages:write'],
      }),
      { status: 200, headers: { 'content-type': 'application/json' } }
    ),
})

await routa.whoami() // makes no network call
```


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