Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Send Message to WhatsApp using Twilio in Node.js

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

We all use WhatsApp in our daily life to send text messages to one another. We can send WhatsApp messages using the Twilio API in NodeJS using the following approach.

Features of Twilio: 

  1. Widely used module for sending text messages.
  2. Supports various languages.
  3. Can be also used to send programmable voice messages.

Step 1: Creating a Twilio Account

Create a Twilio account and go to the WhatsApp Sandbox section, you can find the WhatsApp Sandbox section over here.


Step 2: Linking your WhatsApp number

Send a message defined by Twilio from your WhatsApp number to +14155238886 to set up the Twilio Sandbox.

The code mentioned in the below image is join movement-limited

The code which needs to be sent

Once the sandbox is successfully set up, we get a success message as shown in the below image.

Success message shown when the WhatsApp number is successfully linked.

Step 3: Setting up our files.

To setup our project, we first need to add a package.json file that keeps track of our dependencies. The package.json file is initialized using the following command.

npm init --y

Note: The –y tag makes yes as the default answer for all the questions.


Step 4: Installing the Twilio module.

Once our number is successfully linked, we can start sending WhatsApp messages. But first, we need to install the Twilio module. You can learn more about the Twilio module here. We can install the Twilio module using the following command.

npm i twilio

Step 5: Creating a file

Create a file with .js extension in the same npm repository. We can run the file using the following command.

node filename.js

Step 6: Importing the Twilio module.

To use the Twilio, we must import it. We can import the Twilio module using the following command.

const twilio = require('twilio')

Step 7: Getting the auth token and id from our Twilio account.

We get the auth token and id when we have successfully linked our WhatsApp number. To find your own auth token and id, go to the one-way messaging section of WhatsApp on the Twilio website.

Step 8: Sending the WhatsApp message.

We can send the WhatsApp message by creating a client object by passing the id and auth token and send the messages using the message.create function. (A JavaScript Promise). You can learn more about promises here.

The below code is used to send WhatsApp messages from the Twilio Sandbox.


const id = 'Provide your id';
const token = 'Provide your token';
// Importing the Twilio module
const twilio = require('twilio');
// Creating a client
const client = twilio(id, token);
// Sending messages to the client
        // Message to be sent
        body: 'Hello from Geeks for Geeks',
        // Senders Number (Twilio Sandbox No.)
        from: 'whatsapp:+14155238886',
        // Number receiving the message
        to: 'whatsapp:Provide your number'
    .then(message => console.log("Message sent successfully"))

Step 9: Execution

Make sure you are in the same directory in which the file is created. Execute the above code using the following command.

node twilio.js

Note: Here the file name provided is twilio.js. 

Upon successful execution, we get a message in the console as shown in the below image.

The WhatsApp messages are received as shown in the below image.

My Personal Notes arrow_drop_up
Last Updated : 31 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials