Programmable Messaging for WhatsApp and Node.js Quickstart
Twilio is launching a new Console. Some screenshots on this page may show the Legacy Console and therefore may no longer be accurate. We are working to update all screenshots to reflect the new Console experience. Learn more about the new Console.
With just a few lines of code, your application can send and receive messages with WhatsApp using the Twilio API for WhatsApp.
- Sign up for Twilio and activate the Sandbox.
- Set up your development environment to send and receive messages
- Opt-in to the Sandbox
- Send your first WhatsApp message
- Receive inbound WhatsApp messages
- Reply to incoming WhatsApp messages
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 will need this later when we're ready to send some messages.
Gather your Twilio account information
Before you can send any messages, you'll need to gather your Twilio account credentials. You can find these in the Twilio Console.
- Account SID - Used to authenticate REST API requests
- Auth Token - Used to authenticate REST API requests
For all of our code snippets and curl examples, you will need to authenticate with the Account SID and Auth Token.
Set up your Node.js development environment
The next steps will involve writing some code. We've written up development environment setup in Node.js, where you'll set up a dev environment and prepare to build a simple web app with Express.
Send a message with WhatsApp in Node.js
Using pre-provisioned Sandbox numbers
The sandbox is pre-provisioned with three Twilio phone numbers that are shared across all sandbox users. In order to use the Sandbox, you MUST start by opt-ing in to the sandbox by sending the phone number you chose a message from WhatsApp. Once opted-in, you will only receive messages from your specific Sandbox.
These limitations do not exist on your own business identity which you can request to be provisioned on WhatsApp.
Sandbox Opt-in Message
join <your sandbox keyword>” to your Sandbox number in WhatsApp to join your Sandbox, and we’ll reply with a confirmation that you’ve joined. Your sandbox keyword can be found in the console.
Once you join, you will only receive messages from your specific Sandbox. To disconnect from the sandbox, you can reply to the message from WhatsApp with `sandbox stop`, or switch to a different sandbox by messaging `
join <other sandbox keyword>`.
Inviting other users to your sandbox (OPTIONAL)
To invite someone else to your sandbox, create a link with the following format containing the opt-in message and send it to them:
whatsapp://send?phone=<Your Sandbox Number>&text=<your URL-encoded sandbox keyword>
You can also create a QR code with the link format above that users can scan on their phone to opt-in to your sandbox.
To send a message, use the following code and replace the to parameter with the phone number for your personal WhatsApp account in the E.164 format. (If you haven't already, install WhatsApp on your device and register for an account.) For the from parameter, be sure to include the
whatsapp: channel identifier before the Sandbox number in E.164 format.
From parameter, you need your
Sender ID which should be shown on the installed channel in the Twilio Console, as shown previously.
Receive and reply to messages from WhatsApp
When someone replies to one of your messages, you will receive a webhook request from Twilio.
You can configure webhooks by connecting your Sandbox to an app you've already built for handling incoming messages, or build a new one for WhatsApp messages.
This webhook request also supports TwiML (the Twilio Markup Language) just like a regular Twilio SMS request.
To handle this request, you need to set up a web application and expose it to the internet. The Node.js SMS Quickstart shows you how to respond to a message and generate TwiML in Node with Express.
And - that's all there is to it; receiving and responding is exactly the same as you would do in any SMS app with our Messaging API. Cool, right?
Although these Quickstarts show you how to receive an SMS message, the webhook that Twilio will send will include the same parameters as an incoming SMS message, with the exception that To and From addresses will be set to the WhatsApp number receiving the message (
whatsapp:<E.164 formatted Twilio phone number associated with your business>) and the WhatsApp number sending the message (
whatsapp:<User’s E.164 phone number>), respectively.
WhatsNext for WhatsApp and Node?
Because the Twilio WhatsApp API is essentially the same as the Twilio Programmable SMS API, all of the documentation for that API applies to your apps sending and receiving messages with WhatsApp. To dive deeper with a WhatsApp integration, see the WhatsApp documentation overview as well as the API Reference.
Here are some areas you might like to explore next.
- Send appointment reminders
- Create SMS conversations
- Messaging API overview
- Browse other SMS tutorials
We can't wait to see what kind of WhatsApp integration you build!
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.