# Whatsapp API send message Node JS

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

{% hint style="info" %}
Detailed [tutorial on how to create a bot on Node.JS](https://whapi.cloud/whatsapp-bot-nodejs)
{% endhint %}

After [registering](https://panel.whapi.cloud/register) 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**

<figure><img src="https://2417185145-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhsdgGmVCG31mEaHyRvxC%2Fuploads%2Fpf5TOcIsKhmMv9fcG5ZX%2F00000.png?alt=media&#x26;token=e82d421c-c25e-4858-a18d-e3f8c72c69ab" alt=""><figcaption><p>Pair any number</p></figcaption></figure>

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:

<figure><img src="https://2417185145-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhsdgGmVCG31mEaHyRvxC%2Fuploads%2F05KGZwXRsJPZ4roIjwHs%2Fchannel-tips.png?alt=media&#x26;token=eba3a04d-0a26-49cc-84a4-fc56260eed1f" alt=""><figcaption><p>Grab your token</p></figcaption></figure>

### 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:&#x20;

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

{% embed url="<https://whapi.readme.io/reference/sendmessagetext>" %}
Test requests and sample code snippets
{% endembed %}

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

```javascript
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: 10, 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](https://support.whapi.cloud/help-desk/faq/chat-id.-what-is-it-and-how-to-get-it) 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.

{% hint style="success" %}
Here is the [source code of powerful chatbots in Node.JS](https://support.whapi.cloud/help-desk/source-code/whatsapp-chatbot/whatsapp-node-js-bot)
{% endhint %}

{% embed url="<https://youtu.be/qlJW6rUycw8?si=dq5Tit7qswo0cveO>" %}

[<mark style="background-color:yellow;">**Get Started Free**</mark>](https://panel.whapi.cloud/register)

***

### 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:

{% embed url="<https://whapi.readme.io/reference/sendmediamessage>" %}

### Send Image <a href="#send-image" id="send-image"></a>

{% embed url="<https://whapi.readme.io/reference/sendmessageimage>" %}

### Send Document <a href="#send-document" id="send-document"></a>

{% embed url="<https://whapi.readme.io/reference/sendmessagedocument>" %}

### Send Audio <a href="#send-audio" id="send-audio"></a>

{% embed url="<https://whapi.readme.io/reference/sendmessageaudio>" %}

### Send Voice <a href="#send-voice" id="send-voice"></a>

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**. Our API has an automatic video and audio converter for the necessary codecs compatible with WhatsApp. For more information, you can check out this [article](https://support.whapi.cloud/help-desk/sending/overview-of-other-methods-for-sending/send-voice-message).

{% embed url="<https://whapi.readme.io/reference/sendmessagevoice>" %}

### Send Video <a href="#send-video" id="send-video"></a>

{% embed url="<https://whapi.readme.io/reference/sendmessagevideo>" %}

### Send GIF

{% embed url="<https://whapi.readme.io/reference/sendmessagegif>" %}

### Send Contact <a href="#send-contact" id="send-contact"></a>

{% embed url="<https://whapi.readme.io/reference/sendmessagecontact>" %}

### Send Contact List

{% embed url="<https://whapi.readme.io/reference/sendmessagecontactlist>" %}

### Send Location <a href="#send-location" id="send-location"></a>

{% embed url="<https://whapi.readme.io/reference/sendmessagelocation>" %}

### Send Sticker

{% embed url="<https://whapi.readme.io/reference/sendmessagesticker>" %}
