Skip to main content

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 install @nauth-toolkit/sms-twilio

Configuration

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',
});

TwilioSMSConfig

PropertyTypeRequiredDescription
accountSidstringYesTwilio Account SID (starts with AC)
authTokenstringYesTwilio Auth Token
fromNumberstringNo*Sender phone number in E.164 format. *Required if messagingServiceSid is not set.
messagingServiceSidstringNo*Twilio Messaging Service SID (starts with MG). *Required if fromNumber is not set.

Provide either fromNumber or messagingServiceSid, not both.

Usage

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!,
}),
});

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.