WhatsApp is the biggest messaging platform right now, with over two billion users worldwide. In many countries, most people with a smartphone are on WhatsApp, which is why a lot of businesses and clients want the integration of sending notifications/messages through WhatsApp.
Before you start sending messages, WhatsApp requires an explicit opt-in by users, whatever service you are using. The following is a screenshot from Twilio docs.

Let’s see how you do that in the case of Twilio. Go to https://www.twilio.com/console. In the left pane click on send a WhatsApp message.

Now, let’s opt-in and begin with our testing. Once our testing works fine, we will write a program to do this.
Send the message join ********** to +1 415 523 8886.Replace the start with the message you see on your Twilio console. The following is for my console.

Once you do that it should look like this.

And your phone’s WhatsApp chat should look like this.

Now, let’s test the connection. Click on send a one-way message in your console.

Select one of the following three options. I am selecting Appointment Reminders.

After doing that click on make request, you will receive the message:

Click on the two-way message now. Send a reply from your phone, this will be received by Twilio and it will show you the option to send a reply.

You can send the reply and it will be received by your phone.
Now, since all this is working, we will write a program to do this.
Create an empty folder, open it in your IDE of choice and initialize an empty nodejs project by running npm init. Create a file called index.js.
Now install the Twilio express package by the command npm i twilio express.
Open the index.js file and paste the following code into it.
1
2
3
4
5
6
7
const express = require('express');
const app = express();
app.listen(3000, () => {
console.log('Server Started');
});
It will set up a server and run the file with the command nodemon index.js. You should see the following.

Now, import the Twilio package in the following way and instantiate with a variable called client.
const client = require('twilio')();
Twilio provides an account SID and auth token, you will need this so that your app can be verified. Make sure to never make these two things public. It is best to store them in environment variables and use them from there.
To see your credentials go to the top-right corner and click on my account > general settings.

There you’ll see your SID and auth token.
Now, modify your import statement like this.
const client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
If you are not using environment variables then you can put the credentials directly in as well. If you do not know how to configure environment variables, check out the instructions at the end of the article.
Add the following import statements so that your app can process json data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
Now, create the following API to send the message.
app.post("/", (req, res) => {
const msg = req.body.msg;
client.messages.create({
from: 'whatsapp:+14155238886',
to: 'whatsapp:+911234567890',
body: msg
})
.then(message => res.send(message))
.catch(err => {
console.log(err);
res.send(err)
})
});
You can change the to, from, and message body accordingly.
Let’s test this now.
Open postman and make a post request with the following body to http://localhost:3000.
1 2 3{ "msg": "Hi" }
Hit send.
You will receive the text on your phone.
Install dotenv package using npm i dotenv.
Create a file called .env.
If you want to make your auth token private, with an example value of 1234567890, then in the .env file write. AUTH_TOKEN=1234567890.
In your index.js file write require(‘dotenv’).config().
Now you can access your environment variables using process.env. For example, to use auth token, write process.env.AUTH_TOKEN.
That’s it. You can now send messages using Twilio.