---
docId: doc_1c81eaa8ac8848239d88c5e86a4eb289
title: Embed it on your site
description: "Install and control the web chat in your site or application."
---

<script>
  import { Note } from '$lib/components';
</script>

# Embed it on your site

Install the web chat with a script, as a floating button or inside a page container. Use `window.PlaticaWebchat` to control it from your code.

Use `embed.publicKey`, the public key you can put in your HTML. The responses from [create](/api-reference/web-chats/create) and [get a web chat](/api-reference/web-chats/get) include ready-to-use installation snippets.

The chat only opens on the domains in `allowedDomains`. To try it on your computer, add your local domain, such as `localhost:5173`.

## With a floating button

Paste `embed.script` in your site's HTML, before `</body>`:

```html
<script
  src="https://chat.platica.mx/widget.js"
  data-webchat="wc_pub_7Hq2LmN4pR8sT1vX"
  defer>
</script>
```

The button appears according to `config.launcher`. The chat loads when the visitor clicks it; opening the page does not start a conversation.

## Inside a container

Use `embed.inline` to show the chat open inside a page element, without a button:

```html
<div id="platica-chat"></div>

<script
  src="https://chat.platica.mx/widget.js"
  data-webchat="wc_pub_7Hq2LmN4pR8sT1vX"
  data-target="#platica-chat"
  defer>
</script>
```

`data-target` is a CSS selector. The height comes from `config.inline` (`height` and `minHeight`). Unlike the button, the chat loads as soon as the page opens.

### In a panel of your app

If the chat goes in a panel that already has its own header and its own close button, add three attributes:

```html
<div id="platica-chat" style="height: 100%"></div>

<script
  src="https://chat.platica.mx/widget.js"
  data-webchat="wc_pub_7Hq2LmN4pR8sT1vX"
  data-target="#platica-chat"
  data-fit="container"
  data-closable="false"
  data-theme="host"
  defer>
</script>
```

| Attribute | What it does |
|-----------|--------------|
| `data-fit="container"` | Takes the full height of the container, with no minimum height, border, shadow, or rounded corners. The container needs a defined height |
| `data-closable="false"` | Removes the X and does not close with Escape. Your page can hide the panel without losing the conversation |
| `data-theme="host"` | Follows your app's theme. See [follow your app's theme](#follow-your-apps-theme) |

Without these attributes, the chat uses the published height and theme and can be closed.

## From your code

Use `window.PlaticaWebchat` to mount, open, or close the chat from your buttons.

| Method | What it does |
|--------|--------------|
| `mount(options)` | Shows the chat open inside a container. Returns a promise |
| `open(id)` | Opens the chat |
| `close(id)` | Closes it |
| `destroy(id)` | Removes it from the page |
| `expand(id)` and `collapse(id)` | Make it full screen and bring it back |
| `refresh(id)` | Reads the published configuration again, without reloading the page |

`id` is the `instanceId` you gave it when mounting, or the public key. If you omit it, it applies to the first chat on the page.

For example, to open the chat from a button of your own:

```html
<script
  src="https://chat.platica.mx/widget.js"
  data-webchat="wc_pub_7Hq2LmN4pR8sT1vX"
  defer>
</script>

<button onclick="window.PlaticaWebchat.open()">Questions? Write to us</button>
```

### Mounting it

`mount` does the same as `data-target`, but when you decide:

```js
await window.PlaticaWebchat.mount({
  publicKey: 'wc_pub_7Hq2LmN4pR8sT1vX',
  target: '#platica-chat',
  instanceId: 'support'
});

// When the visitor leaves that view:
window.PlaticaWebchat.destroy('support');
```

| Option | What it does | Required |
|--------|--------------|----------|
| `publicKey` | The web chat's public key | ✓ |
| `target` | Where to show it: a CSS selector or an element | ✓ |
| `instanceId` | A name for this chat. It is used to open, close, or remove it, and to have several on the same page | — |
| `fit` | `'container'` does the same as `data-fit="container"` | — |
| `closable` | `false` does the same as `data-closable="false"`. `close()` and `destroy()` still work | — |
| `theme` | `'host'` does the same as `data-theme="host"` | — |

Wait for the script to finish loading before using `window.PlaticaWebchat`.

## In React or Svelte

Load the same script once from your component. Mount the chat and call `destroy` when the component unmounts.

This function loads the script only once, even if the component mounts several times:

```js
const SCRIPT_URL = 'https://chat.platica.mx/widget.js';

export function loadPlaticaWebchat() {
  if (window.PlaticaWebchat) return Promise.resolve();
  let script = document.querySelector(`script[src="${SCRIPT_URL}"]`);
  if (!script) {
    script = document.createElement('script');
    script.src = SCRIPT_URL;
    document.head.appendChild(script);
  }
  return new Promise((resolve) => script.addEventListener('load', resolve, { once: true }));
}
```

In React, with `useEffect`:

```jsx
import { useEffect, useRef } from 'react';
import { loadPlaticaWebchat } from './loadPlaticaWebchat';

export function SupportChat({ publicKey }) {
  const container = useRef(null);

  useEffect(() => {
    let active = true;
    loadPlaticaWebchat().then(() => {
      if (!active) return;
      window.PlaticaWebchat.mount({ publicKey, target: container.current, instanceId: 'support' });
    });
    return () => {
      active = false;
      window.PlaticaWebchat?.destroy('support');
    };
  }, [publicKey]);

  return <div ref={container} style={{ height: 600 }} />;
}
```

In Svelte, with `onMount`:

```svelte
<script>
  import { onMount } from 'svelte';
  import { loadPlaticaWebchat } from './loadPlaticaWebchat';

  let { publicKey } = $props();
  let container;

  onMount(() => {
    let active = true;
    loadPlaticaWebchat().then(() => {
      if (!active) return;
      window.PlaticaWebchat.mount({ publicKey, target: container, instanceId: 'support' });
    });
    return () => {
      active = false;
      window.PlaticaWebchat?.destroy('support');
    };
  });
</script>

<div bind:this={container} style="height: 600px"></div>
```

In both cases, if the chat goes in a panel of your app, add `fit: 'container'`, `closable: false`, and `theme: 'host'` to `mount`.

## Follow your app's theme

With `data-theme="host"` or `theme: 'host'`, the chat takes your page's theme instead of the published one:

- **Light or dark:** it follows the `light` and `dark` classes, or the `data-theme` attribute, of the root element (`<html>`). If there is none, it uses the system theme.
- **Colors:** if your page defines these CSS variables, it uses them: `--background`, `--foreground`, `--card`, `--muted`, `--muted-foreground`, `--border`, `--brand`, `--brand-foreground`, and `--destructive`. It accepts any color format, even OKLCH. If it does not define them, it uses the chat's light or dark palette with your published main color.
- **Effects:** it turns off the visual effects (`config.appearance.visualEffects`).

When you change your page's theme, the chat changes right away, without restarting the conversation. It saves nothing in the web chat's configuration.

## Events

The chat notifies your page with three events on `window`:

| Event | When | `detail` |
|-------|------|----------|
| `platica:webchat:opened` | The chat opened | `{ publicKey }` |
| `platica:webchat:closed` | The chat closed | `{ publicKey }` |
| `platica:webchat:error` | The chat could not open; for example, because your domain is not in `allowedDomains` | `{ publicKey, message }` |

```js
window.addEventListener('platica:webchat:opened', (event) => {
  console.log('Chat opened', event.detail.publicKey);
});

window.addEventListener('platica:webchat:error', (event) => {
  console.warn('The chat did not open:', event.detail.message);
});
```

<Note type="important">

Events only say whether the chat opened, closed, or failed. They do not carry the conversation's messages: those are read with the API, in [the conversation's messages](/api-reference/conversations/messages).

</Note>

## If it does not appear

- **Your domain is not in `allowedDomains`.** The chat does not open and `platica:webchat:error` is emitted. Add it with [update a web chat](/api-reference/web-chats/update).
- **The page does not match the rules.** `config.behavior.displayRules` says which pages of each domain it appears on.
- **It is paused or turned off.** Check its `health`. With `paused`, `config.enabled` is `false`: send it as `true` and publish. With `disconnected`, you turned it off: [publish it](/api-reference/web-chats/publish) to turn it on.
- **`mode` is `page`.** That way only its Platica page works, not your site. Change it to `both` or `embed`.
