Skip to main content

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

FeatureDescription
EmailSend emails via SMTP, SendGrid, or SES
SMSSend text messages via Twilio or Vonage
WhatsAppSend 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 email
  • POST /messaging/sms -- Send an SMS
  • POST /messaging/whatsapp -- Send a WhatsApp message
  • GET /messaging/logs -- Get message delivery logs
  • GET /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

VariableDefaultDescription
SMTP_HOST--SMTP server hostname
SMTP_PORT587SMTP 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