Transports

Twilio SMS

Send SMS through the Twilio Messages API with createSmsSender.

Use Twilio when your SMS traffic should go through the Programmable Messaging Messages API. Wire TwilioSmsTransport into createSmsSender — Verify, Voice, and other Twilio products stay off this transport.

The one rule

Provide From or messagingServiceSid (or both), keep E.164 + prefixes, and send only through createSmsSender.

Quick start

Configure

import { createSmsSender } from "sently/sms";
import { TwilioSmsTransport } from "sently/transports/twilio-sms";

const sms = createSmsSender({
  transport: new TwilioSmsTransport({
    accountSid: process.env.TWILIO_ACCOUNT_SID!,
    apiKey: process.env.TWILIO_API_KEY!,
    authToken: process.env.TWILIO_API_KEY_SECRET!,
    from: "+15557654321",
  }),
});

Production auth: API Key SID as Basic username and API Key Secret as password, with Account SID still in the URL path. For local testing you may omit apiKey and use Account SID + Auth Token.

Send

const result = await sms.send({
  to: "+15551234567",
  body: "Your code is 123456",
});

See the result

console.log(result.messageId); // SM…
console.log(result.status); // e.g. queued

Configuration

OptionTypeDefaultMeaning
accountSidstringrequiredAccount SID in the Messages URL path.
authTokenstringrequiredAuth Token, or API Key Secret when apiKey is set.
apiKeystringAPI Key SID used as Basic-auth username (recommended in production).
fromstringDefault From number / sender id.
messagingServiceSidstringMessaging Service SID (MG…) as sender alternative.
statusCallbackstringDelivery status callback URL.

Either from / options.from or messagingServiceSid is required for each send.

Messaging Service

When messagingServiceSid is set and From is omitted, Twilio picks a sender from the service pool. You may set both to pin a specific From inside that pool.

const sms = createSmsSender({
  transport: new TwilioSmsTransport({
    accountSid: process.env.TWILIO_ACCOUNT_SID!,
    authToken: process.env.TWILIO_AUTH_TOKEN!,
    messagingServiceSid: "MG…",
  }),
});

Verify and errors

verify() fetches GET /2010-04-01/Accounts/{AccountSid}.json with the same Basic credentials.

Failed Messages responses throw TwilioSmsError with Twilio’s message / code payload when present.

Troubleshooting

Learn more

Next

On this page