How I Automated WhatsApp Messages with Node.js
Easy way to Automate WhatsApp using Node.js
WhatsApp is a messaging service used by people all over the world. With Twilio’s Messaging API you can programmatically send WhatsApp messages. I was able to successfully integrate Whatsapp and send messages with Node.js.
Let’s walk through how to use JavaScript to send a message over WhatsApp.
Development Environment Setup
Let’s start by making sure we have the right software installed and set up that we’ll need to use for the rest of this article.
- Node.js and npm installed (do this first if you haven’t already)
- A free Twilio account with an activated WhatsApp Sandbox
- The Twilio Node library
Sign up for Twilio and activate the Sandbox
Before you can send a WhatsApp message from your web language, you’ll need to sign up for a Twilio account or sign into your existing account and activate the Twilio Sandbox for WhatsApp. It allows you to prototype with WhatsApp immediately using a shared phone number, without waiting for a dedicated number to be approved by WhatsApp.
To get started, select a number from the available sandbox numbers to activate your sandbox.

Be sure to take note of the phone number you choose in the Sandbox. You’ll need this later when we’re ready to send some messages.
In order to use the Sandbox, you need to opt in by sending the phone number you chose a message from WhatsApp. Send “join <your sandbox keyword>” to your Sandbox number in WhatsApp, and you will receive a confirmation that you’ve joined. Your sandbox keyword can be found in the console.

Sending a WhatsApp message with Node.js
Now that you have a Twilio account and have activated the WhatsApp Sandbox, you’re ready to dive into some code and send messages! Start by opening your terminal and navigating to the directory where you want your project to live and running the following command to initiate a package.json
file for npm to install dependencies:
npm init --yes
Now install the Twilio helper library for Node:
npm install twilio@3.30.0
With that out of the way, create a file called index.js
in this same directory and add the following code to it (don't forget to replace the example phone numbers with your WhatsApp Sandbox number and personal phone number respectively!):
index.js
const client = require('twilio')();client.messages.create({
from: 'whatsapp:+14155238886',
body: 'Ahoy world!',
to: 'whatsapp:+15555555555'
}).then(message => console.log(message.sid));
Before running this code, make sure you set the environment variables TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
with their respective values from your Twilio account credentials, which can be found in your Twilio Console. The Twilio Node library will automatically look at those values, so you can avoid hard coding them in your index.js
file!
Finally, in your terminal run the following command to run this code and send yourself a message on WhatsApp:
node index.js
Check your messages and you should see something like this!

Summary
One thing to keep in mind is that you will have to use a pre-approved template if you want to message someone more than 24 hours after their last incoming message.
I’m looking forward to seeing what you build. Feel free to reach out and share your experiences or ask any questions. See you, Bye!