Payload logoPayload logo

Payload Integration

Embed the Maison concierge chat widget in the website of a Payload 3 project — the Next.js pages that show your Payload content to guests. Payload official site ↗

The widget goes on your website, not in Payload. Payload is a headless CMS: it stores your content and the frontend renders it. In Payload 3 that frontend is the same Next.js app, in the (frontend) route group. Nothing is installed in the Payload admin panel, and there is no Maison plugin for Payload. If your website is a separate app that reads Payload over its API, follow the guide for that app instead: Next.js, React, Vue, Angular or plain HTML.

Prerequisites

  • A Payload 3 project with its website in the same Next.js app (the official website and blank templates are set up this way)
  • Your Client UUID — find it in the Business Console under Settings

Steps

  1. Create a MaisonChat client component

    This is the same component as the Next.js guide. The 'use client' directive must be the first line, because the component uses useEffect and window.

    tsx
    // src/components/MaisonChat.tsx
    'use client';
    
    import { useEffect } from 'react';
    
    export function MaisonChat({ clientId }: { clientId: string }) {
      useEffect(() => {
        const script = document.createElement('script');
        script.src = 'https://maison-labs.com/agent-inject.bundle.js';
        script.setAttribute('data-cfasync', 'false');
        script.setAttribute('data-no-minify', '1');
        script.setAttribute('data-no-optimize', '1');
        script.setAttribute('data-no-defer', '1');
        script.setAttribute('data-noptimize', '1');
        script.onload = () => {
          const sessionId = new URLSearchParams(window.location.search).get('mcbsid');
          const mcbs = (window as any).initMcbs({
            clientId,
            sessionId,
            // locale: 'en-US',                     // Override browser locale
            // iconPosition: { top: 0, bottom: 20, left: 0, right: 20 },  // Chat icon position (px)
            // loadChatWindow: 'load',   // When the chat window loads: 'load' (default), 'interaction', 'lazy' or 'eager'
            // onSessionUpdate: (id) => console.log('Session:', id),
            // onLocaleUpdate: (locale) => console.log('Locale:', locale),
          });
          mcbs.showIcon();
        };
        document.head.appendChild(script);
    
        return() => {
          document.head.removeChild(script);
        };
      }, [clientId]);
    
      return null;
    }
  2. Render it in the frontend root layout

    Open src/app/(frontend)/layout.tsx (in an existing Next.js app, the root layout of whichever route group holds your website) and render MaisonChat as the last child of <body>. Keep everything else in your layout as it is — the website template's providers, header and footer stay where they are.

    tsx
    // src/app/(frontend)/layout.tsx
    import React from 'react'
    import { MaisonChat } from '@/components/MaisonChat'
    import './styles.css'
    
    export default async function RootLayout(props: { children: React.ReactNode }) {
      const { children } = props
    
      return(
        <html lang="en">
          <body>
            <main>{children}</main>
            <MaisonChat clientId="YOUR_CLIENT_UUID" />
          </body>
        </html>
      )
    }
  3. Verify in DevTools

    Open a page of your website (not /admin) and the launcher appears in the corner. In DevTools, the Elements tab shows #maison-chat-icon in the DOM. The admin panel shows no chat icon. No chat icon? Check that the Maison script loaded and that the client UUID is the one for your hotel. An opening (welcome) message is not required: a hotel without one still shows the launcher, and its chat opens empty.

Never add the widget to the admin layout. src/app/(payload)/layout.tsx renders the Payload admin panel. It is generated by Payload and not meant to be edited, and the widget there would show to your editors, not your guests.

Optional: let editors manage the widget in Payload

A Payload Global gives editors one settings screen in the admin panel to switch the chat off or change the Client UUID, without a code change. The layout reads it on the server with Payload's Local API, which skips access control by default, so the Global needs no public read access and stays editable only by signed-in admin users.

  1. Add a Maison Global

    typescript
    // src/globals/Maison.ts
    import type { GlobalConfig } from 'payload'
    import { revalidatePath } from 'next/cache'
    
    export const Maison: GlobalConfig = {
      slug: 'maison',
      label: 'Maison chat',
      fields: [
        {
          name: 'enabled',
          type: 'checkbox',
          label: 'Show the Maison chat on the website',
          defaultValue: true,
        },
        {
          name: 'clientId',
          type: 'text',
          label: 'Maison Client UUID',
        },
      ],
      hooks: {
        // Re-render cached pages so a saved change shows without a redeploy.
        afterChange: [
          ({ doc, req: { context } }) => {
            if(!context.disableRevalidate) revalidatePath('/', 'layout')
            return doc
          },
        ],
      },
    }
  2. Register it in your Payload config

    typescript
    // src/payload.config.ts
    import { Maison } from './globals/Maison'
    
    export default buildConfig({
      // ...the rest of your config
      globals: [Maison], // the website template: [Header, Footer, Maison]
    })
  3. Regenerate your Payload types

    The official templates ship this script (it runs payload generate:types), which adds the maison Global to payload-types.ts so the layout below is typed.

    bash
    pnpm generate:types
  4. Read the Global in the frontend layout

    Replace the fixed Client UUID from step 2. The widget renders only when the box is ticked and a Client UUID is filled in.

    tsx
    // src/app/(frontend)/layout.tsx
    import React from 'react'
    import config from '@payload-config'
    import { getPayload } from 'payload'
    import { MaisonChat } from '@/components/MaisonChat'
    import './styles.css'
    
    export default async function RootLayout(props: { children: React.ReactNode }) {
      const { children } = props
      const payload = await getPayload({ config })
      const maison = await payload.findGlobal({ slug: 'maison' })
    
      return(
        <html lang="en">
          <body>
            <main>{children}</main>
            {maison.enabled && maison.clientId ? <MaisonChat clientId={maison.clientId} /> : null}
          </body>
        </html>
      )
    }
  5. Fill it in

    In the admin panel, open Maison chat under Globals, paste your Client UUID and save. Reload a page of your website: the launcher appears. Untick the box and save to hide it.

Content Security Policy. The official Payload templates send no Content Security Policy (CSP). If your project adds one (in next.config headers or a middleware), add maison-labs.com to its script-src allowlist. When the browser still blocks a request, its console names the directive that blocked it.

For the full list of configuration options, control methods, and callbacks, see the Widget SDK Reference.

Common mistakes
Adding the widget to the (payload) layout. That layout is the admin panel. Guests never see it; the widget belongs in the (frontend) root layout.
Missing 'use client' directive. The layout is a server component and cannot run useEffect. The MaisonChat file must start with 'use client'.
Mounting MaisonChat inside a page. Rendered only on some pages, the widget unmounts and remounts during navigation and injects a new script tag each time. Render it once, in the frontend root layout.
A saved change does not show. Payload's templates cache pages. The afterChange hook in the Global revalidates them; without it, the change appears only after the cache expires or the next deploy.

Not sure if it's set up correctly? Use the Site Diagnostic tool.