# Web chat JavaScript API

> For developers. Tell the web chat who your signed-in visitor is, which pages of your site it may open, open it from your own button and fix its language.

Source: https://docs.komplian.com/api/widget

Once the [web chat](https://docs.komplian.com/channels/web-widget) is on your site, your pages have a `window.KW` object. Use it to pass the agent what your site already knows, and to let the agent move the visitor around your site.

## Install the script

The web chat page in Komplian gives you this tag with your own widget ID. Paste it on every page where the chat should appear.

```html
<script src="https://api.komplian.com/widget.js" data-widget-id="YOUR_WIDGET_ID" async></script>
```

| Attribute | What it does |
| --- | --- |
| `data-widget-id` | Your web chat. Required. Copy it from the web chat page in Komplian. |
| `data-language` | Optional. `es` or `en`. Sets the language of the chat's own texts until the conversation has a language. |
| `async` | Loads the chat without slowing your page. |

## Methods

| Method | What it does |
| --- | --- |
| `KW.identify({...})` | Tells the agent who the visitor is. |
| `KW.setCapabilities({...})` | Lists the pages of your site the agent may send the visitor to. |
| `KW.toggle()` | Opens the chat, or closes it if it is open. |

## Identify the visitor

Call `KW.identify` when the visitor is signed in to your site.

```js
KW.identify({
  externalId: 'user_123',
  email: 'ana@example.com',
  name: 'Ana Pérez',
  phone: '+34600000000',
  plan: 'premium',
  accountId: 'acct_42',
  customFields: { orders: 7, vip: true }
});
```

| Field | What it holds |
| --- | --- |
| `externalId` | The visitor's ID in your system. |
| `email`, `name`, `phone` | Their contact details. |
| `plan` | Their plan or tier in your product. |
| `accountId` | The account or company they belong to in your system. |
| `customFields` | Up to 20 extra keys. A key uses letters, numbers, `_`, `.` or `-`. Each value is text, a number or true/false. |

These details come from your page, so Komplian treats them as unverified context. The agent uses them to answer, but they do not prove who the visitor is.

## Let the agent use your site

Call `KW.setCapabilities` to list the pages of your site the agent may send the visitor to.

```js
KW.setCapabilities({
  navigate: ['/pricing', '/account/orders']
});
```

| Key | What it holds | Limit |
| --- | --- | --- |
| `navigate` | Paths on your own site, starting with `/`. Any other address is ignored. | Up to 40 paths. |

When the agent suggests one of these pages, the chat shows a button to open it. When the visitor presses it, the chat opens the page. Your page also receives a browser message with `source: 'komplian-widget'`, `type: 'komplian-host-actions'` and `actions: [{ kind: 'navigate', path }]`.

## Open the chat from your own button

```html
<button onclick="KW.toggle()">Chat with us</button>
```

## Related

- [Web chat](https://docs.komplian.com/channels/web-widget): Design, publish and install the chat.
- [How the agent works](https://docs.komplian.com/ai-agent/overview): What the agent knows and how it answers.
