SDK

@kili/react

React SDK

Add Kili's chat widget to a React or Next.js app with @kili/react. Conversations go to your team's Kili inbox in real time.

Requirements

React 18 or 19. react and react-dom are peer dependencies, so your app supplies them. The package ships its own compiled stylesheet, so you don't need Tailwind in your app.

Install

npm install @kili/react

Quick start

Support.tsx
import { KiliChat } from "@kili/react";
import "@kili/react/styles.css";

export function Support() {
  return (
    <KiliChat
      config={{
        apiBaseUrl: "https://api.kili.africa",
        apiKey: "YOUR_API_KEY",
        apiSecret: "YOUR_API_SECRET",
      }}
    />
  );
}

Both imports are required

Keep the styles.css import. Without it the widget renders unstyled. The KiliChat import alone is not enough.

Configuration

KiliChat takes one prop, config, typed as KiliConfig:

OptionTypeRequiredDescription
apiBaseUrlstringrequiredBase URL of the Kili REST API, for example https://api.kili.africa.
socketBaseUrlstringoptionalBase URL of the realtime server. Defaults to apiBaseUrl. Use a wss:// URL if your socket server is separate.
apiKeystringrequiredYour organisation's API key.
apiSecretstringrequiredYour organisation's API secret.

Keep the values in environment variables. In Vite, variables must start with VITE_ to reach the browser:

.env
# .env
VITE_KILI_API_BASE_URL=https://api.kili.africa
VITE_KILI_API_KEY=YOUR_API_KEY
VITE_KILI_API_SECRET=YOUR_API_SECRET
Support.tsx
import { KiliChat } from "@kili/react";
import "@kili/react/styles.css";

const kiliConfig = {
  apiBaseUrl: import.meta.env.VITE_KILI_API_BASE_URL,
  apiKey: import.meta.env.VITE_KILI_API_KEY,
  apiSecret: import.meta.env.VITE_KILI_API_SECRET,
};

export function Support() {
  return <KiliChat config={kiliConfig} />;
}

Keep the key and secret private

Never commit the key or secret. A client-side SDK runs in the browser, so these values are included in your app bundle. The widget also sends them with every request and socket connection, so anyone who loads your page can read them.

Not configurable yet

There are no props for a brand name, greeting text, colours or launcher position. The SDK also can't take a signed-in user's name or email yet.

Placing the widget

The widget's root element is h-dvh w-full, so it always fills the full viewport height. It ignores the height of its container. Use one of these patterns:

A full-page support route

SupportPage.tsx
// A route that renders only the widget, for example /support
import { KiliChat } from "@kili/react";
import "@kili/react/styles.css";

const kiliConfig = {
  apiBaseUrl: "https://api.kili.africa",
  apiKey: "YOUR_API_KEY",
  apiSecret: "YOUR_API_SECRET",
};

export function SupportPage() {
  return <KiliChat config={kiliConfig} />;
}

A full-screen overlay

SupportOverlay.tsx
import { useState } from "react";
import { KiliChat } from "@kili/react";

const kiliConfig = {
  apiBaseUrl: "https://api.kili.africa",
  apiKey: "YOUR_API_KEY",
  apiSecret: "YOUR_API_SECRET",
};

export function SupportOverlay() {
  const [open, setOpen] = useState(false);

  return (
    <>
      <button onClick={() => setOpen(true)}>Chat with us</button>
      {open && (
        <div className="fixed inset-0 z-50 bg-background">
          <KiliChat config={kiliConfig} />
          {/* The widget has no close button. Add one that doesn't cover its header. */}
          <button onClick={() => setOpen(false)}>Close</button>
        </div>
      )}
    </>
  );
}

A fixed-size panel won't work cleanly

Placing the widget in a fixed-height panel or a small drawer doesn't work. The root stays at full viewport height, and overflow-hidden clips its bottom, which can hide the message composer. Use the full-page or full-screen overlay pattern instead.

Next.js

The build starts with a "use client" banner, so you can import KiliChat directly into an App Router page. Its first render doesn't read localStorage or sessionStorage. Those are only read after mount, in effects and event handlers, so a server render is safe.

app/support/page.tsx
// app/support/page.tsx
import { KiliChat } from "@kili/react";
import "@kili/react/styles.css";

const kiliConfig = {
  apiBaseUrl: "https://api.kili.africa",
  apiKey: process.env.NEXT_PUBLIC_KILI_API_KEY!,
  apiSecret: process.env.NEXT_PUBLIC_KILI_API_SECRET!,
};

export default function SupportPage() {
  return <KiliChat config={kiliConfig} />;
}

If a server render does report an error from browser storage, load the widget on the client only. In the App Router, ssr: false works only inside a Client Component:

components/SupportChat.tsx
"use client";

import dynamic from "next/dynamic";

// Use this only if a server render reports an error from localStorage or sessionStorage.
const KiliChat = dynamic(() => import("@kili/react").then((m) => m.KiliChat), { ssr: false });

export default KiliChat;

Dark mode

Add a dark class to any ancestor element. The stylesheet includes both light and dark themes.

tsx
<div className="dark">
  <KiliChat config={kiliConfig} />
</div>

Styling notes

The stylesheet changes global styles

@kili/react/styles.css includes Tailwind's base styles (preflight). Preflight resets margins, heading sizes, lists and buttons across the whole page. The stylesheet also loads Space Grotesk and DM Sans from Google Fonts. Import it only on the route that renders the widget, so the rest of your app keeps its own styles.

Building your own UI

To compose the widget yourself, call configureKili(config) first. Then build on the exports: AppStateProvider and useApp, ChatWidget, ChatHome, ChatThread, InitialsAvatar, Button, Textarea, SocketProvider, chatService, coreService, SocketEvent, and the Chat, Message and Customer types.

KiliChat also runs the bootstrap: it creates the customer, sets the HMAC, registers the customer and loads their chats. A custom composition has to make those four calls itself, as shown below.

CustomSupport.tsx
import { useEffect } from "react";
import {
  AppStateProvider,
  ChatWidget,
  chatService,
  configureKili,
  coreService,
  useApp,
} from "@kili/react";

configureKili({
  apiBaseUrl: "https://api.kili.africa",
  apiKey: "YOUR_API_KEY",
  apiSecret: "YOUR_API_SECRET",
});

// Runs the same bootstrap as KiliChat.
function Bootstrap() {
  const state = useApp();

  useEffect(() => {
    const run = async () => {
      const customer = await coreService.createCustomer();
      await coreService.setCustomerHmac();
      const auth = await coreService.registerCustomer();

      state.setUser(customer);
      state.setAccessToken(auth.accessToken);

      const chats = await chatService.getAllChats(customer.id);
      state.setChats(chats.data);
    };
    run();
  }, []);

  return null;
}

export function CustomSupport() {
  return (
    <AppStateProvider>
      <Bootstrap />
      <ChatWidget />
    </AppStateProvider>
  );
}

Troubleshooting