September 22, 2025 • 6 min read
Hey there! If you're looking to add WhatsApp messaging to your app or business workflow, without worrying about the WhatsApp Business API, Twilio makes it surprisingly straightforward. WhatsApp is huge for customer communication, especially in regions where it's the go-to messaging app. By integrating it with Twilio, you can send notifications, handle support chats, or even automate reminders, all programmatically.
In this guide, I'll walk you through the process step by step, starting with Twilio's sandbox for testing (no real costs or approvals needed upfront) and then moving to production using WhatsApp senders. WhatsApp senders are essentially your approved phone numbers tied to a WhatsApp Business Account, which let you send messages at scale under your brand.
We'll use Python for the code examples because it's beginner-friendly, but Twilio supports tons of languages like PHP, Node.js, Java, and more. Let's dive in!
Before we start, you'll need:
Twilio handles the heavy lifting with their API, so you don't need to deal directly with WhatsApp's backend.
Set them as environment variables to keep things secure (don't hardcode them in your scripts!). On macOS/Linux, open your terminal and run:
export TWILIO_ACCOUNT_SID='your_account_sid_here'
export TWILIO_AUTH_TOKEN='your_auth_token_here'
On Windows, use set instead of export.
This setup ensures your code can authenticate with Twilio's servers.
The sandbox is a testing environment where you can send and receive messages without full WhatsApp approval. It's perfect for prototyping.
Pro tip: Only numbers that have joined the sandbox can interact with it. For testing with multiple users, have them join too. This is all free during the trial.
Now, let's send a message from your code to your phone via the sandbox.
Paste in this code:
from twilio.rest import Client
import os
# Grab your credentials from environment variables
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
message = client.messages.create(
from_='whatsapp:+14155238886', # Your sandbox number
body='Hello from Twilio! This is your first WhatsApp message.',
to='whatsapp:+12345678901' # Your WhatsApp number in E.164 format (e.g., +1 for US)
)
print(f"Message SID: {message.sid}")
If it works, congrats! You've just sent a programmatic WhatsApp message. Note: For business-initiated messages outside a 24-hour reply window, you'll need pre-approved templates in production.
To make it two-way, you need a webhook to handle incoming messages.
pip install flask
.Create a new file called reply_whatsapp.py:
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.route('/whatsapp', methods=['POST'])
def whatsapp_reply():
# Get the incoming message
incoming_msg = request.values.get('Body', '').lower()
resp = MessagingResponse()
msg = resp.message()
# Simple echo reply for testing
msg.body(f'You said: {incoming_msg}. Thanks for chatting!')
return str(resp)
if __name__ == '__main__':
app.run(port=3000)
ngrok http 3000
. Note the forwarding URL (e.g., https://abc123.ngrok.io).
This webhook setup is crucial for real apps since you can expand it to integrate with databases, AI, or other services.
Once testing is done, switch to production for real users.
whatsapp:+your-approved-number
.Pricing: Messages cost about $0.005–$0.01 each, depending on the region. Monitor your usage in the console.
And there you have it, a fully functional WhatsApp integration! This guide covers the most crutial pieces in order to get started with WhatsApp messaging, and Twilio makes it easy to use without having to worry too much about setting custom APIs for the WhatsApp Business setup. As you can see, you can send messages programatically in a few lines of code.
Ready to take your project to the next level?
Contact meSr. Software Engineer
Senior software engineer located in Buenos Aires, Argentina. I specialize in building highly scalable web applications and I've been delivering MVPs and helping companies with their digital transformation for the past 7 years. I am proficient in a variety of technologies, including Laravel, Vue.js, Twilio, AWS, React.js, Node.js and MySQL.
If you enjoy the content that I make, you can subscribe and receive insightful information through email. No spam is going to be sent, just updates about interesting posts or specialized content that I talk about.