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

# Signed link

> How to build and sign the link the customer opens to connect their number: parameters, HMAC signature, test vectors and the return to your site.

The customer connects their number on a Routa page. You send them there with a **signed link**, which the dashboard calls the onboarding link:

```text theme={null}
https://app.routa.chat/connect/{partner_id}?…
```

The signature proves that the link comes from you. It uses the **signing secret** of your partner profile, which is different from the partner key. Always sign on your server.

## Parameters

The link carries these parameters and, besides them, only [`lang`](#page-language), which is optional and outside the signature. An unknown or repeated parameter makes the link invalid.

| Parameter | Required | What it is |
| - | - | - |
| `partner_id` | Yes | Your `ptn_…` ID, shown in the dashboard. |
| `external_ref` | Yes | Your ID for the customer: 1 to 128 characters among `A-Z a-z 0-9 . _ : -`. Never an email address, a phone number or a name. |
| `ts` | Yes | The moment you built the link, in Unix seconds. |
| `signature` | Yes | The HMAC below, as 64 lowercase hexadecimal characters. |
| `name` | No | What the project is called, up to 200 characters. The default is the `external_ref`. |
| `redirect_url` | No | `https://…`, on an origin you registered in the dashboard, up to 2048 characters. |
| `state` | No | Opaque value of up to 256 characters. It comes back unchanged to your `redirect_url`. |

Leave out an optional parameter you do not use. An empty parameter makes the link invalid. No value can contain a line break.

The first valid link creates the customer project, if it does not exist yet.

## The signature

`signature` is an **HMAC-SHA256**, in lowercase hexadecimal, with your signing secret as the key, computed over this string and nothing else:

```text theme={null}
routa-partner-link-v1
external_ref=<value>
name=<value>            (only if you send it)
partner_id=<value>
redirect_url=<value>    (only if you send it)
state=<value>           (only if you send it)
ts=<value>
```

* The first line is the fixed label `routa-partner-link-v1`. Each following line is `name=value` for every parameter you send, **except** `signature`.
* The names are in **ascending byte order** (`external_ref`, `name`, `partner_id`, `redirect_url`, `state`, `ts`), separated by a single `\n`, with **no trailing line break**.
* The values are the **decoded** strings, exactly what you put in the link, before any URL encoding.

```ts Node.js (SDK) theme={null}
import { buildOnboardingLink } from '@routa-chat/sdk/partner'

const url = await buildOnboardingLink({
  partnerId: process.env['ROUTA_PARTNER_ID']!,
  signingSecret: process.env['ROUTA_PARTNER_SIGNING_SECRET']!,
  externalRef: 'lead_123',
  name: 'Acme Store',
  redirectUrl: 'https://app.example.com/connected',
  state: 'value-you-check-against-the-customer-session',
})
```

```js Node.js (without the SDK) theme={null}
import { createHmac } from 'node:crypto'

const params = {
  partner_id: partnerId,
  external_ref: 'lead_123',
  ts: String(Math.floor(Date.now() / 1000)),
  // name, redirect_url, state: include only the ones you use
}

const lines = Object.entries(params)
  .sort(([a], [b]) => (a < b ? -1 : 1))
  .map(([name, value]) => `${name}=${value}`)

const signature = createHmac('sha256', signingSecret)
  .update(['routa-partner-link-v1', ...lines].join('\n'))
  .digest('hex')

const url = `https://app.routa.chat/connect/${partnerId}?${new URLSearchParams({ ...params, signature })}`
```

> [!NOTE]
>
> The SDK's `buildOnboardingLink` is a pure function: it makes no requests and validates the parameters locally. That matters, because Routa answers every invalid link with the same message, without saying why. The SDK is the only place where you find out why a value would be refused. It is marked as experimental.

## Page language

The connect page exists in Portuguese and in English. To choose the language, add `lang` to the link you already signed:

```text theme={null}
https://app.routa.chat/connect/{partner_id}?…&signature=…&lang=en
```

```ts Node.js (SDK) theme={null}
const link = await buildOnboardingLink(params)
const url = `${link}&lang=en` // `buildOnboardingLink` has no language parameter
```

* The values are `pt-BR` or `en`, written exactly like that. Any other value, such as `pt_BR` or `pt`, is ignored, and the link stays valid.
* `lang` is **not part of the signature**. Do not include it in the signed string.
* Without `lang`, the page uses the language the person had already chosen on Routa or, without a choice, the browser's first language: Portuguese for any variant of Portuguese, English for everything else.
* Someone signed in to a Routa account sees the language of the account, even with `lang` in the link.
* The person can switch the language on the page itself.

## Validity

Routa accepts the link for **10 minutes** after `ts`, and up to 60 seconds ahead of its own clock, to tolerate clock differences. Within that window, the customer can open the same link more than once.

Build the link **when the customer clicks**, not earlier. If it has to last longer, in an email for example, host your own redirector that signs on click.

## The `redirect_url` origin

`redirect_url` must:

* use `https`, with no username or password, spaces, control characters or backslash;
* have a full origin (scheme, host and port) **equal** to an origin you registered in the dashboard.

You register the origin in the dashboard. Partner keys do not register origins. Routa checks the address when the link arrives and again when the attempt ends.

## Returning to your site

When the attempt ends, the customer's browser is sent to your `redirect_url`. Your own query string is preserved, and Routa adds:

| Parameter | Value |
| - | - |
| `status` | `completed` or `failed`. |
| `project_id` | The project ID. |
| `channel_id` | The channel ID (`chan_...`), only when there is one. |
| `error` | A closed code, only on failure. |
| `state` | Your `state`, unchanged, only if you sent it. |

If the page was opened with `window.open`, it also posts the same fields to `window.opener`, addressed to the registered origin of your `redirect_url`, never to `*`:

```js theme={null}
window.addEventListener('message', event => {
  if (event.origin !== 'https://app.routa.chat') return
  if (event.data?.type !== 'routa:connect') return

  // event.data: { type, status, project_id, channel_id?, error?, state? }
})
```

> [!WARNING]
>
> Treat the redirect and the message as **hints**. Anyone can forge a request to your `redirect_url`, and `window.opener` can be lost after the provider's connection screen. Check your `state` against the customer session and then [retrieve the project](https://docs.routa.chat/en/partners/onboarding.md#4-confirm-the-redirect-is-a-hint-not-proof) to confirm the result.

The `error` field uses these values:

| Value | Meaning |
| - | - |
| `token_exchange_failed` | The customer's authorization could not be completed. |
| `waba_not_found` | No WhatsApp Business account was found in the authorization. |
| `phone_not_found` | No number was found in the authorization. |
| `phone_already_connected` | This number is already a channel. |
| `phone_registration_failed` | The number could not be registered. |
| `unexpected_error` | Unexpected error. Try again. |
| `timed_out` | The connection took too long. Try again. |

No key, secret or provider credential appears in the URL or in the message.

## When a link is refused

Every kind of bad link gets the **same response**: the customer sees "this link is not valid", and so does anyone testing links. This includes:

* an unknown `partner_id` or a wrong signature;
* a `ts` outside the window;
* a `redirect_url` that is not `https` or is not on a registered origin;
* a malformed, unknown or repeated parameter, except for [`lang`](#page-language);
* a plan without the Partner API.

If your links are refused, check in this order:

1. **The signing secret in use.** After a rotation, the old secret stays valid for 24 hours.
2. **The canonical string.** Compare it with the test vectors below.
3. **Your server clock.**
4. **The origins registered in the dashboard.**

## Test vectors

Use these two examples to check your implementation. The test secret is `rt_psign_TESTVECTORSECRET0000000000000000000000`.

**Minimal.** Parameters: `partner_id=ptn_TestPartner0000000000`, `external_ref=lead_123`, `ts=1790000000`.

```text theme={null}
routa-partner-link-v1
external_ref=lead_123
partner_id=ptn_TestPartner0000000000
ts=1790000000
```

`signature = 0676f9f5ee8e76eff763defdac73798afd0b7cf165abb875356ef4f370ff524a`

**Complete.** Adds `name=Acme Ltda`, `redirect_url=https://partner.example/connected?x=1` and `state=opaque:state/42`.

```text theme={null}
routa-partner-link-v1
external_ref=lead_123
name=Acme Ltda
partner_id=ptn_TestPartner0000000000
redirect_url=https://partner.example/connected?x=1
state=opaque:state/42
ts=1790000000
```

`signature = ff77d689a13e1057dda3b87e9f3f4e0788a98568d3022794ea6ecfc7c6dbb90c`

Both values were computed with Node.js `createHmac('sha256', secret)` over the strings above.


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