JavaScript SDK 1.0.0
Install, style and control your chat with JavaScript. One script, no libraries and no changes to your website build.
Create a Website chat connection, allow your domains and activate a workflow. Copy the snippet from connection settings and place it before </body>. Replace ACCOUNT_ID with the connection UUID.
Instructions for your platform ↗
<script
src="https://api.aitema.cloud/api/v1/chat/widget.js"
data-account="ACCOUNT_ID"
defer
></script>HTTPS is required. The www and non-www addresses work as a pair; add other subdomains separately. HTTP is supported for localhost and 127.0.0.1.
The button starts in the bottom right corner. The root element has the .aitema-widget class. Use one of the four positioning classes or add your own with data-class. Classes can change after loading.
aitema-bottom-rightaitema-bottom-leftaitema-top-rightaitema-top-left<script
src="https://api.aitema.cloud/api/v1/chat/widget.js"
data-account="ACCOUNT_ID"
data-class="my-chat aitema-bottom-left"
defer
></script>Your website CSS can override the root position and --aitema-* variables. Style internal elements with ::part(). Page styles do not automatically enter the chat.
Numeric options and accent passed through data attributes or update() set inline variables. Use !important to override these explicit values in CSS. Position classes work without it.
.my-chat {
--aitema-offset-x: 24px;
--aitema-offset-y: 80px;
--aitema-accent: #167d8d;
--aitema-width: 380px;
--aitema-height: 540px;
--aitema-z-index: 1200;
}
.my-chat::part(launcher) { border-radius: 12px; }
.my-chat::part(panel) { border-radius: 20px; }
.my-chat::part(header) { font-weight: 600; }::part(): launcher, panel, header, close, messages, message, user-message, bot-message, input, send, status.
.my-chat {
--aitema-font: 14px/1.5 system-ui, sans-serif;
--aitema-radius: 16px;
--aitema-launcher-radius: 28px;
--aitema-surface: #ffffff;
--aitema-raised: #f0f4f6;
--aitema-text: #1d303c;
--aitema-muted: #596f7e;
--aitema-border: #d7e0e6;
--aitema-on-accent: #ffffff;
}Use data attributes or update() to change options. locale="auto" selects Russian, English, Spanish or Serbian Latin from the browser. theme="auto" follows the system theme.
<script
src="https://api.aitema.cloud/api/v1/chat/widget.js"
data-account="ACCOUNT_ID"
data-theme="light"
data-locale="en"
data-accent="#167d8d"
data-launcher-text="Ask a question"
data-title="Support"
data-greeting="Hello! How can we help?"
defer
></script>greeting appears in an empty conversation. It does not send a visitor message or start a workflow. A message or visitor choice starts the workflow; write workflow messages in the editor.
| Option | Type / values | Default | Purpose |
|---|---|---|---|
accountdata-account | UUID | — | Connected account UUID |
classNamedata-class | string | "" | Additional CSS classes |
positiondata-position | bottom-right | bottom-left | top-right | top-left | bottom-right | Screen corner |
themedata-theme | auto | light | dark | auto | Color theme |
localedata-locale | auto | ru | en | es | sr | auto | Interface language |
accentdata-accent | #RRGGBB | #6268d9 | Accent color |
offsetX / offsetYdata-offset-x / data-offset-y | number: 0–512 | 22 / 22 | Edge offsets, px |
width / heightdata-width / data-height | number: 280–640 / 300–900 | 360 / 490 | Window width and height, px |
zIndexdata-z-index | number: 1–2147483647 | 10000 | Page stacking level |
launcherVisibledata-launcher | boolean | true | Show the launcher |
launcherTextdata-launcher-text | string ≤ 150 | locale | Launcher label |
title / subtitledata-title / data-subtitle | string ≤ 150 | account / locale | Title and subtitle |
greetingdata-greeting | string ≤ 2000 | locale | Empty chat greeting |
placeholderdata-placeholder | string ≤ 150 | locale | Input placeholder |
storagedata-storage | local | session | none | local | Visitor token storage |
containerdata-container (selector) | selector | Element | ShadowRoot | document.body | Parent element; mount only |
noncedata-style-nonce | string | script.nonce | Style nonce; mount only |
Aitema.get(ACCOUNT_ID) returns the automatically mounted instance. Aitema.mount(options) or new Aitema.Widget(options) creates one manually. A page supports one widget per connected account.
Aitema.ready(sdk => {
const chat = sdk.get("ACCOUNT_ID");
chat.update({
position: "bottom-left",
theme: "light",
locale: "en",
launcherVisible: false
});
document.querySelector("#contact-us")
.addEventListener("click", () => {
chat.open().catch(console.error);
});
});open(): Promise<Widget> | Open the window; resolves after loading the UI. |
close(): Widget | Close the window. |
toggle(): Promise<Widget> | Toggle the window. |
update(options): Widget | Change options without losing the conversation. |
on(name, listener): () => void | Subscribe; returns an unsubscribe function. |
destroy(): void | Remove the instance and release resources. |
Queue a callback when your code runs before the script. After loading, use Aitema.ready(callback).
window.Aitema = window.Aitema || { q: [] };
const configure = sdk => {
const chat = sdk.get("ACCOUNT_ID");
chat.update({ className: "my-chat" });
};
if (Aitema.ready) Aitema.ready(configure);
else Aitema.q.push(configure);// Load widget.js without data-account for manual mounting.
Aitema.ready(sdk => {
const chat = new sdk.Widget({
account: "ACCOUNT_ID",
container: "#support-root",
className: "my-chat",
position: "bottom-right",
storage: "session"
});
chat.element.classList.add("aitema-bottom-left");
// On SPA component cleanup:
// chat.destroy();
});Events bubble from the root element to document. detail always contains account. The message event includes status and response count, without conversation text or tokens.
document.addEventListener("aitema:message", event => {
const { account, status, messagesCount } = event.detail;
console.log({ account, status, messagesCount });
});
Aitema.ready(sdk => {
const chat = sdk.get("ACCOUNT_ID");
const unsubscribe = chat.on("error", event => {
console.log(event.detail.code);
});
// unsubscribe();
});aitema:ready | Button mounted; chat code is still unloaded. |
aitema:open | Window opened. |
aitema:close | Window closed. |
aitema:message | Response processed: status and messagesCount. |
aitema:error | Failure: code and message. |
Installation loads only the button and loader. Chat code, UI, session and history load on the first open. When idle, the widget does not poll the server or run background animations.
| Brotli | gzip | Uncompressed | |
|---|---|---|---|
| Initial load | 2.84 | 3.28 | 9.59 |
| On first open | 4.45 | 5.29 | 13.17 |
JavaScript and CSS are minified. The server sends prebuilt Brotli or gzip. The loader caches for 5 minutes with ETag; the hashed chat module caches for a year. Cached v1 loaders keep working after server updates. No fonts, images, iframes or third-party libraries are loaded.
Load the script once in an SPA. Call update() to change settings and destroy() when removing the widget. Destroy cancels requests and listeners, without deleting server history.
storage controls token storage: local across visits, session within a tab, none in memory only. Signed sessions last 24 hours after renewal. Clearing storage, switching browser or using another origin creates a separate visitor.
Allow api.aitema.cloud in Content Security Policy script-src and connect-src. When inline styles require a nonce, pass the current response nonce in data-style-nonce. Automatic mounting also inherits the script nonce. Keep your existing CSP restrictions.
<script
src="https://api.aitema.cloud/api/v1/chat/widget.js"
data-account="ACCOUNT_ID"
data-style-nonce="RESPONSE_NONCE"
nonce="RESPONSE_NONCE"
defer
></script>