Twilio Provider
Package: @nauth-toolkit/sms-twilio
Type: SMS Provider
Sends SMS via the Twilio Programmable Messaging API. Supports direct phone numbers and Messaging Services.
- npm
- Yarn
- pnpm
- Bun
npm install @nauth-toolkit/sms-twilio
yarn add @nauth-toolkit/sms-twilio
pnpm add @nauth-toolkit/sms-twilio
bun add @nauth-toolkit/sms-twilio
Configuration
- Phone Number
- Messaging Service
import { TwilioSMSProvider } from '@nauth-toolkit/sms-twilio';
const smsProvider = new TwilioSMSProvider({
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
fromNumber: '+15551234567',
});
import { TwilioSMSProvider } from '@nauth-toolkit/sms-twilio';
const smsProvider = new TwilioSMSProvider({
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
messagingServiceSid: process.env.TWILIO_MESSAGING_SERVICE_SID!,
});
Use a Messaging Service for automatic sender selection, sticky sender, opt-out management, and advanced delivery features.
TwilioSMSConfig
| Property | Type | Required | Description |
|---|---|---|---|
accountSid | string | Yes | Twilio Account SID (starts with AC) |
authToken | string | Yes | Twilio Auth Token |
fromNumber | string | No* | Sender phone number in E.164 format. *Required if messagingServiceSid is not set. |
messagingServiceSid | string | No* | Twilio Messaging Service SID (starts with MG). *Required if fromNumber is not set. |
Provide either fromNumber or messagingServiceSid, not both.
Usage
- NestJS
- Express
- Fastify
src/auth/auth.module.ts
import { TwilioSMSProvider } from '@nauth-toolkit/sms-twilio';
AuthModule.forRoot({
smsProvider: new TwilioSMSProvider({
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
fromNumber: process.env.TWILIO_FROM_NUMBER!,
}),
});
src/config.ts
import { TwilioSMSProvider } from '@nauth-toolkit/sms-twilio';
const nauth = await NAuth.create({
config: {
smsProvider: new TwilioSMSProvider({
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
fromNumber: process.env.TWILIO_FROM_NUMBER!,
}),
},
// ...
});
src/config.ts
import { TwilioSMSProvider } from '@nauth-toolkit/sms-twilio';
const nauth = await NAuth.create({
config: {
smsProvider: new TwilioSMSProvider({
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
fromNumber: process.env.TWILIO_FROM_NUMBER!,
}),
},
adapter: new FastifyAdapter(),
// ...
});
Environment Variables
.env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_FROM_NUMBER=+15551234567
# Or use a Messaging Service instead of fromNumber:
# TWILIO_MESSAGING_SERVICE_SID=MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Get your Account SID and Auth Token from the Twilio Console under "Account Info".
Templates
Customize SMS message content with templates:
AuthModule.forRoot({
smsProvider: new TwilioSMSProvider({
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
fromNumber: process.env.TWILIO_FROM_NUMBER!,
}),
sms: {
templates: {
globalVariables: {
appName: 'My App',
supportPhone: '+1-800-123-4567',
},
customTemplates: {
verification: {
content: '{{appName}}: Your code is {{code}}. Expires in {{expiryMinutes}} min.',
},
},
},
},
});
See SMS Templates for complete documentation.
Related
- SMS Overview
- SMS Templates Configuration
- Console SMS - Development provider
- AWS SNS - AWS provider