How to Send WhatsApp Messages with Node.js
Any statistics or percentages in this article are general industry figures or illustrations, not QuackAPI measurements or promises. Results depend on your audience, consent and sending patterns. QuackAPI connects to WhatsApp as a linked device (an unofficial method), so read our risk and acceptable use policy before sending.
Prerequisites & Installation
You need Node.js 18 or higher installed on your system. We will primarily use axios for HTTP requests, but also demonstrate the built-in fetch API available in modern Node.js versions.
Install axios in your project using npm:
npm install axiosSetting Up
Configure your API key and base URL as environment variables. Create a .env file or export them in your shell. The API key authenticates every request to the QuackAPI API.
Never hardcode your API key in source files. Use environment variables or a secrets manager in production.
const axios = require("axios");
const API_KEY = process.env.QUACKAPI_API_KEY;
const BASE_URL = "https://quackapi.com/v1/messages/send";
const headers = {
"Content-Type": "application/json",
"x-api-key": API_KEY
};Sending Text Messages with Axios
Axios makes it simple to send JSON POST requests. The following example sends a text message to a WhatsApp number using async/await syntax for clean, readable code.
async function sendTextMessage(to, message) {
try {
const response = await axios.post(BASE_URL, {
deviceId: 1,
to: to,
content: message,
type: "text"
}, { headers });
console.log("Message sent:", response.data);
return response.data;
} catch (error) {
console.error("Error:", error.response?.data || error.message);
throw error;
}
}
// Usage
sendTextMessage("923001234567", "Hello from Node.js!");Using Native Fetch
Node.js 18+ includes a built-in fetch API, so you can send messages without any third-party dependencies. This is great for lightweight scripts and serverless functions.
async function sendWithFetch(to, message) {
const response = await fetch("https://quackapi.com/v1/messages/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.QUACKAPI_API_KEY
},
body: JSON.stringify({
deviceId: 1,
to: to,
content: message,
type: "text"
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "Request failed");
}
return await response.json();
}
sendWithFetch("923001234567", "Hello via fetch!");Sending Media Messages
Send images, documents, and other media by changing the type and content fields. Images support optional captions, and documents require a filename parameter.
// Send an image with caption
await axios.post(BASE_URL, {
deviceId: 1,
to: "923001234567",
content: "https://example.com/photo.jpg",
type: "image",
caption: "Check out this photo!"
}, { headers });
// Send a PDF document
await axios.post(BASE_URL, {
deviceId: 1,
to: "923001234567",
content: "https://example.com/report.pdf",
type: "pdf",
filename: "report.pdf"
}, { headers });Building a Reusable Module
Wrap all messaging functionality into a reusable module that you can import across your Node.js project. This class handles authentication, error handling, and supports all message types.
Need to justify your choice of WhatsApp API provider to stakeholders? Check our QuackAPI vs WATI comparison — covering pricing, setup time, and REST API flexibility for Node.js developers.
const axios = require("axios");
class QuackAPI {
constructor(apiKey) {
this.apiKey = apiKey || process.env.QUACKAPI_API_KEY;
this.client = axios.create({
baseURL: "https://quackapi.com",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
}
});
}
async sendText(deviceId, to, message) {
const { data } = await this.client.post("/v1/messages/send", {
deviceId, to, content: message, type: "text"
});
return data;
}
async sendImage(deviceId, to, imageUrl, caption = "") {
const { data } = await this.client.post("/v1/messages/send", {
deviceId, to, content: imageUrl, type: "image", caption
});
return data;
}
async sendDocument(deviceId, to, docUrl, filename) {
const { data } = await this.client.post("/v1/messages/send", {
deviceId, to, content: docUrl, type: "pdf", filename
});
return data;
}
}
module.exports = QuackAPI;
// Usage
const wa = new QuackAPI();
wa.sendText(1, "923001234567", "Hello from QuackAPI!").then(console.log);