> For the complete documentation index, see [llms.txt](https://docs.limio.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.limio.com/agents/chat-embed/api-reference.md).

# JavaScript API

Control the embedded chat widget from your own JavaScript

Once the snippet has loaded, `window.LimioChat` is available on the page. Use it to open chat from your own buttons, react to widget events, or switch identity in a single-page app.

## Waiting for the widget

The snippet loads with `defer`, so `LimioChat` doesn't exist while your own early scripts run. Use the `onReady` callback:

```html
<script>
  window.LimioChatConfig = {
    onReady: (chat) => {
      document
        .querySelector("#talk-to-sales")
        .addEventListener("click", () => chat.open())
    }
  }
</script>
```

## Methods

| Method                | What it does                                                                                                                                                                                                                           |
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `open()`              | Opens the chat panel. With `data-fab="never"` this also loads the widget on first call.                                                                                                                                                |
| `close()`             | Closes the panel.                                                                                                                                                                                                                      |
| `toggle()`            | Opens if closed, closes if open.                                                                                                                                                                                                       |
| `identify(jwt)`       | Switches to an identified session using a signed [identity assertion](https://docs.limio.com/agents/chat-embed/identity.md). Restarts the conversation under the new identity. Returns a promise; rejects if the assertion is invalid. |
| `logout()`            | Switches to a fresh guest session. Returns a promise.                                                                                                                                                                                  |
| `destroy()`           | Removes the widget from the page and clears the stored session.                                                                                                                                                                        |
| `on(event, listener)` | Subscribes to a widget event. Returns an unsubscribe function.                                                                                                                                                                         |

## Events

```javascript
const unsubscribe = LimioChat.on("open", () => {
  analytics.track("chat_opened")
})
// later, e.g. in a React effect cleanup:
unsubscribe()
```

| Event             | Fires when                                                                         |
| ----------------- | ---------------------------------------------------------------------------------- |
| `ready`           | The widget has loaded and the API is usable                                        |
| `open`            | The chat panel opens, whether by the visitor or by `open()`                        |
| `close`           | The chat panel closes                                                              |
| `identityChanged` | `identify()` or `logout()` completed; payload is `{ identity: "user" \| "guest" }` |

{% hint style="info" %}
**Single-page apps:** `on()` returns an unsubscribe function for a reason: if you subscribe inside a component effect that re-runs, call it on cleanup or listeners accumulate for the lifetime of the page.
{% endhint %}

## Session storage

The visitor's chat session token lives in your site's localStorage under `limio.chat.<api-host>.session`. It lasts 24 hours and renews automatically. Clearing site data resets the visitor to a fresh guest. The token only grants access to that visitor's own chat conversation; it carries no other permissions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.limio.com/agents/chat-embed/api-reference.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
