@chat-adapter/twilio

中文文档
4.44.0发布于 2026/11/02MITTypeScriptES Module构建来源已验证近 30 天下载 42.8万

Twilio adapter for Chat SDK — SMS and MMS bots with Messaging webhooks and the Messages API

原始使用文档
文档目录11 节
  1. Installation
  2. Scaffold with the CLI
  3. Usage
  4. Configuration
  5. Typing indicators
  6. Media
  7. Templates
  8. Low-level helpers
  9. Voice
  10. AI Coding Agents
  11. License

Twilio adapter for Chat SDK

@chat-adapter/twilio

npm package: @chat-adapter/twilio

Agent Stack MIT License

Twilio adapter for Chat SDK. Build SMS and MMS bots with Twilio Messaging webhooks and the Messages API.

Documentation: chat-sdk.dev/adapters/official/twilio · Guides: vercel.com/kb/chat-sdk

Installation

pnpm add @chat-adapter/twilio

Scaffold with the CLI

To scaffold a new Twilio bot with this adapter preselected:

npx create-chat-sdk@latest my-bot --adapter twilio memory

Visit the adapters directory to see other available official and vendor-official adapters.

Usage

The adapter auto-detects TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER, and TWILIO_MESSAGING_SERVICE_SID from environment variables:

import { createTwilioAdapter } from "@chat-adapter/twilio";
import { Chat } from "chat";

const bot = new Chat({
  userName: "mybot",
  adapters: {
    twilio: createTwilioAdapter(),
  },
});

bot.onDirectMessage(async (thread, message) => {
  await thread.post(`You said: ${message.text}`);
});

Point your Twilio Messaging webhook to a route that calls bot.webhooks.twilio(request):

import { bot } from "@/lib/bot";

export async function POST(request: Request): Promise<Response> {
  return bot.webhooks.twilio(request);
}

Configuration

createTwilioAdapter({
  accountSid: process.env.TWILIO_ACCOUNT_SID,
  authToken: process.env.TWILIO_AUTH_TOKEN,
  phoneNumber: process.env.TWILIO_PHONE_NUMBER,
  messagingServiceSid: process.env.TWILIO_MESSAGING_SERVICE_SID,
  webhookUrl: "https://your-domain.com/api/webhooks/twilio",
});

Use phoneNumber for a single Twilio number, or messagingServiceSid when sending through a Twilio Messaging Service.

Typing indicators

WhatsApp and RCS conversations can show a typing indicator via thread.startTyping(). Twilio's v3 Indicators API uses one URL with channel-specific JSON:

  • WhatsApp: { channel: "WHATSAPP", messageId } (inbound Message SID)
  • RCS: { channel: "RCS", from: "rcs:<agent_id>", to: "rcs:+E164", event: "START" }

On WhatsApp, Twilio also marks the referenced message as read. Phone-number threads are SMS and never send typing. Messaging Service (MG…) threads send RCS typing only when rcsSenderId is set and the thread has an inbound message; rcs: threads always do. If Twilio rejects the request, the adapter logs a warning instead of throwing. Set messagingApiUrl (or apiUrl) to point typing requests at a proxy or emulator.

Media

Inbound MMS media is exposed as attachments. Twilio media URLs are private, so attachments include fetchData() for authenticated downloads.

Outbound MMS supports attachments with public url values. Chat SDK cannot upload binary files to Twilio because Twilio's Messages API requires media URLs that Twilio can fetch.

Templates

Cards create Content API templates on demand for RCS senders only. To send a template on any channel, such as an approved WhatsApp template, pass its ContentSid to sendTemplate:

const twilio = bot.getAdapter("twilio");

const threadId = twilio.encodeThreadId({
  sender: "whatsapp:+15550000001",
  recipient: "whatsapp:+15551234567",
});

await twilio.sendTemplate(threadId, {
  contentSid: "HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  contentVariables: { "1": "Tomorrow at 2pm" },
});

WhatsApp templates need a whatsapp: sender or a Messaging Service with a WhatsApp sender. Quick-reply taps on templates sent this way are not decoded as Chat SDK actions: taps that carry a message body arrive as regular messages, and taps without one reach onAction with the raw ButtonPayload as actionId.

Low-level helpers

Runtime-light api, format, voice, and webhook subpaths are available for apps that only need Twilio primitives. These subpaths do not import the full Chat SDK adapter or the twilio npm package.

Voice

Voice calls are exposed as low-level helpers, not routed through the SMS/MMS adapter. Use @chat-adapter/twilio/voice with @chat-adapter/twilio/webhook when your app owns the voice route and wants reusable TwiML or call-update helpers.

Custom voice routes should verify the Twilio signature and apply your own caller allow-list before returning TwiML.

AI Coding Agents

If you use an AI coding agent such as OpenAI Codex, Claude Code, or Cursor, install the Chat SDK skill so it knows the SDK APIs, adapter patterns, and project conventions before writing code.

npx skills add vercel/chat

The skill references bundled documentation in node_modules/chat/docs, plus adapter guides and starter templates in the published package.

You can also install the Vercel Plugin for a broader agent toolkit — it includes the Chat SDK skill alongside specialist agents, agent slash commands, and more:

npx plugins add vercel/vercel-plugin

The plugin is optional; the skill alone is enough to build with Chat SDK.

For agent-readable documentation, see chat-sdk.dev/llms.txt (page index) or chat-sdk.dev/llms-full.txt (full text).

License

MIT

版本与发布记录

@chat-adapter/twilio 共发布 18 个版本,首次发布于 2026/05/28;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.44.0latest2026/11/026 天前
4.43.02026/10/1921 天前
4.42.02026/10/1425 天前
4.41.12026/09/291 个月前
4.41.02026/09/181 个月前
4.40.02026/09/052 个月前

关于 @chat-adapter/twilio 的常见问题

@chat-adapter/twilio 是什么?

Twilio adapter for Chat SDK — SMS and MMS bots with Messaging webhooks and the Messages API

如何安装 @chat-adapter/twilio?

在项目目录运行 npm install @chat-adapter/twilio,或使用 pnpm add / yarn add / bun add。

@chat-adapter/twilio 支持 TypeScript 吗?

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

@chat-adapter/twilio 支持 ESM 还是 CommonJS?

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

@chat-adapter/twilio 需要什么版本的 Node.js?

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

@chat-adapter/twilio 可以商用吗?

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

@chat-adapter/twilio 的下载量有多少?

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

@chat-adapter/twilio 还在维护吗?

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