How to add an AI chatbot to a Next.js app
In Next.js, load the widget with next/script in the root layout, so it appears on every page. You don't need an npm package. Get a key at /get-api-key, or have your agent call the keys API.
App Router: app/layout.tsx
import Script from 'next/script';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script src="https://onelinechat.com/widget.js" data-key="olc_pub_YOUR_KEY" strategy="afterInteractive" />
</body>
</html>
);
}
Pages Router: pages/_app.tsx
import Script from 'next/script';
import type { AppProps } from 'next/app';
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
<Script src="https://onelinechat.com/widget.js" data-key="olc_pub_YOUR_KEY" strategy="afterInteractive" />
</>
);
}
Notes
next/scriptpassesdata-*attributes through to the script tag, sodata-color,data-greetinganddata-positionwork here too.- The key is public, so put it straight into the component. You don't need a
NEXT_PUBLIC_environment variable, although one works if you prefer. - Vercel preview deployments (
*.vercel.app) are allowed automatically.