JavaScript API
The widget is deliberately small. Most sites never call it: the snippet with data-site-key starts it. The API is for pages that pass a token, control when the bubble is shown, or run inside a single-page app.
window.Sarthi
Pages written for window.Saarthi, the API's name before the brand's spelling was fixed, keep working: it's the same object, and calls queued under either name run.
| Member | What it does |
|---|---|
Sarthi.init(options) | Starts the widget (removing one that is already running). Returns Sarthi, so calls chain. |
Sarthi.open() | Opens the chat panel, starting a chat if there isn't one. |
Sarthi.close() | Closes the panel. The chat continues; the bubble stays. |
Sarthi.destroy() | Removes the bubble and panel from the page. The chat itself is kept in this browser and continues on the next init. |
Sarthi.setTheme(theme) | 'light', 'dark' or 'auto' (the visitor's device): switches the chat's look at once, e.g. from your site's own dark-mode toggle. null goes back to the look chosen in Settings. Returns Sarthi. |
Saarthi.version | '1'. Present once the script has loaded: a cheap way to check. |
init options
| Option | Type | Notes |
|---|---|---|
siteKey | string | Your site key. Defaults to the script tag's data-site-key. Required one way or the other. |
getEndUserToken | () => Promise<string> | For signed-in pages: returns a fresh token from your server each time it's called. See Signed-in customers. |
endUserToken | string | A single token. It works once; after that a new chat needs a page refresh. Prefer getEndUserToken. |
apiBase | string | Where the widget talks to Sarthi. Defaults to the address the script was loaded from. You don't normally set it. |
theme | 'light' | 'dark' | 'auto' | The chat's look on this page. Defaults to the script tag's data-theme, then to Settings → Workspace → Chat look (light unless you change it). |
Passing either token option makes the chat a signed-in chat; without them it is anonymous.
Calling init before the script has loaded
Any call queued like this runs as soon as the script arrives, so you never have to wait for the load event:
<script src="https://api.mysarthi.tech/widget/v1.js" async></script>
<script>
window.Sarthi = { q: [['init', { siteKey: 'ws_acme_7f3a9c' }]] };
</script>
Other methods queue the same way (['open']). When you queue an init, leave data-site-key off the tag so the widget doesn't start twice.
Examples
Open the chat from your own button:
<button onclick="window.Sarthi && window.Sarthi.open()">Chat with us</button>
Start a signed-in chat after a login that happens without a page reload:
window.Sarthi.destroy();
window.Sarthi.init({ siteKey: 'ws_acme_7f3a9c', getEndUserToken: fetchToken });
Only show the bubble on some pages (single-page app): call init on the pages that should have it and destroy() when leaving them. The chat picks up where it left off when the bubble returns.
What the API does not do
There are no callbacks for messages, no custom launcher icons and no way to read the transcript from the page. That's on purpose: the conversation belongs to the visitor and your team, and nothing on the page (including other scripts) can read it. Transcripts, ratings and every turn's reasons are in the console, and events reach your systems through webhooks.