How To Send Location Details on WhatsApp in Node JS

December 13, 2019
Written by
Felistas Ngumi
Contributor
Opinions expressed by Twilio contributors are their own

How To Send Location Details on WhatsApp in Node JS

WhatsApp is one of the most preferred mobile messaging applications in the world with over 1.5 billion active monthly global users. Since the introduction of WhatsApp business, companies have leveraged this API to engage with their customers to send order or payment notifications, appointment reminders, and shipping alerts among others. One of the key reasons for increased adoption and trust of WhatsApp for users and businesses is the secure end-to-end encryption. Users can share user contacts, documents, locations, and different types of media such as images, audio, and videos. The Twilio API for WhatsApp recently added support for read receipts and location messages. In this tutorial, I will take you through how to send location messages using the Twilio API for WhatsApp.

Prerequisites

To complete this tutorial you will need the following:

  1. Twilio Account
  2. Node Version 8 and above

Getting Started

In your preferred terminal, run the following commands to set up the project:

$ mkdir demo && cd demo
$ npm init -y
$ npm i axios dotenv --save
$ touch test.js .env

The commands above created a folder to store all of your project files named demo. A package.json file was created by running npm init -y which will keep track of all the packages like axios and dotenv. These two packages are responsible for making HTTP requests and accessing environment variables respectively.

Activating Twilio WhatsApp Sandbox

After obtaining a free account from Twilio, create a project under the Programmable SMS product. You should be redirected to the screen below:

Twilio Dashboard

To activate the WhatsApp sandbox, click on Get Started and navigate to the WhatsApp Beta page. Send the indicated code to +1 415 523 8886 from your device.

In my case, I’ll send join smooth-took to +1 415 523 8886

Twilio WhatsApp Sandbox

Using your preferred code editor, add the following lines of code inside your test.js file.

const axios = require("axios");
const dotenv = require("dotenv");
dotenv.config();

const authToken = process.env.ACCOUNT_TOKEN;
const authSID = process.env.ACCOUNT_SID;
const url = process.env.TWILIO_URL;

const messageBody = {
 Body: "Twilio HQ",
 From: "whatsapp:+14155238886",
 PersistentAction: "geo:37.787890,-122.391664",
 To: "whatsapp:+254712345678"
};

axios
 .post(url, new URLSearchParams(messageBody), {
   auth: {
     username: authSID,
     password: authToken
   }
 })
 .then(
   response => {
     console.log(response.data.sid);
   },
   error => {
     console.log(error);
   }
 );

Note: Replace To with your actual phone number.

In your .env file, add:

ACCOUNT_SID=your_account_SID
ACCOUNT_TOKEN=your_account_token
TWILIO_URL=https://api.twilio.com/2010-04-01/Accounts/your_account_SID/Messages.json

Note: Be sure to replace the above keys with their actual values. They can be obtained from the Twilio console.

Testing

In your terminal, run:

$ node test.js

You should receive a text on your phone including geolocation.

WhatsApp message

Conclusion

In this tutorial, we have learned how to send location details via Twilio API for
WhatsApp. At the time of writing, this feature is only available via CURL and is yet to roll out to the Twilio packages and Twilio CLI. I would love to hear from you! Let’s connect on Twitter or send me an email. Happy hacking!