Send Message to WhatsApp using Twilio in Node.js
Last Updated :
31 Mar, 2021
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:Â
- Widely used module for sending text messages.
- Supports various languages.
- 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 sentOnce 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.
JavaScript
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
client.messages
.create({
// 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"))
.done();
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.
Similar Reads
Send unlimited Whatsapp messages using JavaScript When it comes to web development, JavaScript can do wonders! Let me show you one more wonder of JavaScript. Wouldn't it be cool if we could send infinite WhatsApp messages with just one click? be the first one to wish birthdays/anniversaries/special events to your loved ones? schedule any message fo
4 min read
Sending bulk SMS in Node.js using Twilio SMS is a common method of sending short messages between cell phones, but these SMS can be sent to multiple users at a time using Twilio notify service. Sending bulk SMS to users at a time is possible using Twilio. Introduction: It's easy to get started and easy to use. It is widely used for sending
2 min read
How to Edit Messages on WhatsApp Have you ever sent a message on WhatsApp and instantly noticed a typo or wished you could change the wording? It happens to the best of us! Whether you want to correct spelling mistakes, adjust your tone, or completely rephrase your text, WhatsApp now lets you easily edit your messages. No need to d
7 min read
Automate WhatsApp Messages With Python using Pywhatkit module We can automate a Python script to send WhatsApp messages. In this article, we will learn the easiest ways using pywhatkit module that the website web.whatsapp.com uses to automate the sending of messages to any WhatsApp number. Installing pywhatkit module: pywhatkit is a python module for sending W
4 min read
How to Message Yourself on WhatsApp (2 Ways) From the day of its creation, WhatsApp has been the favorite as well as essential messaging app for many. Low-cost messaging, easy-to-make group discussions, and many other features made it popular. With over 5 Billion downloads across the world, WhatsApp is currently the top messaging application a
5 min read