# Widget JavaScript API

> Control the AskVault chat widget from your own page. Open or close it, send a message, identify the visitor, fire site events and listen for chat events.

Once the widget script is on your page, `window.AskVault` gives your own code a small set of methods and events. Use them to open the chat from your own button, pre-fill a question, tell the widget who is signed in, or react when the visitor sends a message.

## Before you start

- Install the widget on your site. See [Website widget](/channels/website-widget/). The install snippet is a single script tag that carries your widget token.
- The snippet loads the widget with `async`, so your own code can run before `window.AskVault` exists. The widget replays calls that were queued in `window.AskVault._pending` once it loads. To call it from an early inline script, define the queue first:

```html
<script>
  window.AskVault = window.AskVault || { _pending: [] };
  window.AskVault.open = window.AskVault.open || function () { window.AskVault._pending.push({ type: 'open' }); };
</script>
```

## Methods

| Method | What it does |
|---|---|
| `AskVault.open()` | Opens the chat panel. |
| `AskVault.close()` | Closes it. |
| `AskVault.toggle()` | Opens it if closed, closes it if open. |
| `AskVault.sendMessage(text)` | Opens the panel if needed and sends `text` as the visitor. Text is trimmed and cut to 4,000 characters. An empty string does nothing. |
| `AskVault.setUser({ user_id, hash, name, email, plan, attributes })` | Tells the widget who is signed in. `user_id` is required. Add `hash` when identity verification is on. See [Identify signed-in visitors](/developers/identity-verification/). |
| `AskVault.clearUser()` | Forgets the signed-in person, for example on logout, so the next visitor on this browser starts a fresh conversation. |
| `AskVault.track(name, properties)` | Reports a site event, such as `viewed_pricing`. It can trigger your website message rules for that event. |
| `AskVault.setOrder({ order_id, order_key })` | Tells the widget which WooCommerce order the visitor is looking at. |
| `AskVault.on(name, handler)` | Listens for a widget event. Returns a function that removes the listener. |
| `AskVault.off(name, handler)` | Removes a listener. |

### Open the chat from your own button

```html
<button onclick="AskVault.open()">Chat with us</button>
<button onclick="AskVault.sendMessage('Where is my order?')">Where is my order?</button>
```

### Identify a signed-in visitor

```js
AskVault.setUser({
  user_id: "user_12345",
  hash: "64-hex-character-signature",
  name: "Alice Chen",
  email: "alice@yourco.com",
  plan: "growth"
});
```

Compute `hash` on your server, never in the browser. The same fields can be set as `data-user-id`, `data-user-email`, `data-user-name`, `data-user-plan` and `data-user-hash` attributes on the install snippet.

### WooCommerce orders

On a store's thank-you and My Account order pages, `setOrder` lets the visitor ask about that exact order without typing an email first. AskVault checks the order id and key against your store, so a wrong value only means the shopper is asked for their email. The AskVault WordPress plugin calls it for you; it is not in the WordPress plugin directory. See [WooCommerce](/stores/woocommerce/).

## Events

Listen with `AskVault.on`. The handler receives one object.

| Event | When it fires | Detail |
|---|---|---|
| `open` | The panel opens. | none |
| `close` | The panel closes. | none |
| `message` | The visitor sends a message. | `text`, `session_id` |
| `reply` | The agent finishes an answer. | `text`, `message_id`, `session_id` |

```js
const stop = AskVault.on('message', (e) => {
  analytics.track('chat_message', { length: e.text.length });
});
// later: stop();
```

Events are also dispatched on `window` as `askvault:open`, `askvault:close`, `askvault:message` and `askvault:reply`. A handler that throws never breaks the widget.

## Limits

- The widget only runs in the browser. There is no server-side version of this API. To call your agent from a server, use the [REST API](/developers/).
- Method names and arguments are the only supported surface. Underscore-prefixed names such as `_ready` are internal.

## Check it worked

Open your site with the browser console and run `AskVault.open()`. The panel should open. Then run `AskVault.on('reply', console.log)` and ask a question: the answer text is logged when it finishes.

## If it doesn't work

**`AskVault is not defined`.** Your code ran before the widget script. Move your code after the snippet, or use the queue shown above.

**`open()` does nothing.** The widget did not finish loading. Check that the snippet's token is correct and that your domain is allowed for that token.

**`setUser` is refused.** Identity verification is on and the hash does not match. See [Identify signed-in visitors](/developers/identity-verification/).

## Related

- [Website widget](/channels/website-widget/)
- [Identify signed-in visitors](/developers/identity-verification/)
- [AskVault API: get started](/developers/)

Source: https://docs.askvault.co/developers/widget-javascript-api/
