@maliffic/nestjs-twilio
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

nestjs-twilio

Injectable Twilio client for Nestjs.

build status codecov npm version miniziped size tree shaking MIT licensed

Implementing the TwilioModule from this package you gain access to Twilio client through dependency injection with minimal setup.

Instalation

$ npm install --save twilio nestjs-twilio
$ yarn add twilio nestjs-twilio

Getting Started

The simplest way to use nestjs-twilio is to use TwilioModule.forRoot

import { TwilioModule } from 'nestjs-twilio';

@Module({
  imports: [
    TwilioModule.forRoot({
      accountSid: process.env.TWILIO_ACCOUNT_SID,
      authToken: process.env.TWILIO_AUTH_TOKEN,
    }),
  ],
})
export class AppModule {}

Utilizing asynchronous providers

import { TwilioModule } from 'nestjs-twilio';

@Module({
  imports: [
    TwilioModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (cfg: ConfigService) => ({
        accountSid: cfg.get('TWILIO_ACCOUNT_SID'),
        authToken: cfg.get('TWILIO_AUTH_TOKEN'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

You can then inject the Twilio client into any of your injectables by using a custom decorator

import { InjectTwilio, TwilioClient } from 'nestjs-twilio';

@Injectable()
export class AppService {
  public constructor(@InjectTwilio() private readonly client: TwilioClient) {}

  async sendSMS() {
    try {
      return await this.client.messages.create({
        body: 'SMS Body, sent to the phone!',
        from: process.env.TWILIO_PHONE_NUMBER,
        to: TARGET_PHONE_NUMBER,
      });
    } catch (e) {
      return e;
    }
  }
}

Dependents (0)

Package Sidebar

Install

npm i @maliffic/nestjs-twilio

Weekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

19.2 kB

Total Files

35

Last publish

Collaborators

  • maliffic