# The connect window

> link.js and the npm package, theme and language, and your own sign-in screens.

`link.js` opens our sign-in window over your page. The user picks nothing: the link token already names the portal.

| Option | Description |
|---|---|
| `token` | The link token from your backend. |
| `onSuccess(connectionId, meta)` | The account is linked. Save `connectionId` on your backend; `meta.portal` names the portal. |
| `onExit(error)` | The window closed without a connection. `error` is null if the user simply left; otherwise `{code, message}` for the last thing that went wrong, with `code` from the [error codes](https://sidelatch.com/docs/errors) (`auth_failed`, `link_expired`, `locked_out`...). |
| `onEvent(name, data)` | As things happen: `opened`, `submitted`, `challenge` (`data.kind`), `browser` (the hosted sign-in page opened), `error` (`data.code`), `connected`, `exit`. For your own funnel analytics. Never carries a login. [Payloads](https://sidelatch.com/docs/events#window-events). |
| `theme` | Optional. `{accent, radius, font, mode}`; see [Theme and language](https://sidelatch.com/docs/guides/connect-window#theme). |
| `locale` | Optional. `en` (default) or `es`. |

**HTML**

```html
<script src="https://sidelatch.com/link.js"></script>
<script>
  Sidelatch.create({
    token: linkToken,
    onSuccess(connectionId, meta) { /* send connectionId to your backend and save it */ },
    onExit(error) { /* closed without connecting */ },
    onEvent(name, data) { /* your funnel analytics */ },
    theme: { accent: '#0f766e', radius: 10, mode: 'auto' },
    locale: 'es',
  }).open();
</script>
```

### The npm package

`@sidelatch/link` does the same as `link.js` as a module, with TypeScript types, no dependencies, and ESM and CommonJS builds. `open()` opens the window at once and returns a handle: `close()` closes it as if the user did (`onExit` is called) and `destroy()` removes it silently. Pass `baseUrl` if your Sidelatch host is not `https://sidelatch.com`.

**JavaScript**

```js
// npm install @sidelatch/link
import { open } from '@sidelatch/link';

const handle = open({
  token: linkToken,
  onSuccess(connectionId, { portal }) { /* save connectionId on your backend */ },
  onExit(error) { /* null, or {code, message} */ },
  theme: { accent: '#0f766e', radius: 10, font: 'Inter, sans-serif', mode: 'light' },
  locale: 'es',
});
```

For React, `@sidelatch/link/react` has a `useSidelatchLink` hook (`open`, `close`, `ready`, `isOpen`) and a `SidelatchLinkButton`. React is an optional peer dependency, used only by that entry. A link token works once, so fetch a new one each time the user opens the window.

**React**

```jsx
import { SidelatchLinkButton, useSidelatchLink } from '@sidelatch/link/react';

<SidelatchLinkButton token={linkToken} onSuccess={(id) => saveConnection(id)}>
  Link your carrier
</SidelatchLinkButton>

// or, with your own button:
const link = useSidelatchLink({ token: linkToken, onSuccess, onExit, locale: 'es' });
<button disabled={!link.ready || link.isOpen} onClick={link.open}>Connect</button>
```

### Theme and language

Match the window to your app. Every value is checked against the pattern below, in the browser and again on our server; one that doesn't fit is dropped and the default is used, so a typo never breaks the window. Nothing you pass is written into the page as raw CSS or HTML.

| Option | Accepted values | Default |
|---|---|---|
| `theme.accent` | `#rgb` or `#rrggbb`. The button and focus colour; its text is black or white, whichever contrasts more. | dark grey (light grey in dark mode) |
| `theme.radius` | 0 to 24, in px, for fields and buttons. The card uses 4 more. | 8 (card 12) |
| `theme.font` | Up to four font names, comma separated: letters, digits, spaces and hyphens. The window loads no web fonts, so use one the device has (or `system-ui`, `serif`, `monospace`). | `system-ui` |
| `theme.mode` | `light`, `dark` or `auto`. | `auto`: follows the device |
| `locale` | `en` or `es`. Regional tags count (`es-MX` is Spanish); others show English. | `en` |

In Spanish, every word the window shows is translated, and the errors the user can act on (wrong password, expired link, too many tries, portal down) read in Spanish too. What the portal itself writes, such as a security question, is shown as the portal wrote it. A hosted sign-in link (from [`relink-link`](https://sidelatch.com/docs/api#ref-post-v1-connections-conn-id-relink-link)) takes the same options as query parameters: `&locale=es&theme_accent=%230f766e&theme_radius=10&theme_mode=dark` (and `theme_font`).

### Verification steps (MFA, CAPTCHAs)

When a portal asks the user to prove who they are, the window shows that step to the user and passes their answer to the portal. Steps can follow one another (pick a phone, then type the code).

| Step | What the user sees |
|---|---|
| One-time code | A code field, for authenticator apps, text messages and email. |
| Delivery method | A choice of where to send the code. |
| Push approval | A waiting message while they approve on their phone. |
| Security questions | One field per question. |
| Image CAPTCHA | The portal's image and a field to type it. |
| Widget check (reCAPTCHA, hCaptcha, Turnstile) | Browser sign-in: a live view of the portal's own sign-in page, with their login filled in. They tick the box or solve the puzzle and press Sign in. |

The user always answers these themselves; we never solve or get around a challenge. Widget checks only work on the portal's own page, so for those the window opens that page in a browser we host and the user signs in there; we then keep the session it created, close that browser window, and every request after that runs over plain HTTP. The browser is for sign-in only, never for reading or writing data. Portals without browser sign-in enabled show a clear message instead. On an authenticator-app step the user can also give the app's setup key ("Answer this automatically from now on"); it is stored encrypted, and later sign-ins answer that step themselves.

### After linking: kept signed in for you

You call the API; everything below the sign-in is ours. Sessions (cookies or tokens) are saved encrypted, so a restart does not sign anyone in again. An expired session is renewed with the portal's refresh mechanism where it has one, and otherwise by signing in again from the vault, answering an authenticator step from the stored setup key. Trusted-device cookies are kept, so most re-logins skip verification. A network blip gets one quiet retry. Only something that needs the person (a text-message code, a push, a CAPTCHA, a changed password) moves the connection to `reconnect_needed` and sends the `connection.reconnect_needed` webhook; we do not keep retrying it.

### Relinking

Create a link token for the same `user_id` and portal and open the window again. The connection id doesn't change, so nothing on your side needs updating.

### Allowed origins

Set `allowed_origins` in [settings](https://sidelatch.com/docs/api#ref-patch-v1-settings) (or the console's Settings tab, one per line) and only those sites can embed the window or hear its result. It takes a list of up to 20 origins, so add production, staging and each local dev port, like `["https://app.yourco.com", "http://localhost:3000"]`. An origin is scheme, host and port only, with no path; a trailing slash is dropped. Leave it empty while developing to allow any site.

## Sign-in challenges

When a portal asks for more than a password, the connect window shows the step and the user answers it. Every step is one object, whatever the portal: a `type`, the portal's own `message`, and the `inputs` to collect.

**A challenge**

```json
{
  "type": "choice",
  "message": "Where should we send your code?",
  "inputs": [
    {
      "name": "code",
      "type": "select",
      "label": "Where should we send your code?",
      "options": [
        {
          "value": "sms",
          "label": "Text ***-***-0142"
        },
        {
          "value": "email",
          "label": "Email a***@example.com"
        }
      ]
    }
  ],
  "expires_in": 300,
  "attempts_left": 3,
  "step": 1,
  "max_steps": 6
}
```

| Type | What the user does | Inputs |
|---|---|---|
| `code` | Types a one-time code from a text, an email or an authenticator app. | one `string`; with an authenticator, an optional setup key so later sign-ins need no one |
| `choice` | Picks where the code is sent, or which account to use. | one `select` with `options` |
| `push` | Approves on their phone. The window waits. | none |
| `questions` | Answers the portal's security questions. | one `string` per question |
| `captcha` | Reads an image and types what it shows. | one `string`, plus `image` |

The limits are fixed. A step waits **5 minutes** for an answer. A sign-in allows **3 wrong answers** and **6 steps**, then fails with `challenge_failed` and the user starts again. Failed sign-ins are capped before the portal can lock the user's real account (`locked_out`). A CAPTCHA widget that only works on the portal's own site is not passed to you: the window opens the portal's real page in a hosted browser and the user solves it there.

The connect window draws these for you. To draw them yourself, start the sign-in with [`POST /v1/links`](https://sidelatch.com/docs/api#ref-post-v1-links) and send each answer to [`POST /v1/links/{link_id}/answer`](https://sidelatch.com/docs/api#ref-post-v1-links-link-id-answer). Build the screen from `inputs`, not from the type, and it keeps working when a portal adds a step.

You hear about these as events: `link.challenge_required` (with the `kind`) and `link.failed`. They show where your users get stuck.

### With no user present

When a saved session ends, we sign in again from the vault. We answer a step ourselves in two cases only: an authenticator code, when the user stored their setup key, and an emailed code, when the user forwards the portal's emails to the connection's [forwarding address](https://sidelatch.com/docs/api#ref-post-v1-connections-conn-id-mfa-email). Any other step means the connection becomes `reconnect_needed` with reason `challenge_required`, and nothing is tried again until the user signs in.

---

Sidelatch docs: [Docs](https://sidelatch.com/docs.md) · [Quick start](https://sidelatch.com/docs/quickstart.md) · [Concepts](https://sidelatch.com/docs/concepts.md) · [The connect window](https://sidelatch.com/docs/guides/connect-window.md) · [Webhooks](https://sidelatch.com/docs/guides/webhooks.md) · [MCP for AI agents](https://sidelatch.com/docs/guides/mcp.md) · [Writes](https://sidelatch.com/docs/guides/writes.md) · [Build a portal](https://sidelatch.com/docs/guides/build-a-portal.md) · [Events](https://sidelatch.com/docs/events.md) · [Errors](https://sidelatch.com/docs/errors.md) · [API reference](https://sidelatch.com/docs/api.md)
