Advanced: embed a bot on your site
Developer-facing tutorial. This page assumes you write code and run a backend. For the no-code path through the ScaiGrid admin UI, see Build a bot with the admin UI.
The quickstart drops a static token into the page. This tutorial covers the production version: tokens issued per visitor by your backend, custom styling, authenticated visitors, and how to set up a content-security policy that doesn't block the widget.
Architecture#
Your server, not your client, talks to ScaiGrid. The widget receives only a short-lived, visitor-scoped token that has no power beyond chatting with the one bot.
1. Server-side token issuance#
Add an endpoint to your backend that mints a token on demand. Cache nothing — each visitor gets their own.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
The response includes token and expires_at. Pass the token to the widget; expiry is informational (the widget refreshes itself automatically if you give it your endpoint).
2. Loading the widget with auto-refresh#
Instead of pasting a static token, point the widget at your token endpoint:
1 2 3 4 5 6 | |
The widget fetches /chat-token on load, then again 60 seconds before expiry. If your visitor is anonymous, the widget passes no extra headers; if authenticated, your endpoint reads session cookies as normal.
3. Visitor identification#
Pass identity attributes so conversations attribute to the right user in analytics:
1 2 3 4 5 6 7 8 9 | |
The token issuance pattern is better — data-user-* attributes are advisory and could be spoofed by a sufficiently determined visitor. Critical attribution should come from the server-issued token, not from HTML attributes.
4. Customising appearance#
Built-in attributes:
1 2 3 4 5 6 7 8 9 10 11 | |
For custom CSS:
1 | |
1 2 3 4 5 6 7 8 | |
Every variable is documented in the widget reference (under Customisation in the admin UI).
5. Embedded-in-page layout#
For a chat panel that lives inside a layout (instead of a floating bubble), mount the widget into a container:
1 2 3 4 5 6 7 8 9 | |
6. CSP#
If you serve under a strict content-security policy, add:
1 2 3 4 5 | |
The widget streams responses over Server-Sent Events; connect-src must allow the ScaiGrid host.
7. Cleaning up across SPAs#
If your site is a single-page app, the widget persists across route changes by default. To unmount it (e.g. on logout):
1 | |
To remount with a new token:
1 | |
Common gotchas#
- 403 on
/chat— token expired and yourdata-token-urlis unreachable. Check your endpoint returns 200 with a fresh token. - Widget visible but never connects — usually CSP blocking
connect-srcto ScaiGrid. Open the browser console; the widget logs a clear error. - Conversations not attributing to users —
data-user-idwas set in HTML but token didn't carry it. Mint tokens withvisitor_idfrom your server. - Widget on top of your nav bar — set
data-z-index="10"(default is 9999, deliberately high). Or usedata-mountwith inline mode.