Docs
JavaScript API
window.nikoori. Seven methods, two read-only properties, six events. That is all of it — enough to let your own chatbot do the thinking and leave the face to us.Methods and properties
| Signature | What it does |
|---|---|
| speak(text, options?) | Make the character say text: lip sync, an expression, and a visible line. options.emotion sets the face; leave it out and it is guessed from the words. An empty or whitespace-only string does nothing. In chat mode the line also joins the transcript, so your AI keeps it as context; if the panel is closed a teaser bubble appears (trimmed to 140 characters). In avatar mode a speech bubble appears for about four seconds plus reading time, and the character expands if she was collapsed.Call it repeatedly to stream. Chunks arriving within about a second of each other are spoken as one continuous turn instead of interrupting one another, so you can forward a model's tokens straight through. The tail is spoken when the chunks stop. options.more says so explicitly — the turn then stays open however long the next chunk takes, which a slow model needs and a timer cannot guess. Send the last chunk without it, or call stop(). A turn held open by more is flushed after ten seconds anyway, so a forgotten final chunk costs a tail rather than a voice. |
| stop() | Silence her immediately and drop anything queued — barge-in, for when the visitor starts talking or typing over a long answer. In avatar mode the speech bubble goes with it. If your page was mid-answer, what it managed to say is what the transcript keeps. |
| open() | Open the chat panel, or expand the character in avatar mode. Fires the open event. |
| close() | Close the panel, or collapse the character back to a small button. Fires the close event. |
| setEmotion(emotion) | One of happy, angry, sad, relaxed, surprised, welcoming, pleasing, cute, neutral. An unknown value logs a warning and changes nothing. The expression holds while she speaks and for about five seconds after she stops, then relaxes to neutral. |
| on(event, callback) | Subscribe to ready, open, close, reply, voice or error. Returns a function that unsubscribes. Subscribing to ready after it has already fired calls you straight away, so you never miss it. |
| destroy() | Remove the widget, its listeners and its WebGL context, and delete window.nikoori. Anything you call afterwards is ignored. To bring it back, insert the script tag again. |
| ready | Read-only boolean: true once the key has been accepted and the character's configuration has arrived. |
| version | Read-only string, e.g. "0.1.0+fcd1bc4c2e" — the build, then the hash of the 3D chunk. Worth quoting in a support email. |
nikoori.speak("Your order shipped this morning.");
nikoori.speak("Sorry, that item is out of stock.", { emotion: "sad" });
nikoori.setEmotion("surprised");
nikoori.open();
nikoori.close();Events
| Event | Detail | When |
|---|---|---|
| ready | — | The key was accepted and the widget is on the page. |
| open | — | The panel (or character) was opened, by the visitor or by your code. |
| close | — | The same, closed. |
| reply | { text, emotion, source } | A line finished arriving. source is "chat" for a streamed AI reply or "speak" for one of your own speak() calls. |
| ask | { text } | The visitor asked something and you are expected to answer — fired only with data-brain="page". Nothing is sent to our servers: no request, no storage, no conversation counted. Answer by calling speak(), once or once per token. See Next to your chat tool. |
| voice | { ready, realTimeFactor } | She can now answer out loud: the speech model has arrived and this device is fast enough for it. It fires once, or not at all — a character with no voice configured, a visitor who declined the download, or a device too slow to keep up never sees it, and nothing else about the widget changes. |
| error | { code, message } | Something was refused or failed. The codes are listed on Troubleshooting. |
const off = nikoori.on("reply", (detail) => {
console.log(detail.source, detail.emotion, detail.text); // "chat" | "speak"
});
nikoori.on("ready", () => console.log("widget live, version", nikoori.version));
nikoori.on("open", () => console.log("panel opened"));
nikoori.on("close", () => console.log("panel closed"));
nikoori.on("error", (e) => console.warn("nikoori:", e.code, e.message));
off(); // unsubscribeStreaming an answer from your own chatbot
Set data-brain="page" on the script tag and the widget stops answering altogether: it emits ask with what the visitor said and waits for you. Nothing reaches our servers — no request, no stored text, no conversation counted — so your knowledge base, your model key and your order system stay where they are. The face, the voice and the lip sync are the only parts we run.
// Your chatbot answers; the character says it as it arrives.
nikoori.on("ask", async ({ text }) => {
const res = await fetch("/my-bot", { method: "POST", body: text });
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
for (;;) {
const { value, done } = await reader.read();
if (done) break;
nikoori.speak(value, { more: true }); // the turn stays open
}
nikoori.speak("", { more: false }); // ...and closes here
});
// The visitor talks over her:
nikoori.stop();It works in both modes. In avatar mode the question comes from the bubble beside the character; in chat mode it comes from the panel, and your chunks arrive in the transcript with the same pacing, sentence chunking and mouth a model's tokens would get — there is no second code path.
stop() when a visitor interrupts, and send your last chunk without more so the final sentence is spoken promptly.Calling before the script has loaded
With async, your own code may run before embed.js does. Two rules cover it:
- Calls made after the script tag has executed are queued internally and replayed, in order, as soon as the key check finishes. You do not have to wait for
ready. - To call it before that — from a chat tool that loads first, say — add the stub below above everything else. Anything you call on it is replayed in order once the real widget is ready.
<script>
window.nikoori = window.nikoori || { q: [] };
["speak", "stop", "open", "close", "setEmotion", "on", "destroy"].forEach(function (m) {
window.nikoori[m] = window.nikoori[m] || function () { window.nikoori.q.push([m, arguments]); };
});
</script>On the stub, on() cannot return an unsubscribe function — it has nothing to unsubscribe from yet. Call on() again on the real API if you need one.
Opening from your own button
The guard matters: on a page where the script was blocked or destroyed, window.nikoori simply is not there.
<button type="button" onclick="window.nikoori && window.nikoori.open()">
Chat with us
</button>Single-page apps
// A single-page app leaving the page the widget belongs on:
window.nikoori?.destroy(); // window.nikoori is undefined afterwardsTypeScript
There is no npm package yet. Declare the global yourself; this matches what the widget installs.
type NikooriEmotion = "happy" | "angry" | "sad" | "relaxed" | "surprised" | "welcoming" | "pleasing" | "cute" | "neutral";
type NikooriEvent = "ready" | "open" | "close" | "reply" | "voice" | "error";
interface NikooriApi {
speak(text: string, options?: { emotion?: NikooriEmotion }): void;
open(): void;
close(): void;
setEmotion(emotion: NikooriEmotion): void;
on(event: NikooriEvent, cb: (detail?: unknown) => void): () => void;
destroy(): void;
readonly ready: boolean;
readonly version: string;
}
declare global {
interface Window { nikoori?: NikooriApi }
}
export {};