> ## Documentation Index
> Fetch the complete documentation index at: https://paperlinkai.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript API

> The methods on the widget instance that init returns.

`window.PaperlinkAIChat.init` mounts the widget and returns an instance. Use the instance to open or close the widget from your own buttons. Use it also to clean up in a single-page app.

```ts theme={"dark"}
window.PaperlinkAIChatQueue = window.PaperlinkAIChatQueue || [];
window.PaperlinkAIChatQueue.push((PaperlinkAIChat) => {
  const chat: PaperlinkAI.ChatInstance = PaperlinkAIChat.init(options);
});
```

The script tag with `data-api-key` starts the widget for you. It stores the instance on `window.PaperlinkAIChatInstance`. For the types, see [TypeScript types](https://paperlinkai.com/docs/developers/install/script-tag.md#typescript-types).

## Methods

**open()** `void`

Opens the widget panel. In inline mode, this method does nothing, because the panel is always visible.

**close()** `void`

Closes the widget panel. In inline mode, this method does nothing.

**resetSession()** `void`

Clears the stored session of the visitor in the browser. The visitor then starts a new conversation.

**destroy()** `void`

Stops the widget and removes it from the page. Call it when your page or component unmounts.

## Example: open the widget from your own button

Add a button to your page. Call `open()` when a visitor selects it.

```html theme={"dark"}
<button id="need-help">Need help?</button>

<script>
  document.getElementById('need-help').addEventListener('click', () => window.PaperlinkAIChatInstance?.open());
</script>
```

The widget script loads with `async`, so read the global inside the handler.

If you start the widget from your code, keep the instance that `init` returns. `window.PaperlinkAIChatInstance` holds only the widget that the tag starts.

```html theme={"dark"}
<script src="https://api.paperlinkai.com/widget.js" async></script>
<script>
  window.PaperlinkAIChatQueue = window.PaperlinkAIChatQueue || [];
  window.PaperlinkAIChatQueue.push(function (PaperlinkAIChat) {
    const chat = PaperlinkAIChat.init({ apiKey: 'pl_live_...' });
    document.getElementById('need-help').addEventListener('click', () => chat.open());
  });
</script>
```

> [!NOTE]
>
> The instance has only the four methods above. These features do not exist: `toggle`, `sendMessage`, `identify` and an event emitter with `on` and `off`. To react to widget activity, use the `onAnalyticsEvent` option.

> **[Analytics events](https://paperlinkai.com/docs/developers/configure/analytics-events.md)**
>
> See the events that the widget reports to your own analytics.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.