The JavaScript API
For the pages that need to decide when the widget appears — and only those.
TipMost sites should not read this page
data-api-key starts the widget on its own. Calling init() yourself means owning the lifecycle: mounting, unmounting, and not doing either twice. Reach for it when the tag genuinely cannot answer the question, not by default.When it earns its cost
| The agent depends on who is signed in | A different agent for staff than for visitors, decided after your app knows which — before the widget starts. |
|---|---|
| A single-page app that tears views down | You want destroy() on the way out rather than a widget outliving the page that asked for it. |
| Consent | Nothing loads until the visitor has accepted. |
| A kiosk or shared screen | autoDeleteChats clears the thread on a timer, and it is only available here. |
Loading the bundle without starting it
<script src="https://cdn.tigerbase.cloud/v2" defer></script>A tag without data-api-key loads the bundle and starts nothing. window.TigerbaseAgent is then yours, with exactly four functions on it.
The four functions
init(options)
Reads the agent's configuration and claims the page for it. Resolves true when it started the agent, and false — rather than throwing — if the page already has an agent on it. It throws only if apiKey or mcpUrl is missing.
const started = await TigerbaseAgent.init({
apiKey: "tb_your_key_here",
mcpUrl: "https://api.tigerbase.cloud/mcp",
target: "#support", // optional, for the inline layout
autoDeleteChats: 30, // optional, minutes
});
if (started) TigerbaseAgent.render();| apiKey | Required. The agent's publishable tb_ key. |
|---|---|
| mcpUrl | Required. Where the backend is: https://api.tigerbase.cloud/mcp. The tag fills this in for you; here you pass it. |
| target | Optional. A CSS selector to render inside — the same as data-target on the tag. Pair it with the Inline layout. |
| autoDeleteChats | Optional. Minutes after the widget starts at which the thread on screen clears itself, once. |
| defaultLang | "ar" or "en". Beats both the agent's Language setting and the page's <html lang>. Leave it out to follow the dashboard. |
| apiSecret, aiKey, style | Accepted from older embeds and ignored. Leave them out of new code — see below. |
WarningCheck what init returned before calling render
init() resolves false it has not set anything up, and render() after it throws Tigerbase not configured. The if (started) above is not decoration.render()
Mounts the widget: into the target element if you gave one and it exists, otherwise as an overlay on <body>. Must come after init(). Called a second time, it warns and does nothing.
TigerbaseAgent.render();destroy()
Unmounts it, stops every timer, listener and request it started, and releases the page so a later init() is not mistaken for a duplicate tag.
TigerbaseAgent.destroy();getBrowserId()
The id this visitor's conversations are stored against — tb- followed by a UUID, kept in localStorage so it is stable across reloads. Where storage is blocked (some private windows), each call makes a new one.
const id = TigerbaseAgent.getBrowserId();
// "tb-3f1c9e2a-…"Recipes
Start only after consent
function startChat() {
const tag = document.createElement("script");
tag.src = "https://cdn.tigerbase.cloud/v2";
tag.onload = async () => {
const started = await TigerbaseAgent.init({
apiKey: "tb_your_key_here",
mcpUrl: "https://api.tigerbase.cloud/mcp",
});
if (started) TigerbaseAgent.render();
};
document.body.appendChild(tag);
}Until startChat() runs, nothing of ours is on the page — not the bundle, not a request.
Mount and unmount with a view
import { useEffect } from "react";
declare global {
interface Window { TigerbaseAgent: any }
}
export function SupportView() {
useEffect(() => {
let cancelled = false;
window.TigerbaseAgent.init({
apiKey: "tb_your_key_here",
mcpUrl: "https://api.tigerbase.cloud/mcp",
target: "#support",
}).then((started: boolean) => {
if (started && !cancelled) window.TigerbaseAgent.render();
});
return () => {
cancelled = true;
window.TigerbaseAgent.destroy();
};
}, []);
return <div id="support" style={{ height: "32rem" }} />;
}Both assume the bundle tag (without data-api-key) is already on the page, and an agent whose layout is set to Inline.
Switching agents on a page
destroy() forgets the agent as well as unmounting it, so init() afterwards starts whichever key you pass — the same one again (which is also how you pick up a changed <html lang>) or a different one. A single-page app that shows a sales agent to visitors and a support agent to signed-in customers switches without a page load.
async function switchAgent(apiKey) {
TigerbaseAgent.destroy();
const started = await TigerbaseAgent.init({
apiKey,
mcpUrl: "https://api.tigerbase.cloud/mcp",
});
if (started) TigerbaseAgent.render();
}
switchAgent("tb_support_key_here");NoteThe visitor id carries over
getBrowserId() is the same before and after, so the new agent sees the same visitor — and starts its own conversation with them, since a conversation belongs to one agent.CarefulTwo tags is not the way to run two agents
apiSecret is accepted and ignored
Older embeds passed a secret alongside the key. The widget path authenticates on the key and the embedding domain, and a credential shipped in a public page's source was never secret — passing one still works so existing embeds keep running, and it does nothing. New integrations should leave it out. The same goes for aiKey, which now prints a deprecation warning: the model key lives on the server.