Traditionally, businesses contact customers by phone or SMS, but sometimes you really want to communicate with your customers via WhatsApp. The Vonage dashboard now allows you to test out WhatsApp messaging without a WhatsApp business account. You can use the cURL command in the Messages API Sandbox to verify you’re able to send a message. Once you’ve tried that, you might like to see how you can incorporate WhatsApp messaging into your Node.js application.

Prerequisites

You can make use of the sandbox to test out your code without doing a lot of set up. Node already includes everything we need to make a POST request, and the Messages API does the rest. All you need is:

  • Node
  • A Vonage Developer account
  • A whitelisted WhatsApp number to test with

Setting up Your Sandbox

If you haven’t already, navigate to Messages and Dispatch > Sandbox in your dashboard to set up your sandbox. The quickest way is to take your device with WhatsApp installed and center the camera on the QR code supplied. Sending the custom message generated will whitelist your number.

You’ll see several additional options for joining the whitelist, including manually creating a message to the number supplied with a unique passphrase. If none of the others will work for your setup, that one should.

Sending a WhatsApp message to join the whitelist

Creating Your Data

To make a call to the sandbox API you’ll need to supply a unique username, password, and to and from WhatsApp numbers. The username and password will be the two halves of the u argument in the cURL command from your dashboard. Copy the masked value, which will look like 12ab3456:123AbcdefghIJklM. The part before the colon is your username and the part after is your password. They’re the same as your Vonage API key and secret, which are also available from Getting Started in the dashboard.

You can also copy the data for your request straight from the cURL command and stringify it. It tells the API to send a text message from the sandbox number to your whitelisted number. You can change the text of the message to whatever you like, including generating it programmatically:

var user = '12ab3456';
var password = '123AbcdefghIJklM';
var from_number = '14151234567';
var to_number = '441234567890';

const data = JSON.stringify({
  "from": { "type": "whatsapp", "number": from_number },
  "to": { "type": "whatsapp", "number": to_number },
  "message": {
    "content": {
      "type": "text",
      "text": "Hi! Your lucky number is " + Math.floor(Math.random() * 100)
    }
  }
});

#node #javascript #web-development #programming #developer

How to Send a WhatsApp Message with Node.js
20.75 GEEK