Send SMS notifications easily using Twilio, Vonage, or custom drivers. Integrated seamlessly with CanxJS's notification system.
Configure your SMS drivers in the app config.
1// src/config/app.ts2export const config = {3// ...4sms: {5default: 'twilio',6drivers: {7twilio: {8sid: process.env.TWILIO_SID,9token: process.env.TWILIO_TOKEN,10from: process.env.TWILIO_FROM,11},12vonage: {13key: process.env.VONAGE_KEY,14secret: process.env.VONAGE_SECRET,15from: process.env.VONAGE_FROM,16}17}18}19};
The recommended way to send SMS is through Notification classes. Simply add sms to the via array and implement toSms.
1import { Notification, type Notifiable } from 'canxjs';23export class OrderShipped extends Notification {4constructor(public orderId: string) {}56via(notifiable: Notifiable) {7return ['sms'];8}910toSms(notifiable: Notifiable) {11return {12content: `Your order ${this.orderId} has been shipped!`,13// Optional: override recipient14// to: '+1234567890'15};16}17}
You can also use the sms facade for quick, direct messaging.
1import { sms } from 'canxjs';23// Send using default driver4await sms().send({5to: '+15550001234',6content: 'Hello form CanxJS!'7});89// Switch driver10await sms().driver('vonage').send({11to: '+15550001234',12content: 'Hello from Vonage!'13});