brandfinedocs
SDK

Live Chat

Put the Brandfine Live Chat bubble on your site — fetch the bootstrap at build or render time, inject the widget in the page.

bf.liveChat renders the chat widget for this workspace. It's a server-half / client-half pair, same shape as analytics: your server or build fetches the bootstrap, your page injects the widget. Both use the site key (brandfine_pk_…), which is safe anywhere.

// Server half (RSC / build step)
import { createBrandfineClient } from '@brandfine/client'

const bf = createBrandfineClient({
  baseUrl: process.env.BRANDFINE_API_URL!,
  apiKey: process.env.BRANDFINE_API_KEY!,   // the site key
})
const config = await bf.liveChat.getConfig()
// → pass `config` to your client component as a prop
// Client half — no secrets, pure DOM injection
'use client'
import { useEffect } from 'react'
import { createBrandfineClient, type LiveChatBootstrap } from '@brandfine/client'

export function LiveChatClient({ config }: { config: LiveChatBootstrap | null }) {
  useEffect(() => {
    if (!config) return
    const bf = createBrandfineClient({
      baseUrl: process.env.NEXT_PUBLIC_BRANDFINE_API_URL!,
      apiKey: 'unused', // install() never sends it
    })
    void bf.liveChat.install({ config })
  }, [config])
  return null
}

Drop the server component once in your root layout. After that, everything is controlled from the CMS — activating, deactivating, greeting, theme all take effect on the visitor's next page load, with no redeploy of your site. The widget's first runtime call re-checks the enabled state, so even static-export sites (where the server half runs at build time) follow the CMS toggle live.

The same applies to the workspace's AI assistant: enable it in the CMS and the widget needs nothing from you — assistant replies arrive like agent messages, automatically labeled AI assistant in the bubble.

API

getConfig() → Promise<LiveChatBootstrap>

Meant for your server or build step — calls GET /external/live-chat/bootstrap. Resolves to:

{ enabled: false }
// or
{
  enabled: true,
  publishableKey: string,    // brandfine_pk_… — safe in public HTML
  greeting: string | null,
  offlineMessage: string | null,
  theme: Record<string, string> | null,   // --bf-chat-* CSS vars
  scriptPath: string,        // widget bundle path, relative to baseUrl
}

{ enabled: false } means the workspace hasn't activated Live Chat (or toggled it off) — pass it through; install() no-ops.

install(opts) → Promise<LiveChatInstallResult>

Client-side half. Appends the widget host (<div data-bf-live-chat data-publishable-key="…">) and the widget <script> to the page. Idempotent via a marker attribute — safe under StrictMode double-invokes and SPA re-mounts. Resolves to:

{ installed: true }
{ installed: false, reason: 'disabled' }           // config.enabled === false
{ installed: false, reason: 'ssr' }                // no document — call client-side
{ installed: false, reason: 'already-installed' }  // prior call won

Unlike analytics.install(), the config option is required: fetch it with getConfig() on your server or at build time, so the page doesn't wait on an extra request before showing the bubble.

Verified visitors (signed identity)

On pages where the visitor is already signed in, label the chat with who they are — the inbox shows their name with a ✓ Verified marker, and their conversation continues across devices and sessions (same externalId = same person).

Identity is signed on your server with the workspace's identity secret (workspace page → Access → Identity secret). Give it to the client once; it never goes in a page.

// Server half — sign the signed-in user.
const bf = createBrandfineClient({
  apiKey: process.env.BRANDFINE_API_KEY!,
  identitySecret: process.env.BRANDFINE_IDENTITY_SECRET!, // server-only
})
const [config, identityToken] = await Promise.all([
  bf.liveChat.getConfig(),
  bf.liveChat.identityToken(user.id),
])
const visitor = {
  externalId: user.id,
  name: user.name,
  email: user.email,
  attributes: { plan: user.plan },
  identityToken,
}
// → pass BOTH config and visitor to your client component
// Client half — serialization only, no crypto
void bf.liveChat.install({ config, visitor })

An invalid or missing identityToken never breaks the chat — the conversation silently starts anonymous (and the failure is logged on the Brandfine side), so a misconfigured integration degrades to today's behavior instead of blocking visitors. Omit visitor entirely on public pages: anonymous chat is unchanged.

identityToken() is server-only: it throws when called in a browser context. The secret comes from { secret }, then the client's identitySecret, then BRANDFINE_IDENTITY_SECRET. With a legacy workspace key (brandfine_… without _pk_) and no secret it still derives one from that key, as it always did. Rotate the identity secret on the workspace page if it ever leaks; the WordPress plugin picks up a rotation by itself within 12 hours.

Localization

Configure per-locale greetings and away messages in the CMS (Inbox → Settings → Live chat → Messages). The widget asks for the rendered page's language automatically — <html lang> first, browser language second — so a bilingual site shows each visitor the right text with no extra code. To pin it explicitly (e.g. your i18n router's active locale):

bf.liveChat.install({ config, locale })

Resolution is server-side: exact tag → primary subtag (tr-TR → tr) → the default text. The page language is also stored on the conversation, so operators see which language the visitor expects before reading a word.

Building your own chat UI

install() renders Brandfine's floating bubble. If chat should live inline in your own design — a panel in the page flow — use createSession(): the same conversation, identity and threading, with the transcript exposed as typed, subscribable state and the pixels entirely yours.

One thing a custom UI owns that the widget does for you: links. Message bodies are plain text, and the pages the assistant recommends arrive as data alongside them — so anchors are yours to render. It is a few lines with segmentChatMessage(); see Links in messages.

Installing

The bubble goes on a site through this SDK (above) or the Brandfine WordPress plugin (turn on Live Chat in its settings). There is no paste-in snippet. Everything else (greeting, hours, AI assistant, theme) is set in the CMS under Inbox → Settings → Live chat and reaches the site on the next page load.

Theming and wording

The widget renders in a Shadow DOM; your site's CSS can't break it and vice versa. You can still make it look like part of your site, and say what you want it to say, from install():

await bf.liveChat.install({
  config,
  appearance: {
    // Any --bf-chat-* variable. Point them at your own design tokens
    // and the chat follows your theme.
    theme: {
      '--bf-chat-primary': 'var(--brand-600)',
      '--bf-chat-font': 'var(--font-sans)',
      '--bf-chat-radius': '20px',
    },
    launcher: 'bubble',          // or 'button' / 'input', in place
    launcherText: 'Ask us anything',
    title: 'Talk to the clinic',
    subtitle: 'We answer within the hour',
    aiName: 'Assistant',
    footerNote: 'Please do not share medical records here.',
    avatarAi: '/avatars/assistant.png',
    avatarTeam: '/avatars/team.png',
    // For what the variables can't do: copied inside the widget.
    css: '.bf-chat-launcher { box-shadow: none; }',
  },
})

Variables: --bf-chat-primary, --bf-chat-primary-contrast, --bf-chat-surface, --bf-chat-text, --bf-chat-muted, --bf-chat-border, --bf-chat-visitor-bubble, --bf-chat-radius, --bf-chat-font, --bf-chat-offset, --bf-chat-z, --bf-chat-whatsapp (the WhatsApp buttons) and --bf-chat-whatsapp-bubble (the WhatsApp bubble). Your stylesheet can set them too, on the host element install() adds:

[data-bf-live-chat] {
  --bf-chat-primary: #0b1f2a;
  --bf-chat-radius: 10px;
}

Which value wins, strongest first:

  1. A theme set for the workspace in Brandfine (through its API): the widget applies it inside itself.
  2. appearance.theme (an inline style on the host).
  3. Your stylesheet's rules on [data-bf-live-chat].
  4. The widget's defaults.

css can restyle the widget's own .bf-chat-* classes. They are not a promised interface and may change; prefer the variables, and keep css to small touches.

Chat, WhatsApp or both

What the bubble opens is a setting, not code: Live Chat settings → Messages → How visitors reach you.

  • The chat (default): the chat panel.
  • Your WhatsApp: the workspace's connected number. On a phone the bubble opens WhatsApp with the first message ready; on a computer it opens a card with a QR code to scan and a WhatsApp Web button. Turn on WhatsApp in the AI assistant tab and the assistant answers there. Bubble icon keeps the chat bubble in your colour (the default) or shows WhatsApp's logo on its green. WhatsApp panel title and Logo brand the card a computer's visitor sees above the QR code (the title can differ per language under Other languages; a white logo reads best on your brand colour).
  • A choice of both: the bubble opens into two buttons, WhatsApp and the chat (on hover with a mouse, on tap on a phone).

Show the greeting beside the bubble pops the greeting up a few seconds after a page opens, until the visitor closes it or uses the bubble (once per browser session). The widget follows both settings on the next page load; nothing changes in your site's code. Inline launchers (data-launcher="button" or "input") always open the chat.

When a visitor picks WhatsApp, the widget sends whatsapp_click to Google Analytics (method: app on a phone, qr when the QR card opens on a computer, continue from the chat panel's "Continue on WhatsApp") and emits whatsapp on window.BrandfineLiveChat for your own scripts. A visitor opening a link the assistant gave (a link set up under Links it can give) sends chat_link_click and emits link ({ label, url }).

On this page