Friday, 20 November, 2020 UTC


Summary

Just like Memphis Raines figured out how to steal fifty cars in one night, you’re going to learn how to send one WhatsApp message in thirty seconds (using JavaScript!).
Buckle up, cause here we go.
Prerequisites
  • A free Twilio account
  • Node.js and npm
Get your development environment setup
Create a new directory on your computer, change into it, and initialize a new Node.js project:
mkdir whatsapp cd whatsapp npm init -y 
Install the Twilio Node Helper Library and the dotenv package:
npm install twilio dotenv 
Create two new files inside your new whatsapp directory:
touch index.js && touch .env 
Open the .env file and add two environment variables to store your Twilio Account SID and Twilio Auth Token. Leave the values for each variable blank for the moment, you’ll collect these credentials shortly.
TWILIO_ACCOUNT_SID= TWILIO_AUTH_TOKEN= 
Activate the Twilio Sandbox for WhatsApp
Head to the WhatsApp section of your Twilio Console to activate the Twilio Sandbox for WhatsApp. The sandbox lets you test out Twilio’s WhatsApp API using a shared, universal number without having to wait for your Twilio number to be approved by WhatsApp.
After checking the box to activate the sandbox, you’ll be asked to send a specific code message from your WhatsApp number to the provided universal WhatsApp number. After completing this connection, you’ll see this success screen:
Write some code
Head to your main dashboard within the Twilio Console and find your Account SID and Auth Token. Copy and paste these values into the .env file you created earlier as the values for the variables TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN respectively.
Open up the index.js file you created earlier in your favorite text editor. Copy and paste the following code into this file:
require('dotenv').config();  // loads the Twilio library, which can find your environment variables automatically const accountSid = process.env.TWILIO_ACCOUNT_SID; const authToken = process.env.TWILIO_AUTH_TOKEN; const client = require('twilio')(accountSid, authToken);  client.messages.create({  from: 'whatsapp:+14155238886', // shared WhatsApp number  body: 'Ahoy world!',  to: 'whatsapp:+15555555555' // change this to your personal WhatsApp number }).then(message => console.log(`Message sent: ${message.sid}`)); 
The code above loads the Twilio library. Then it creates and sends a new message from the shared WhatsApp Sandbox number to your personal WhatsApp number.
Be sure to change the phone number on line 11 above so it reflects your personal WhatsApp number.
Save your file.
Back in your command line, enter the following command to run your file and test out your work:
node index.js 
When the message has been sent, a success message will be logged to your command line, and you’ll receive a WhatsApp message that says “Ahoy world!”
Next steps
Expand your new found JavaScript and WhatsApp skills by trying out another tutorial, like using location data in WhatsApp to find nearby healthy restaurants. Have fun, and let me know on Twitter what projects you’re building!
Ashley is a JavaScript Editor for the Twilio blog. To work with her and bring your technical stories to Twilio, find her at @ahl389 on Twitter. If you can’t find her there, she’s probably on a patio somewhere having a cup of coffee (or glass of wine, depending on the time).