Whatsapp API send message Node JS

How to Send a Message by WhatsApp API using Node.js

Before getting started, ensure that Node.js and npm (Node Package Manager) are installed on your local machine or server.

After registering on our platform, go to the channel page, which will be immediately available to you.

You will now need to link any number to the channel. To do this, use the QR code that is displayed inside the channel.

Open your WhatsApp mobile app, navigate to Settings -> Linked devices -> Link a device -> Scan QR code

Our service enables you to interact with the API without the need for an uninterrupted phone connection. Operate WhatsApp on up to four paired devices and one mobile device concurrently.

Now save the generated API-Key token on channel page:

WhatsApp Messaging with Node.js, Example

Before diving into the code, you need to install the axios package which will be used for HTTP requests. Run npm install axios from your script's directory in your terminal.

To send a text message, you need to make a request to:

POST https://gate.whapi.cloud/messages/text

Test requests and sample code snippets

Here is an example of how to send a WhatsApp message using Node.js:

const request = require('request');

const options = {
  method: 'POST',
  url: 'https://gate.whapi.cloud/messages/text',
  headers: {
    accept: 'application/json',
    'content-type': 'application/json',
    authorization: 'Bearer iVwcoBzBcB2bsbNShV4dDCXPwp1mvBcs'
  },
  body: {typing_time: 0, to: '14409416972', body: 'Hello'},
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

To run this script, save it in a .js file (for example, whapi_script.js), navigate to the script's directory in the terminal, and run the command node whapi_script.js. The message specified in the 'body' of the data object will be sent to the WhatsApp number specified in 'to'. Ensure to replace the 'to' and 'token' values with your actual details.

The parameters are as follows:

  • to: The recipientโ€™s WhatsApp number in international format (e.g., 14409416972) or chatID for contact or group (e.g 14409416972@s.whatsapp.net or 12560234567890@g.us).

  • body: The content of the message.

This script includes basic error handling with the .catch method, logging any errors that occur during execution. For a production environment, be sure to introduce appropriate error and exception handling mechanisms.

Get Started Free


Send Media Message

Additional endpoint for easy send media-file as message. Use request body as file and inpath parameters for send parameters. Media message can be one of the following types:

Send Image

Send Document

Send Audio

Send Voice

You can Send a ppt audio recording to the phone number or group, But WhatsApp is sensitive to this extension, you need to be in OGG format and the codecs should be the opus, For more information, you can check out this article.

Send Video

Send GIF

Send Contact

Send Contact List

Send Location

Send Sticker

Last updated