# Embed and configure the chat widget (/docs/developers/embed-the-widget)

> Embed the Loopwix chat widget with one script tag and your public key. Show it as a bubble, inside a page or behind your own button, control it with the Loopwix JavaScript API, limit it to your allowed sites, and set your Content Security Policy.

Type: how-to | Status: ga | Last reviewed: 2026-11-21 | Minimum role: builder

The Loopwix chat widget is one script. Paste it into your page and your published agent answers visitors there. Everything else on this page is optional.

## How do I embed the chat widget on my site? [#how-do-i-embed-the-chat-widget-on-my-site]

Copy the script tag with your public key from the **Install** tab of the website chat, and place it before the closing body tag. To show the chat inside a page, choose **Embedded in the page** and add a `loopwix-chat` element. To open it from your own button, choose **My own button**.

## Add the script [#add-the-script]

1. Open the agent, choose **Channels**, then **Manage** on Website chat, then the **Install** tab.
2. Copy the snippet. It holds the widget address and your public key.
3. Paste it once, just before `</body>`, on every page that should have the chat.
4. Add your site under **Advanced → Allowed sites**, or the chat will not load.

```html
<script src="https://YOUR-WIDGET-HOST/widget.js" data-key="lwix_pub_…" defer></script>
```

Use the exact `src` from your Install tab. The script starts by itself when it has a `data-key`. Add `data-mount="page"` to make the chat fill the whole page instead of showing a launcher.

Single-page apps: load the script once, in the page shell, not on every route. For React and Next.js, see [React and Next.js](/docs/developers/react-and-next-js).

<Screenshot src="/media/developers/install-tab.png" alt="The Install tab of the website chat studio" caption="The Install tab" captured="2026-11-02" />

## Choose how the chat appears [#choose-how-the-chat-appears]

On the **Install** tab, **How it appears** has a **Chat window** setting.

**Opens from a button** (the default) shows a launcher. Under **Button**, choose **Chat bubble** for the standard launcher, or **My own button** to use an element on your page. With **My own button**, add `data-loopwix-open` to any button or link:

```html
<button data-loopwix-open>Chat with us</button>
```

An element with the id `loopwix-launcher` works the same way.

**Embedded in the page** renders the chat inside a container you place. Give it the size you want; the chat fills it:

```html
<div id="loopwix-chat" style="width:420px;height:600px"></div>
```

If the container is missing, the widget shows the chat bubble instead and logs a warning in the browser console. Save the draft and publish the agent for these settings to apply.

## JavaScript API [#javascript-api]

The script defines a global `Loopwix` object.

```js
// Open, close or toggle the chat. open() is safe to call before the widget has loaded.
document.querySelector('#help').addEventListener('click', () => Loopwix.open());

// React to replies. on() returns a function that removes the listener.
const stop = Loopwix.on('message', (event) => {
  console.log(event.role, event.text); // role is "assistant" (your agent) or "human" (a teammate)
});
stop();
```

| Method | What it does |
|---|---|
| `init(options)` | Starts the widget. Not needed when the script tag has `data-key` |
| `open()`, `close()`, `toggle()` | Show or hide the chat |
| `on(event, fn)` | Listen for an event; returns a function that removes the listener |
| `off(event, fn)` | Remove a listener |

| Event | When it fires |
|---|---|
| `ready` | The widget has loaded and is ready |
| `open`, `close` | The chat window opens or closes |
| `message` | Your agent or a teammate replies; the event has `role` and `text` |

## Widget options [#widget-options]

Start the widget yourself when you leave out `data-key`, for example to load it only after the visitor gives consent:

```html
<script src="https://YOUR-WIDGET-HOST/widget.js" defer></script>
<script>
  window.addEventListener('load', () => {
    Loopwix.init({ apiKey: 'lwix_pub_…', mount: 'embed' });
  });
</script>
```

| Option | Meaning |
|---|---|
| `apiKey` | Your public key (`lwix_pub_…`). Required |
| `mount` | `embed` (default): the launcher or the `loopwix-chat` container. `page`: the chat fills the page. `preview` is used by the studio |

Call `init` once. A second call logs "widget already mounted" and does nothing.

## Public keys and allowed sites [#public-keys-and-allowed-sites]

The `lwix_pub_…` key is **public**: it appears in your page source and cannot read your data. What limits its use is **Allowed sites**: a request from an address not on the list is refused. See [Advanced](/docs/channels/website-chat/advanced).

Never put secret credentials in a page. Integration credentials stay on the server; see [Security](/docs/security).

## CSP, cookies and privacy [#csp-cookies-and-privacy]

If your site sends a Content Security Policy, allow the widget host from your snippet and the API host the widget calls. Find the API host in your browser's network tab, then add both:

```http
Content-Security-Policy: script-src 'self' https://YOUR-WIDGET-HOST; connect-src 'self' https://YOUR-API-HOST; img-src 'self' data: https://YOUR-API-HOST
```

Check the browser console for anything still blocked.

The widget sets no cookies. It keeps its session in the browser's session storage, or in local storage when the agent keeps chat history across visits. It also stores a few display preferences.

If you use a consent tool, decide whether the chat loads before or after consent; use `Loopwix.init` to load it after. Link your privacy policy in [Contact and legal](/docs/channels/website-chat/customise#contact-and-legal). Files visitors send are scanned and kept for a limited time; see [Messages](/docs/channels/website-chat/messages).
