> 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/ai/chat-embed/api-reference.md).

# JavaScript API

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](/ai/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
