Register the module
TwilioModule is built with Nest’s ConfigurableModuleBuilder, so it exposes
the standard forRoot() / forRootAsync() pair. Both accept the same
options shape.
Synchronous registration
Section titled “Synchronous registration”import { Module } from '@nestjs/common';import { TwilioModule } from 'nestjs-twilio';
@Module({ imports: [ TwilioModule.forRoot({ accountSid: 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', authToken: 'auth_token_here', webhookAuthToken: 'webhook_token_override', // optional webhookUrl: 'https://example.com/webhooks', // optional, for proxy scenarios region: 'ie1', // optional, flat, not nested under `options` edge: 'sydney', // optional }), ],})export class AppModule {}Credentials are validated when the module initializes. TwilioModule throws
a BadRequestException (never including the credential value itself) if:
accountSidis missing or not a stringaccountSiddoesn’t start with"AC"- neither
authToken, norapiKeytogether withapiSecret, is provided apiKeyis given withoutapiSecretapiKeydoesn’t start with"SK"
Authenticating with an API key
Section titled “Authenticating with an API key”Twilio treats API keys as the preferred way to authenticate REST requests, and
they are also what Access Tokens
are signed with. Supply apiKey and apiSecret instead of authToken:
TwilioModule.forRoot({ accountSid: process.env.TWILIO_ACCOUNT_SID, apiKey: process.env.TWILIO_API_KEY, // SK… apiSecret: process.env.TWILIO_API_SECRET,});apiSecret is mandatory whenever apiKey is set. The secret is what signs
the request, so a key without it cannot authenticate anything.
If both an authToken and an API key are supplied, the auth token wins,
matching the precedence used by the Twilio CLI and the SDK’s own
environment-variable handling.
Asynchronous registration
Section titled “Asynchronous registration”Use forRootAsync() to resolve options from another provider, such as
ConfigService:
import { Module } from '@nestjs/common';import { ConfigModule, ConfigService } from '@nestjs/config';import { TwilioModule } from 'nestjs-twilio';
@Module({ imports: [ TwilioModule.forRootAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ accountSid: configService.get('TWILIO_ACCOUNT_SID'), authToken: configService.get('TWILIO_AUTH_TOKEN'), region: configService.get('TWILIO_REGION'), }), inject: [ConfigService], }), ],})export class AppModule {}The factory’s return value is validated the same way as the synchronous
forRoot() options, and it is flattened the same way: no options key.
Global registration
Section titled “Global registration”forRoot() always registers TwilioModule globally. Import it once in your
root module and TwilioService, the default client and every named client are
available application-wide. There is no flag to set.
This matches TypeOrmCoreModule, Mongoose’s core module and
BullModule.forRoot(), all of which register globally. It is also what allows
named clients to inherit the options you pass here:
a named client is registered as its own module, and a non-global root would be
invisible to it.
Further reading
Section titled “Further reading”For the full list of accepted options, including every Twilio SDK client option and every type and utility this package exports, see Configuration options.