Lynkist Guides
Website Chat

Install in React or Next.js

Add Lynkist Website Chat to a React or Next.js app with the @lynkist/chat-widget npm package.

The @lynkist/chat-widget package loads the same chat as the snippet, once, and gives you typed calls and React hooks. It does not bundle the chat, so the chat is always the latest version. It is safe in server rendering.

  1. Install the package:

    npm install @lynkist/chat-widget
  2. In Lynkist, open Channels → Website Chat, open your widget and go to the Install tab. Copy the widget key (it starts with wk_). The React or Next.js part of that tab has the code below with your key in it.

  3. Put LynkistChat once in your root layout. It renders nothing; the chat draws its own button.

    // app/layout.tsx
    import { LynkistChat } from '@lynkist/chat-widget/react'
    
    export default function RootLayout({ children }: { children: React.ReactNode }) {
      return (
        <html lang="en">
          <body>
            {children}
            <LynkistChat widgetKey="wk_…" />
          </body>
        </html>
      )
    }
  4. Add your domain in Security → Allowed websites. To test on your dev server first, switch on Allow localhost for testing in the same tab.

The chat follows route changes of your app on its own.

Hide the chat on a screen

import { useHideChat } from '@lynkist/chat-widget/react'

function CheckoutPage() {
  useHideChat() // no chat while this page is open
  // …
}

Lift the button above a bottom bar

A cookie banner or a sticky "Add to cart" bar sits where the chat button is. Lift the button above it while the bar shows:

import { useChatOffset } from '@lynkist/chat-widget/react'

function CookieBanner({ height }: { height: number }) {
  useChatOffset(height)
  // …
}

A "Chat with us" button

import { useLynkistChat } from '@lynkist/chat-widget/react'

function HelpButton() {
  const chat = useLynkistChat()
  return <button onClick={() => chat.open()}>Chat with us</button>
}

Nothing shows? Open the browser console: the chat writes the reason there, starting with [Lynkist]. You can also listen for it with useChatEvent('error', (e) => …).

Content-Security-Policy. Add https://chat.lynkist.io to script-src and frame-src. With 'strict-dynamic' and a nonce on your app's scripts, nothing else is necessary, because the package adds the chat from your own code. With a nonce but no 'strict-dynamic', pass it: <LynkistChat widgetKey="wk_…" nonce={nonce} />.

Install in React or Next.js | Lynkist Guides