@chatlychat/sdk-js

中文文档
0.26.2发布于 2026/11/06MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 4606

Browser SDK for Chatly — identify visitors, fire CDP events, control the widget, run a conversation headlessly, and type window.Chatly.

原始使用文档
文档目录8 节
  1. Quick start
  2. Chatly.init(options)
  3. Methods
  4. Typing window.Chatly (the loader snippet's global)
  5. A second, inline view ( mount )
  6. Signed page context
  7. This README used to document an API that did not exist
  8. About this repository

@chatlychat/sdk-js

Browser SDK for the Chatly platform: control the in-page widget, identify visitors, fire CDP events, and — headlessly — list conversations, send messages and subscribe to replies without rendering our UI at all.

Two halves, and only one is on the widget global. The widget-control methods (open, close, identify, track) are also available with no package at all — load the widget snippet and call them on window.Chatly. The headless half (listConversations, listMessages, send, uploadFile, subscribe, rate, emailTranscript) has no global and needs this package or a direct call to the documented REST endpoints.

npm install @chatlychat/sdk-js
# or
pnpm add @chatlychat/sdk-js

Quick start

import { Chatly } from '@chatlychat/sdk-js';

// The constructor is PRIVATE. `init` is the only way in.
const chatly = Chatly.init({ publicId: 'ch_web_0192…' });

await chatly.identify({ externalId: 'user_42', name: 'Ada', userHash: '…' });
const { conversationId } = await chatly.startConversation('Hello');
await chatly.send(conversationId, 'and one more thing');

Chatly.init(options)

OptionTypeDefaultNotes
publicIdstring—The web channel's public id, from Dashboard → Channels.
apiUrlstring?https://api.chatlychat.comAnother Chatly environment, or a test server in your own suite.
realtimeUrlstring?reported by the serverSocket.IO host for subscribe(). Only for a host the server cannot know.

Methods

Widget control — these mirror window.Chatly and need the widget on the page:

  • open() / close()

Visitor identity and analytics:

  • identify(input) → { contactId }. Pass userHash to make the identity verified; without it the visitor is unverified and tools that require a proven identity will refuse.
  • track(name, properties?)

Headless conversation surface — no widget required:

  • startConversation(body, attachments?) → { conversationId }
  • send(conversationId, body, attachments?)
  • listConversations(limit = 20)
  • unreadCount() → number. Agent and bot messages this visitor has not read, across their threads — the number a custom launcher's badge shows. It costs a request, so it is a call rather than a property.
  • listMessages(conversationId, limit = 50)
  • subscribe(conversationId, handlers, options?) → Subscription. Uses the push socket when it can and falls back to polling, so it keeps working where a WebSocket cannot be opened.
  • uploadFile(file) → VisitorAttachment, to pass to send or startConversation
  • rate(conversationId, score, comment?)
  • emailTranscript(conversationId, email)

Typing window.Chatly (the loader snippet's global)

If you embed the widget with the two-line loader snippet and then drive it from your own TypeScript, this package is also the type declaration for the global. Install it as a dev dependency — you need no runtime code for this — and import any type from it. Importing augments Window for the whole program, so there is no /// <reference> line and no tsconfig types entry to add:

import type { ChatlyGlobal, InitOptions } from '@chatlychat/sdk-js';

const options: InitOptions = { publicId: 'acme-web', theme: { primaryColor: '#4f46e5' } };
window.Chatly?.init(options);

Exported from 0.2.0: ChatlyApi, ChatlyGlobal, InitOptions, WidgetTheme, WidgetBehavior, WidgetCopy, WidgetQuickLink, WidgetQuip, WidgetIdentity, SessionSnapshot, ChatlyEventName, ChatlyEventHandler, OpenRequest, StartConversationRequest, SetContextInput, ReportActionInput. And from 0.4.0: MountOptions. These are the widget's own declarations, re-exported rather than restated, so there is no copy to drift.

A second, inline view (mount)

ChatlyApi declares mount(opts: MountOptions) and unmount(el) — a SECOND widget view, inline in your page, beside the launcher. It is on the widget's global and not on the Chatly class above: a view is rendered by the widget bundle, so it needs the bundle on the page.

import type { MountOptions } from '@chatlychat/sdk-js';

const view: MountOptions = { el: '#shop-assistant', publicId: 'ch_web_shop…' };
window.Chatly?.('mount', view);
// later, when your route unmounts
window.Chatly?.('unmount', '#shop-assistant');

MountOptions is InitOptions with mountTo replaced by el and publicId made optional. What a view shares with the primary widget, what it does not, and why there is no botSlug: see the web channel guide.

Window.Chatly is typed as the full API, because window.Chatly.identify(...) being a type error is useless to everybody. What is really on the page between the snippet running and the bundle arriving is only the queueing function the snippet installed. From code that can run that early, use the callable form, which the snippet queues and the bundle replays — window.Chatly?.('identify', { externalId }) — or wait for Chatly.on('ready', …).

Signed page context

setContext is not on this SDK — it is on the widget's own global. See the web channel guide for the call and identity verification for how to mint the signed contextToken.

This README used to document an API that did not exist

For the record, because it may have been copied: it described new Chatly({ channelPublicId, baseUrl, widgetSelector, locale }), plus toggle(), on(), destroy() and send(body). None of those exist. The constructor is private, the option names are publicId / apiUrl / realtimeUrl, and send takes a conversation id first. Corrected 2026-10-12 against src/index.ts.


About this repository

This is a mirror. @chatlychat/sdk-js is built from a private monorepo where the rest of the platform, its migrations and its contract checks live, so a pull request here cannot be merged directly — but a patch is still the fastest way to get something fixed and we will port it with attribution.

What you can do here: read the source, run the tests, open an issue, or open a PR and we will treat the diff as the proposal.

Not mirrored: src/headless-message-parity.spec.ts, src/icon-names-parity.spec.ts — specs that assert a type restated here still matches the monorepo's original, which is only a meaningful claim inside it. They keep running there.

Issues: https://github.com/chatlychat/chatly-sdk-js/issues · Docs: https://chatlychat.com/docs

版本与发布记录

@chatlychat/sdk-js 共发布 31 个版本,首次发布于 2026/10/13;过去 12 个月发布了 31 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.26.2latest2026/11/063 天前
0.26.12026/11/045 天前
0.26.02026/11/045 天前
0.25.02026/10/2812 天前
0.24.02026/10/2812 天前
0.23.02026/10/2812 天前

关于 @chatlychat/sdk-js 的常见问题

@chatlychat/sdk-js 是什么?

Browser SDK for Chatly — identify visitors, fire CDP events, control the widget, run a conversation headlessly, and type window.Chatly.

如何安装 @chatlychat/sdk-js?

在项目目录运行 npm install @chatlychat/sdk-js,或使用 pnpm add / yarn add / bun add。

@chatlychat/sdk-js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chatlychat/sdk-js 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chatlychat/sdk-js 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chatlychat/sdk-js 可以商用吗?

@chatlychat/sdk-js 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chatlychat/sdk-js 的下载量有多少?

根据 npm 官方下载统计,@chatlychat/sdk-js 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,606 次,日均约 154 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chatlychat/sdk-js 还在维护吗?

最新版本 0.26.2 发布于 2026/11/06(3 天前);至今共发布 31 个版本,其中过去 12 个月发布了 31 个;首次发布于 2026/10/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。