On this page

#Browser SDK

The browser SDK is an npm package with no dependencies, and types included. It takes a publishable vatpub_ key: the MCP's get_status returns it as put_in_the_app.publishable_key. It is one key for development and production alike, and it works from any site or app (Publishable keys).

Terminal window
npm install @vatio-ai/sdk
import { Vatio } from "@vatio-ai/sdk";
const options = { workspace: "acme", token: "vatpub_REPLACE_ME" };
const messages = new Map();
let started = null;
function receive(message) {
messages.set(message.id, message);
// Render messages ordered by ID in your UI, escaping untrusted content.
}
function conversation() {
started ??= Vatio.chat(options).then(async (chat) => {
chat.on("message", receive);
chat.on("typing", (typing) => { /* Update your typing indicator. */ });
chat.on("status", (status) => { /* Update your connection indicator. */ });
chat.on("error", (error) => console.error(error.code, error.message));
for (const message of await chat.history()) receive(message);
return chat;
});
return started;
}
// A returning visitor sees their conversation as soon as the page loads.
if (Vatio.resumable(options)) conversation();
// Wire this to your composer.
async function send(text) {
const chat = await conversation();
await chat.send(text);
}
// Call chat.close() when this view unmounts.

Start the conversation on the visitor's first message, not when the page loads. Vatio.chat starts a new conversation whenever the tab has none stored, and always with fresh: true, so calling it on load opens one for every visit, most of them people who never write. Vatio.resumable answers on load whether there is one to show. A conversation nobody writes in never reaches the inbox and is deleted after a day; see Retention and deletion.

#Without a bundler

A classic script from Vatio's CDN leaves the SDK on window.Vatio:

<script src="https://cdn.vatio.ai/v1/sdk.js"></script>
<script>
const options = { workspace: "acme", token: "vatpub_REPLACE_ME" };
let started = null;
document.querySelector("form").addEventListener("submit", async (event) => {
event.preventDefault();
started ??= Vatio.chat(options).then((chat) => {
chat.on("message", (message) => { /* render it */ });
return chat;
});
const input = event.target.elements.message;
(await started).send(input.value);
input.value = "";
});
</script>

It is always the newest SDK, the same one the widget's script tag carries: a change to the SDK reaches it the moment it is released, with nothing to do on your side. To choose when you upgrade, pin a version the ordinary way — "@vatio-ai/sdk": "^3.0.0" from npm, or the module from any npm CDN:

<script type="module">
import { Vatio } from "https://cdn.jsdelivr.net/npm/@vatio-ai/sdk@3/+esm";
</script>

Next: the reference for every method and option, and the limits every page hits.

Last updated:

Latest release: 0.144.0 · Changelog · The whole contract as Markdown: /docs/docs.md