Skip to main content

How to Send WhatsApp Notifications from Node.js How to Send WhatsApp Notifications from Node.js

How to Send WhatsApp Notifications from Node.js

Send alerts, reminders, and updates to WhatsApp from any Node.js app. whatsmeow-node stays connected in the background — you just call sendMessage() whenever you need to notify someone.

Prerequisites

  • A paired whatsmeow-node session (How to Pair)
  • The recipient's phone number (as a JID: 5512345678@s.whatsapp.net)

Step 1: Set Up a Persistent Connection

import { createClient } from "@whatsmeow-node/whatsmeow-node";

const client = createClient({ store: "session.db" });

async function start() {
const { jid } = await client.init();
if (!jid) {
console.error("Not paired! See: How to Pair WhatsApp");
process.exit(1);
}
await client.connect();
await client.sendPresence("available");
console.log("Notification service is ready");
}

start().catch(console.error);

The client stays connected and ready to send messages at any time.

Step 2: Send a Notification

async function notify(phone: string, message: string) {
const jid = `${phone}@s.whatsapp.net`;
await client.sendMessage(jid, { conversation: message });
}

// Example: order update
await notify("5512345678", "Your order #1234 has shipped! Track it at https://example.com/track/1234");

Step 3: Trigger from an HTTP Endpoint

Wrap the notification in an Express route so other services can trigger it:

import express from "express";

const app = express();
app.use(express.json());

app.post("/notify", async (req, res) => {
const { phone, message } = req.body;

if (!phone || !message) {
return res.status(400).json({ error: "phone and message are required" });
}

try {
const jid = `${phone}@s.whatsapp.net`;
const resp = await client.sendMessage(jid, { conversation: message });
res.json({ sent: true, id: resp.id });
} catch (err) {
res.status(500).json({ error: "Failed to send" });
}
});

app.listen(3000, () => console.log("Notification API on :3000"));

Now any service can send a notification:

curl -X POST http://localhost:3000/notify \
-H "Content-Type: application/json" \
-d '{"phone": "5512345678", "message": "Your appointment is in 1 hour"}'

Step 4: Send to Multiple Recipients

async function broadcast(phones: string[], message: string) {
for (const phone of phones) {
const jid = `${phone}@s.whatsapp.net`;
await client.sendMessage(jid, { conversation: message });

// Wait between sends to avoid rate limiting
await new Promise((r) => setTimeout(r, 2000));
}
}

await broadcast(
["5512345678", "5598765432", "5511223344"],
"Reminder: team meeting at 3 PM today",
);
Rate limiting

WhatsApp rate-limits sending. Space messages 1-3 seconds apart for bulk sends. Sending too fast can get your account temporarily restricted. See Rate Limiting.

Notification Patterns

Appointment Reminder

await notify(phone, `Reminder: your appointment with Dr. Smith is tomorrow at 10:00 AM.

Reply CONFIRM to confirm or CANCEL to cancel.`);

Order Update

await notify(phone, `Order #${orderId} update: ${status}

${status === "shipped" ? `Track: ${trackingUrl}` : ""}`);

System Alert

await notify(adminPhone, `⚠ Server alert: CPU usage at ${cpuPercent}% on ${hostname}`);

Group Notification

// Send to a group instead of an individual
const groupJid = "120363XXXXX@g.us";
await client.sendMessage(groupJid, {
conversation: "Deploy complete: v2.1.0 is live",
});

Complete Example

A notification service with an HTTP API:

import { createClient } from "@whatsmeow-node/whatsmeow-node";
import express from "express";

const client = createClient({ store: "session.db" });
const app = express();
app.use(express.json());

app.post("/notify", async (req, res) => {
const { phone, message } = req.body;
if (!phone || !message) {
return res.status(400).json({ error: "phone and message are required" });
}

try {
const jid = `${phone}@s.whatsapp.net`;
const resp = await client.sendMessage(jid, { conversation: message });
res.json({ sent: true, id: resp.id });
} catch (err) {
console.error("Send failed:", err);
res.status(500).json({ error: "Failed to send" });
}
});

app.post("/broadcast", async (req, res) => {
const { phones, message } = req.body;
if (!phones?.length || !message) {
return res.status(400).json({ error: "phones[] and message are required" });
}

// Send in background
(async () => {
for (const phone of phones) {
try {
const jid = `${phone}@s.whatsapp.net`;
await client.sendMessage(jid, { conversation: message });
} catch (err) {
console.error(`Failed to send to ${phone}:`, err);
}
await new Promise((r) => setTimeout(r, 2000));
}
console.log(`Broadcast complete: ${phones.length} recipients`);
})();

res.json({ queued: true, count: phones.length });
});

async function main() {
const { jid } = await client.init();
if (!jid) {
console.error("Not paired!");
process.exit(1);
}
await client.connect();
await client.sendPresence("available");

app.listen(3000, () => console.log("Notification API on :3000"));

process.on("SIGINT", async () => {
await client.sendPresence("unavailable");
await client.disconnect();
client.close();
process.exit(0);
});
}

main().catch(console.error);

Common Pitfalls

Don't spam

Sending bulk unsolicited messages violates WhatsApp's Terms of Service and will get your account banned. Only send notifications to users who have opted in.

Rate limiting

Space out sends with a 1-3 second delay. See Rate Limiting for details.

Keep the connection alive

The client must stay connected to send messages. If the process exits, you'll need to reconnect. For production, use a process manager like PM2 or run as a systemd service.