Back to Blog
Language How-Tos

How to Send WhatsApp Messages in Twilio Using Node.js

Send WhatsApp messages via Twilio in Node.js using the whatsapp: channel prefix with sandbox testing and production configuration.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
Node.js
JavaScript
How-To
Code
How to Send WhatsApp Messages in Twilio Using Node.js

This tutorial shows how to send WhatsApp messages using the Twilio Node.js SDK by prefixing both the sender and recipient numbers with the whatsapp: scheme. It covers the sandbox setup for development and the steps to move to a production WhatsApp Business Account. You will send your first WhatsApp message in Node.js and verify it arrives in the WhatsApp app on your test device.

Prerequisites and Setup

Install the Twilio SDK with npm install twilio dotenv and configure your credentials in a .env file. Join the Twilio WhatsApp sandbox by sending the sandbox join keyword from your WhatsApp account to the sandbox number shown in the Twilio Console under Messaging, Try it out, Send a WhatsApp message. The sandbox number is always +14155238886 and the from value in your code must be prefixed as whatsapp:+14155238886. Outbound sandbox messages must match one of Twilio's pre-approved templates unless the recipient has messaged you first within the 24-hour conversation window.

npm install twilio dotenv

# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_WHATSAPP_NUMBER=whatsapp:+14155238886

The Complete Code

The only difference between sending a WhatsApp message and an SMS in the Twilio SDK is the whatsapp: prefix on both from and to. The message body, mediaUrl, and statusCallback parameters work identically to SMS. The returned MessageInstance has the same sid and status fields, making it easy to track WhatsApp messages through the same delivery pipeline you use for SMS. Error handling for WhatsApp uses the same err.code pattern as SMS, but WhatsApp-specific error codes start in the 63xxx range.

require('dotenv').config();
const twilio = require('twilio');

const client = twilio(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);

async function sendWhatsApp(toNumber, body) {
  try {
    const message = await client.messages.create({
      body: body,
      from: process.env.TWILIO_WHATSAPP_NUMBER,
      to: 'whatsapp:' + toNumber,
    });
    console.log('WhatsApp SID:', message.sid);
    console.log('Status:', message.status);
    return message.sid;
  } catch (err) {
    console.error('WhatsApp error', err.code, err.message);
    return null;
  }
}

sendWhatsApp('+15551234567', 'Hello from Telphi Consulting via WhatsApp!');

Testing and Debugging

Run the script and open your WhatsApp app to confirm the message arrives from the Twilio sandbox number. If you receive error code 63016, your number has not joined the sandbox; send the join keyword from your WhatsApp account to the sandbox number and try again. Error code 63007 means the recipient number is not registered on WhatsApp; verify the number exists on a real device before testing. Check the Twilio Console under Messaging Logs for the full delivery status chain if the script reports success but the WhatsApp message does not appear.

Taking It Further

Apply for a WhatsApp Business Account through Twilio to move out of the sandbox and message any WhatsApp user without requiring them to join a sandbox first. Use Twilio's Content Template Builder to create pre-approved message templates for transactional notifications, which are required for outbound messages initiated outside an active 24-hour conversation. Add mediaUrl to the create options to send images, PDFs, or audio clips as WhatsApp attachments alongside the message body. Build a bidirectional WhatsApp bot by combining this outbound code with an Express inbound webhook that parses the Body field and replies with TwiML MessagingResponse.

Contact Telphi Consulting to set up a WhatsApp Business channel with approved templates and automated conversation workflows.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.