Chat Widget embed
Next.js
Add the Assistrio AI chat widget to a Next.js app with next/script in app/layout.tsx or pages/_document.tsx, and pass signed-in users securely.
Add the code once in the root layout so it's on every route. Keep the stylesheet <link> in <head>, and turn each <script> into a next/script <Script> with strategy="afterInteractive", in the same order. The inline one needs an id and its code as children.
Get your install code
- 1
Open Deploy › Chat Widget
In Assistrio, open your agent, click Deploy in the top bar, and pick Chat Widget.
- 2
Allow your domains
Under Allowed websites, add the domain your app runs on in production (and a staging domain, if you test there). See Allowed websites.
- 3
Copy Snippet
Under Install code, click Copy Snippet. Replace the placeholder code below with yours — it has your real
agentId.
<link rel="stylesheet" href="https://widget.assistrio.com/assistrio-chat.css" />
<script>
window.AssistrioChatConfig = {
agentId: "YOUR_AGENT_ID",
apiBaseUrl: "https://chat.assistrio.com",
position: "right"
};
</script>
<script src="https://widget.assistrio.com/assistrio-chat.js" async></script>Add it to your app
App Router — app/layout.tsx
import Script from "next/script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<link rel="stylesheet" href="https://widget.assistrio.com/assistrio-chat.css" />
</head>
<body>
{children}
<Script id="assistrio-config" strategy="afterInteractive">
{`window.AssistrioChatConfig = {
agentId: "YOUR_AGENT_ID",
apiBaseUrl: "https://chat.assistrio.com",
position: "right"
};`}
</Script>
<Script src="https://widget.assistrio.com/assistrio-chat.js" strategy="afterInteractive" />
</body>
</html>
);
}app/layout.tsx, after {children}.Pages Router — pages/_document.tsx
import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";
export default function Document() {
return (
<Html lang="en">
<Head>
<link rel="stylesheet" href="https://widget.assistrio.com/assistrio-chat.css" />
</Head>
<body>
<Main />
<NextScript />
<Script id="assistrio-config" strategy="afterInteractive">
{`window.AssistrioChatConfig = { agentId: "YOUR_AGENT_ID", apiBaseUrl: "https://chat.assistrio.com", position: "right" };`}
</Script>
<Script src="https://widget.assistrio.com/assistrio-chat.js" strategy="afterInteractive" />
</body>
</Html>
);
}<Script> tags in pages/_document.tsx.- Don't put the code in
page.tsxor a component that re-renders per route — it loads twice. - Only want it on part of the site? Put it in that route group's
layout.tsx(for exampleapp/(marketing)/layout.tsx) instead of the root.
Client-side routing
Load the code once for the whole app — not inside a page component, and don't re-run it on navigation. To hide it on some routes, call window.AssistrioChat?.unmount() and mount() — see the JavaScript API.
Local development
The chat only loads on your allowed websites, so it stays hidden on localhost — that's expected. Test on a preview or staging domain you've added (each agent can have up to 3), or test the agent itself in the Playground.
Check it works
- Deploy, open your site on an allowed domain, and look for the launcher at the bottom right. Send “Hi”.
- In the browser console,
window.AssistrioChat?.isMounted()returnstrue. - Navigate between a few pages: there's still exactly one launcher.
- Nothing? Look for an
[Assistrio Chat]warning in the console — it names the problem (missingagentId, website not allowed, and so on). See Troubleshooting.
Pass a signed-in user
In the App Router, sign the user in the root layout (a Server Component) and pass both values in the config. If users sign in without a full page load, also call window.AssistrioChat?.mount({ userId, userHash }) after sign-in. The hash is HMAC-SHA256 of the user ID with your agent's secret key, made on your server — never put the key in browser code. See Identity verification.
// app/layout.tsx — a Server Component: the key stays on the server.
import crypto from "node:crypto";
import Script from "next/script";
import { getCurrentUser } from "@/lib/auth"; // your auth
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const user = await getCurrentUser();
const config: Record<string, string> = {
agentId: "YOUR_AGENT_ID",
apiBaseUrl: "https://chat.assistrio.com",
position: "right",
};
if (user) {
config.userId = String(user.id);
config.userHash = crypto.createHmac("sha256", process.env.ASSISTRIO_SECRET_KEY!).update(config.userId).digest("hex");
}
return (
<html lang="en">
<head>
<link rel="stylesheet" href="https://widget.assistrio.com/assistrio-chat.css" />
</head>
<body>
{children}
<Script id="assistrio-config" strategy="afterInteractive">
{`window.AssistrioChatConfig = ${JSON.stringify(config)};`}
</Script>
<Script src="https://widget.assistrio.com/assistrio-chat.js" strategy="afterInteractive" />
</body>
</html>
);
}Install with AI
Using Claude Code, Cursor, GitHub Copilot, Windsurf, or another coding assistant? Pick it, copy the prompt, paste it into the tool, then paste your install code where it says. It's written for Next.js. Tip: Deploy › Chat Widget › Install with AI in Assistrio gives the same picker with your code and websites already filled in.
AI tool you use
Add the "Assistrio" AI Chat Widget to my website. What to do: - Add it once in the root layout (app/layout.tsx, or pages/_document.tsx for the pages router), so it shows on every page. - Put the <link> tag in <head>. Turn each <script> into next/script <Script> with strategy="afterInteractive" (the inline one with an id and its code as children), in the same order. - Add it only once. Don't add it twice or load it again on each page change. - Paste the code exactly as it is. Don't change the agentId or the URLs. How to check it works: - Open the website: a Chat Widget shows at the bottom right. - Send it a message like "Hi": the agent answers. - If it doesn't show, check the code is on the page and look for an [Assistrio Chat] message in the browser console. The code: [Paste your install code from Assistrio › Deploy › Chat Widget › Copy Snippet here]
Replace the last line with your install code from Deploy › Chat Widget › Copy Snippet. In Assistrio, Install with AI fills it in for you.
Signing in users too? Add this line under What to do:
- Identity verification is on: on the server, for each signed-in user, set userId to their id and userHash to HMAC-SHA256(userId) in hex, made with the secret key read from an environment variable named ASSISTRIO_SECRET_KEY. For visitors who aren't signed in, leave both out. Never put the secret key in browser code.Content Security Policy
If your site sends a Content-Security-Policy header, add these sources to it. They're everything the Chat Widget and Center Stage load. The inline config <script> also needs to be allowed (with a nonce or hash), or move it into a file you serve yourself.
script-src https://widget.assistrio.com;
style-src https://widget.assistrio.com 'unsafe-inline';
connect-src https://widget.assistrio.com https://chat.assistrio.com;
img-src https: data: blob:;
media-src https://assistrio-public-assets.s3.us-east-1.amazonaws.com blob:;
frame-src https://calendly.com;| Directive | Why the widget needs it |
|---|---|
script-src | The widget script, assistrio-chat.js. |
style-src | The stylesheet, plus the <style> tags the widget puts inside its own shadow root (hence 'unsafe-inline'). |
connect-src | chat.assistrio.com for chat, file uploads, voice, and analytics; widget.assistrio.com for a small location check when the chat starts. |
img-src | Your agent's avatar and uploaded files, product photos from your store's own image host (like cdn.shopify.com), and previews of files a visitor attaches (blob:). |
media-src | Voice messages: recorded ones play from blob:, sent ones from Assistrio's file storage. |
frame-src | Only if you use a Calendly booking action: the booking page opens inside the chat. Cal.com times show in the chat itself, with no frame. |
Note
Using voice messages or dictation? Your site must also allow the microphone (no Permissions-Policy: microphone=() header). For the iframe, keep allow="clipboard-write; microphone" on the tag.
Next steps
Was this page helpful?
Need help? Contact support — we reply within one to two business days.
