Send Messages To WhatsApp Groups Using Node Js

July 26, 2024

This tutorial will teach you how to send messages to WhatsApp groups using the API.

Efficient communication is crucial for coordinating a project, building a community, or managing customer support. Wassenger simplifies this by enabling you to manage and engage WhatsApp groups seamlessly. With Wassenger’s API and automation tools, sharing messages, videos, images, documents, and voice memos becomes easy.

Here’s a guide on how to send messages to WhatsApp groups using Node.js.

🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and get started in minutes**!**

If you need to send messages from code, you can use any programming language to perform HTTPS API requests. Below is the live API tester with ready-to-use code examples in various programming languages.

Requirements

  • Have a WhatsApp number already linked to the platform and online.
  • Group WhatsApp ID (WID) that you can find in two ways:

How to obtain the Group WhatsApp ID

You can obtain the Group WhatsApp ID by using one of these methods:

  1. Web: go to number’s settings > Groups > Copy the Group WID.
  2. API: query the available groups in your number using this endpoint.

API endpoint

We will use the following API endpoint to send messages to a group:

🖥️ Looking for a code example? Go to the API live tester and get ready-to-use code examples in 15+ programming languages, including Python, JavaScript, PHP, C#, Java, Ruby, Go, Powershell, cURL and more.

Send automated messages with Node (fetch)

  • Setup: The url, apiKey, phone, and message variables are defined for easy modification.
  • Payload: The payload is created as a JavaScript object and then stringified using JSON.stringify.
  • Options: The options for the fetch request include method, headers, and body.
  • Fetch Call: The fetch the call is made with the URL and options. The response is checked for success, and the JSON response is logged to the console.
  • Error Handling: Errors are caught and logged to the console.
// Examples requires you to have installed node-fetch Node.js package.
// Install it by running: npm install --save node-fetch or yarn add node-fetch
const fetch = require('node-fetch');
const url = 'https://api.wassenger.com/v1/messages';
const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Token: 'API_TOKEN_GOES_HERE'
  },
  body: '{"group":"12036302981363@g.us", "message":"Sample group message"}'
};
try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}

🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!

Send automated messages with Node (Axios)

  • Setup: The url, apiKey, phone, and message variables are defined for easy modification.
  • Options: The options for the axios request include method, URL, headers, and data (the payload).
  • Axios Request: The axios.request method is called with the options. The response is logged to the console on success.
  • Error Handling: Errors are caught and logged to the console, with differentiation between response errors, request errors, and setup errors.
const axios = require('axios').default;
const options = {
  method: 'POST',
  url: 'https://api.wassenger.com/v1/messages',
  headers: {
    'Content-Type': 'application/json',
    Token: 'API_TOKEN_GOES_HERE'
  },
  data: { group: '12036302981363@g.us', message: 'Sample group message' }
};
try {
  const { data } = await axios.request(options);
  console.log(data);
} catch (error) {
  console.error(error);
}

Send automated messages with Node (Unirest)

  • Setup: The url, apiKey, phone, and message variables are defined for easy modification.
  • Headers: Headers for Content-Type and Token are added to the request.
  • Payload: The request body is set to a JSON object with the phone number and message.
  • End Callback: The end method is used to send the request, and a callback function handles the response. Errors are logged if they occur, and the response body is printed to the console on success.
// Examples requires you to have installed unirest Node.js package.
// Install it by running: npm install --save unirest or yarn add unirest
const unirest = require('unirest');
const req = unirest('POST', 'https://api.wassenger.com/v1/messages');
req.headers({
  'Content-Type': 'application/json',
  Token: 'API_TOKEN_GOES_HERE'
});
req.type('json');
req.send({
  group: '12036302981363@g.us',
  message: 'Sample group message'
});
req.end(function (res) {
  if (res.error) throw new Error(res.error);
  console.log(res.body);
});

🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!

More examples for group messages with Node (fetch)

For (axios and unirest) visit our API Live Tester

Send images to a group chat with Node (fetch)

// Examples requires you to have installed node-fetch Node.js package.
// Install it by running: npm install --save node-fetch or yarn add node-fetch
const fetch = require('node-fetch');
const url = 'https://api.wassenger.com/v1/messages';
const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Token: 'API_TOKEN_GOES_HERE'
  },
  body: JSON.stringify({
    group: '12036302981363@g.us',
    message: 'This is a caption for an image message',
    media: {
      url: 'https://picsum.photos/seed/picsum/600/400'
    }
  })
};
try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}

Send audio to a group chat with Node (fetch)

// Examples requires you to have installed node-fetch Node.js package.
// Install it by running: npm install --save node-fetch or yarn add node-fetch
const fetch = require('node-fetch');
const url = 'https://api.wassenger.com/v1/messages';
const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Token: 'API_TOKEN_GOES_HERE'
  },
  body: JSON.stringify({
    group: '12036302981363@g.us',
    media: {
      url: 'https://download.samplelib.com/mp3/sample-9s.mp3',
      format: 'ptt'
    }
  })
};
try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}

Send video to a group chat with Node (fetch)

// Examples requires you to have installed node-fetch Node.js package.
// Install it by running: npm install --save node-fetch or yarn add node-fetch
const fetch = require('node-fetch');
const url = 'https://api.wassenger.com/v1/messages';
const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Token: 'API_TOKEN_GOES_HERE'
  },
  body: JSON.stringify({
    group: '12036302981363@g.us',
    message: 'This is a caption for a video message',
    media: {
      url: 'https://download.samplelib.com/mp4/sample-5s.mp4'
    }
  })
};
try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}

Send documents to a group chat with Node (fetch)

// Examples requires you to have installed node-fetch Node.js package.
// Install it by running: npm install --save node-fetch or yarn add node-fetch
const fetch = require('node-fetch');
const url = 'https://api.wassenger.com/v1/messages';
const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Token: 'API_TOKEN_GOES_HERE'
  },
  body: JSON.stringify({
    group: '12036302981363@g.us',
    media: {
      url: 'https://www.africau.edu/images/default/sample.pdf'
    }
  })
};
try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}

Live API testing

You can live-test and play with the API directly from your browser.

Once you are done testing, get the auto-generated code example in your preferred programming language and you will be ready to go.

Try our API-Live tester now

FAQ

How to send messages to multiple phone numbers

You just have to send multiple API requests, one per target phone number.

For instance, if you want to send a message to 10 phone numbers, you should send 10 independent HTTPS requests to the API.

There is no option to send multiple messages in a single API request.

How to validate if a phone number can receive WhatsApp messages

You can validate if a given phone number is linked to a WhatsApp account and can receive messages.

The API provides an endpoint that can validate whether a given phone number exists in WhatsApp or not.

The only requirement is to have at least one WhatsApp number connected to the platform in your current account.

For more details, please check out the API endpoint documentation here.

Before you check if a phone number exists on WhatsApp, you can also validate and normalize the format of a list of phone numbers by using the numbers validator API endpoint. This endpoint only validates the correct E164 format, but it does not check whether the phone number effectively exists on WhatsApp.

Note: The number of WhatsApp check validations is limited per month based on your subscription plan. Please check out the pricing table for more details about the limits.

Looking for more answers? Check out the extended FAQs.

Further useful resources

API Documentation

For more details about the endpoint API, please check the documentation where you will find all the details about the accepted request params, possible success or error responses and ready-to-use code examples in multiple programming languages:

https://app.wassenger.com/docs/#tag/Messages/operation/createMessage

Ready to transform your WhatsApp communication?

Start automating your customer interactions today with Wassenger

Get Started Free