Messaging Wire
The Messaging wire (@fluxkitdev/messaging) provides a unified interface for sending emails, SMS, and WhatsApp messages through pluggable providers.
Installation
npm install @fluxkitdev/core @fluxkitdev/messaging
Quick Start
import { FluxKit } from "@fluxkitdev/core";
import { messaging } from "@fluxkitdev/messaging";
const app = new FluxKit({
wires: [
messaging({
email: {
provider: "smtp",
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
from: "noreply@myapp.com",
},
sms: {
provider: "twilio",
accountSid: process.env.TWILIO_SID,
authToken: process.env.TWILIO_TOKEN,
from: process.env.TWILIO_PHONE,
},
whatsapp: {
provider: "twilio",
accountSid: process.env.TWILIO_SID,
authToken: process.env.TWILIO_TOKEN,
from: process.env.TWILIO_WHATSAPP,
},
}),
],
database: { uri: process.env.MONGODB_URI },
});
await app.start({ port: 3000 });
Features
| Feature | Description |
|---|---|
| Send emails via SMTP, SendGrid, or SES | |
| SMS | Send text messages via Twilio or Vonage |
| Send WhatsApp messages via Twilio or the WhatsApp Business API |
Programmatic API
// Send an email
await app.messaging.email.send({
to: "user@example.com",
subject: "Welcome!",
body: "<h1>Welcome to My App</h1>",
});
// Send an SMS
await app.messaging.sms.send({
to: "+1234567890",
body: "Your verification code is 482901",
});
// Send a WhatsApp message
await app.messaging.whatsapp.send({
to: "+1234567890",
body: "Your order has been shipped!",
});
REST API
The Messaging wire also exposes REST endpoints:
POST /messaging/email-- Send an emailPOST /messaging/sms-- Send an SMSPOST /messaging/whatsapp-- Send a WhatsApp messageGET /messaging/logs-- Get message delivery logsGET /messaging/health-- Health check
Message Logging
All sent messages are logged in the messaging_logs collection in MongoDB:
const logs = await app.db.collection("messaging_logs")
.find({ channel: "email", to: "user@example.com" })
.sort({ createdAt: -1 })
.toArray();
Environment Variables
| Variable | Default | Description |
|---|---|---|
SMTP_HOST | -- | SMTP server hostname |
SMTP_PORT | 587 | SMTP server port |
SMTP_USER | -- | SMTP username |
SMTP_PASS | -- | SMTP password |
SMTP_FROM | -- | Default sender email |
TWILIO_SID | -- | Twilio Account SID |
TWILIO_TOKEN | -- | Twilio Auth Token |
TWILIO_PHONE | -- | Twilio phone number for SMS |
TWILIO_WHATSAPP | -- | Twilio WhatsApp number |
SENDGRID_API_KEY | -- | SendGrid API key |