Send WhatsApp messages through Twilio or the WhatsApp Business API.
Providers
| Provider | Configuration Key | Description |
|---|---|---|
twilio | Account SID, Auth Token | Twilio WhatsApp API |
whatsapp-business | API Token | Meta WhatsApp Business API |
Configuration
Twilio
messaging({
whatsapp: {
provider: "twilio",
accountSid: process.env.TWILIO_SID,
authToken: process.env.TWILIO_TOKEN,
from: "whatsapp:+14155238886", // Twilio sandbox or your number
},
});
WhatsApp Business API
messaging({
whatsapp: {
provider: "whatsapp-business",
apiToken: process.env.WHATSAPP_API_TOKEN,
phoneNumberId: process.env.WHATSAPP_PHONE_ID,
},
});
Sending Messages
Text Message
await app.messaging.whatsapp.send({
to: "+1234567890",
body: "Your order has been shipped! Track it here: https://track.example.com/abc123",
});
Template Message (WhatsApp Business API)
WhatsApp Business API requires pre-approved templates for initiating conversations:
await app.messaging.whatsapp.sendTemplate({
to: "+1234567890",
template: "order_confirmation",
language: "en",
components: [
{
type: "body",
parameters: [
{ type: "text", text: "Alice" },
{ type: "text", text: "#1234" },
],
},
],
});
Media Message
await app.messaging.whatsapp.send({
to: "+1234567890",
body: "Here is your receipt:",
media: {
url: "https://example.com/receipt.pdf",
type: "document",
filename: "receipt.pdf",
},
});
API Reference
whatsapp.send(options): Promise<MessageResult>
| Option | Type | Required | Description |
|---|---|---|---|
to | string | Yes | Recipient phone (E.164 format) |
body | string | Yes | Message text |
from | string | No | Override sender |
media | MediaObject | No | Attach media |
whatsapp.sendTemplate(options): Promise<MessageResult>
| Option | Type | Required | Description |
|---|---|---|---|
to | string | Yes | Recipient phone (E.164 format) |
template | string | Yes | Template name |
language | string | Yes | Template language code |
components | Component[] | No | Template variables |
REST API
curl -X POST http://localhost:3000/messaging/whatsapp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGci..." \
-d '{
"to": "+1234567890",
"body": "Your order has been shipped!"
}'
Response (200):
{
"messageId": "wamid.abc123",
"status": "sent",
"channel": "whatsapp",
"to": "+1234567890"
}
Webhook for Incoming Messages
FluxKit can receive incoming WhatsApp messages via webhooks:
app.messaging.whatsapp.onMessage(async (message) => {
console.log(`From: ${message.from}`);
console.log(`Body: ${message.body}`);
// Auto-reply
await app.messaging.whatsapp.send({
to: message.from,
body: "Thanks for your message! We'll get back to you soon.",
});
});
The webhook endpoint is available at POST /messaging/whatsapp/webhook.