This tutorial shows how to send WhatsApp messages using Twilio's WhatsApp API in Python, covering both the sandbox environment for development and the production setup for approved business accounts. It targets developers who want to add WhatsApp as a messaging channel alongside or instead of SMS. You will send your first WhatsApp message by joining the Twilio sandbox and prefixing the recipient number with the whatsapp: scheme.
Prerequisites and Setup
You need Python 3.7 or higher, the Twilio SDK installed, and a WhatsApp-enabled phone to join the sandbox. Install the SDK with pip install twilio python-dotenv and configure your Twilio credentials in a .env file. Join the Twilio WhatsApp sandbox by sending the sandbox join code (shown in the Twilio Console under Messaging, Try it out, Send a WhatsApp message) from your WhatsApp account to the sandbox number. The sandbox restricts you to pre-approved template messages for outbound conversations until a user messages you first, which is sufficient for initial testing.
pip install twilio python-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 the from_ and to numbers. The sandbox sender number is always whatsapp:+14155238886 for trial accounts. The message body must match one of Twilio's approved sandbox templates if the conversation is not already active; the simplest is "Your Twilio sandbox message" style text. The returned MessageInstance has the same sid and status fields as SMS, making it easy to track delivery.
import os
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
from dotenv import load_dotenv
load_dotenv()
client = Client(os.environ["TWILIO_ACCOUNT_SID"], os.environ["TWILIO_AUTH_TOKEN"])
try:
message = client.messages.create(
body="Hello from Telphi Consulting via WhatsApp!",
from_=os.environ["TWILIO_WHATSAPP_NUMBER"],
to="whatsapp:+15551234567",
)
print(f"WhatsApp SID: {message.sid}")
print(f"Status: {message.status}")
except TwilioRestException as e:
print(f"Error {e.code}: {e.msg}")
Testing and Debugging
Run the script and check your WhatsApp app for the incoming message from the sandbox number. If you receive error code 63016, your number has not joined the sandbox; send the join code again from your WhatsApp account to the sandbox number. Error code 63007 typically means the recipient number is not registered on WhatsApp; verify the number on a real device before testing. Check the Twilio Console under Messaging Logs to see whether Twilio successfully handed the message to WhatsApp's delivery infrastructure.
Taking It Further
Apply for a WhatsApp Business Account through Twilio to move out of the sandbox and send messages to any WhatsApp user without them joining first. Use Twilio's Content Template Builder to create approved message templates for notifications, alerts, and transactional messages that go to users outside an active conversation window. Add media_url to your messages.create() call to send images, documents, or audio attachments, since WhatsApp supports rich media natively. Build a bidirectional WhatsApp bot by combining outbound message sending with an inbound webhook that handles replies using the same Flask patterns as the SMS webhook tutorial.
Contact Telphi Consulting to set up a WhatsApp Business channel with approved templates and automated conversation handling.
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)
Be the first to comment
No comments yet. Share your thoughts below.