TypeScript and React
The @senlerio/widget package contains API types, a loader, and optional React components. Basic integration requires neither React nor @senlerio/ui.
Install the package
Use the stable SDK 3.0.0 from the v3.0.0 tag, a loader and iframe using protocol 10, and an API supporting v3. The production loader is available at https://widget.senler.io/v3/widget-loader.js. For a local or test host, use its /v3/widget-loader.js URL. Installing the package does not update the widget application or API.
npm install @senlerio/widget@https://github.com/SenlerBot/senler-widget/archive/refs/tags/v3.0.0.tar.gz
With a compatible package available:
import { createSenlerWidget } from "@senlerio/widget";
const widget = await createSenlerWidget({
src: "LOADER_URL_FROM_CABINET",
channelId: "CHANNEL_ID",
lang: "ru",
});
widget.open();
const result = await widget.ask({ text: "Help me choose an offer." });
console.log(result.answers);
Take the loader URL and channel ID from the embed code. createSenlerWidget waits for chat readiness and authentication. To manage the wait explicitly, use createSenlerWidgetSession({ src, config }): session.ready returns the instance; session.destroy() releases it, including while loading.
loadSenlerWidget({ src, timeoutMs, nonce, signal }) only loads and validates the script. Call init or create yourself afterward and wait for readiness. Instances share one loader URL while keeping independent dialogs and requests.
The root package declares optional window.SenlerWidget. For a page managing its own script, add @senlerio/widget/global to compilerOptions.types; use import type for type-only imports. The global may be absent until the script loads. Importing is SSR-safe; initialization requires a browser.
React component
Use a separate entry point of the same package for React 18/19:
import { useMemo } from "react";
import { SenlerWidget } from "@senlerio/widget/react";
export function Chat({ channelId }: { channelId: string }) {
const config = useMemo(() => ({ channel_id: channelId }), [channelId]);
return <SenlerWidget
src="LOADER_URL_FROM_CABINET"
config={config}
settings={{ lang: "ru", themeMode: "dark", borderRadius: 16 }}
containerProps={{ style: { height: 600 } }}
/>;
}
The component creates an embedded container and releases its instance on unmount, including React StrictMode. For a popup or existing container, use SenlerWidgetProvider and useSenlerWidget, or the standalone useSenlerWidgetController. Multiple widgets can work independently on one page.
Keep config stable with useMemo: changing it recreates the session. config={null} defers initialization; changing retryKey retries after failure. The controller and hook return { api, status, error }, where status is idle, loading, ready, or failed.
Supply changing presentation settings through the component or provider's settings prop; this does not recreate the widget. The standalone hook uses api.configure(settings). Send messages and navigate from handlers through api.ask(), api.startNewDialog(), api.selectDialog(), and api.open(), not persistent props or every render.
For a collapsible panel, update surfaceVisible through settings or api.configure: it controls read receipts, not site layout. All methods, startup errors, context.
After a successful await api.resetSession(), the hook returns status: "idle" and api: null. Supply a new config for the next visitor; change retryKey to start again with the same configuration. If reset fails, first finish signing out on the same instance.
Custom interface in React
For headless mode, use SenlerWidgetProvider and pass your own interface as children:
import { useMemo, type ReactNode } from "react";
import { SenlerWidgetProvider } from "@senlerio/widget/react";
export function CustomChat({
channelId, children,
}: { channelId: string; children: ReactNode }) {
const config = useMemo(
() => ({ channel_id: channelId, headless: true }),
[channelId],
);
return <SenlerWidgetProvider src="LOADER_URL_FROM_CABINET" config={config}>
{children}
</SenlerWidgetProvider>;
}
Child components receive { api, status, error } through useSenlerWidget. After status: "ready", use history, subscriptions, and sending. Create the subscription in an effect depending on api and dialogId, returning its unsubscribe function from the effect. The provider itself releases the instance on unmount.
The <SenlerWidget> component is for the built-in embedded chat and does not accept headless: true. Both approaches expose the same programmatic methods; the website owns its custom DOM elements and interface state.