@sadikkhan02/adapter-twilio

中文文档
1.0.0发布于 2026/11/02MITTypeScriptESM + CJS零依赖

Twilio SMS provider adapter for NotifyKit

原始使用文档
文档目录7 节
  1. Installation
  2. Configuration
  3. Environment Variables
  4. Usage
  5. Error Classification & Resilience
  6. Regulatory Notice & Carrier Costs
  7. License

@sadikkhan02/adapter-twilio

Global SMS adapter for NotifyKit using Twilio Messaging API.

This package provides an enterprise SMS transport for NotifyKit via Twilio. It implements the SmsProvider interface, supports alphanumeric senders and Messaging Service SIDs, normalizes E.164 phone numbers, and adheres to @sadikkhan02/test-utils contract tests.


Installation

npm install @sadikkhan02/notifykit @sadikkhan02/adapter-twilio twilio

Note: twilio is declared as an external peer dependency to allow consumers control over SDK version pinning and connection reuse.


Configuration

Environment Variables

VariableRequiredDescription
TWILIO_ACCOUNT_SIDYesTwilio Account SID (AC...)
TWILIO_AUTH_TOKENYesTwilio Auth Token
TWILIO_FROMOptional*Default sender phone number in E.164 format (e.g. +15555550100)
TWILIO_MESSAGING_SERVICE_SIDOptional*Twilio Messaging Service SID (MG...) for automated sender pooling

* Either TWILIO_FROM or TWILIO_MESSAGING_SERVICE_SID must be configured (or provided in options).


Usage

import { createNotifier } from '@sadikkhan02/notifykit';
import { TwilioSmsProvider } from '@sadikkhan02/adapter-twilio';

const smsProvider = new TwilioSmsProvider({
  accountSid: process.env.TWILIO_ACCOUNT_SID!,
  authToken: process.env.TWILIO_AUTH_TOKEN!,
  from: process.env.TWILIO_FROM, // e.g. +15555550100
  // messagingServiceSid: process.env.TWILIO_MESSAGING_SERVICE_SID,
});

const notifier = createNotifier({
  providers: {
    sms: {
      type: 'twilio',
      adapter: smsProvider,
      from: process.env.TWILIO_FROM,
    },
  },
});

// Register SMS template
notifier.registerTemplate(
  'otp-verification',
  'Your verification code is {{otp}}. Valid for 5 minutes. Do not share this code.',
);

// Enqueue SMS (returns immediately with jobId)
const { jobId } = await notifier.notify({
  to: { phone: '+15555550199' }, // E.164 required
  channels: ['sms'],
  template: 'otp-verification',
  data: { otp: '482910' },
});

// Start processing SMS queue
notifier.startWorker({ channels: ['sms'], concurrency: 5 });

Error Classification & Resilience

The Twilio adapter maps structured Twilio error codes directly into standardized ProviderResult objects:

Twilio Error CodeReasonretryableBullMQ Action
20429Rate limit exceeded / concurrency throttlingtrueRetried with exponential backoff
30001Queue overflowtrueRetried with exponential backoff
30008Unknown carrier failure / transient networktrueRetried with exponential backoff
30010Carrier unreachable / temporarily unavailabletrueRetried with exponential backoff
20003Authentication failure (invalid Account SID / token)falseRouted directly to DLQ (no retry)
21211Invalid phone number formatfalseRouted directly to DLQ (no retry)
21614Target number is a landline / cannot receive SMSfalseRouted directly to DLQ (no retry)
21408Permission denied / region not enabledfalseRouted directly to DLQ (no retry)

Regulatory Notice & Carrier Costs

[!WARNING] Sending SMS via Twilio incurs per-segment carrier fees and pass-through charges. In the United States, traffic sent via 10-digit long codes (10DLC) requires an approved A2P 10DLC Brand and Campaign registration. Unregistered traffic is subject to severe carrier filtering, throttling, or non-delivery fees.


License

MIT

版本与发布记录

@sadikkhan02/adapter-twilio 共发布 2 个版本,首次发布于 2026/11/02;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.0latest2026/11/021 天前
0.0.0-stage2026/11/021 天前

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

@sadikkhan02/adapter-twilio 是什么?

Twilio短信提供商适配器为NotifyKit

如何安装 @sadikkhan02/adapter-twilio?

在项目目录运行 npm install @sadikkhan02/adapter-twilio,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:twilio(>=4.0.0)、@sadikkhan02/notifykit(^1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@sadikkhan02/adapter-twilio 支持 TypeScript 吗?

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

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

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

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

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

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

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

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

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