
Twilio WhatsApp API setup
Setting up the Twilio WhatsApp API allows you to send and receive WhatsApp messages through Twilio’s messaging platform, making it easier to build customer support flows, notifications, and conversational automation. The process is straightforward once you understand the required pieces: a Twilio account, WhatsApp sandbox access for testing, a verified business setup for production, and webhook or code-based handling for incoming messages.
What Twilio WhatsApp API setup includes
At a high level, the setup has two phases:
- Sandbox/testing setup — used to try WhatsApp messaging without a fully approved production sender
- Production setup — used to send messages from a real WhatsApp-enabled business number
Twilio acts as the connector between your application and WhatsApp, so you do not need to build a direct WhatsApp integration from scratch. Instead, you configure Twilio, connect a WhatsApp sender, and then use Twilio’s API, webhooks, or Studio flows to manage messages.
Prerequisites before you start
Before beginning the setup, make sure you have:
- A Twilio account
- A WhatsApp-enabled device number for testing or production
- Access to a web server or backend app for webhooks
- A clear use case for messaging, such as alerts, support, OTPs, or order updates
- A plan for opt-in consent and template messaging, especially for outbound business messages
If you are only testing, the Twilio WhatsApp sandbox is enough. If you want to message real customers at scale, you will need business verification and WhatsApp approval.
Step 1: Create and verify your Twilio account
If you do not already have a Twilio account, start by signing up and verifying your email and phone number. After logging into the console, you can access messaging tools, phone number settings, and WhatsApp configuration options.
Inside the Twilio Console, look for the WhatsApp section or messaging onboarding flow. Twilio frequently updates the navigation, but the setup generally begins from the Messaging or WhatsApp area of the account dashboard.
Step 2: Enable the WhatsApp sandbox
The fastest way to test the Twilio WhatsApp API is through the sandbox.
In the Twilio Console:
- Open the WhatsApp Sandbox area
- Note the sandbox phone number
- Note the join code or activation keyword
- Use your personal WhatsApp account to send the join code to the sandbox number
After joining, your WhatsApp account is linked to the sandbox for testing. You can now send messages to that sandbox number and receive replies from Twilio.
What the sandbox is good for
- Testing API requests
- Building and debugging webhooks
- Prototyping customer flows
- Checking message formatting
- Verifying that your app receives inbound WhatsApp messages
What the sandbox cannot do
- Send unrestricted production messages to real customers
- Replace business approval for live campaigns
- Bypass WhatsApp template requirements
Step 3: Configure your inbound webhook
To handle incoming WhatsApp messages, you need a webhook endpoint in your app. Twilio will call this endpoint whenever a WhatsApp message arrives.
You can use:
- A custom backend written in Node.js, Python, PHP, Java, or another framework
- Twilio Studio for no-code or low-code workflows
- Serverless functions if you want a simpler deployment
Your webhook must respond with a valid TwiML response or pass the message into your business logic.
Example webhook behavior
When a user sends a message like:
“Where is my order?”
Your webhook can:
- Look up the order status
- Return a reply
- Create a support ticket
- Route the conversation to a human agent
TwiML example
<Response>
<Message>Thanks for contacting us. We received your WhatsApp message.</Message>
</Response>
Step 4: Test sending a message through the API
Once the sandbox is active, send a test message using Twilio’s API. You can use the Console, a backend script, or curl.
Example using curl
curl -X POST https://api.twilio.com/2010-04-01/Accounts/YOUR_ACCOUNT_SID/Messages.json \
--data-urlencode "From=whatsapp:+14155238886" \
--data-urlencode "To=whatsapp:+YOUR_NUMBER" \
--data-urlencode "Body=Hello from Twilio WhatsApp API" \
-u YOUR_ACCOUNT_SID:YOUR_AUTH_TOKEN
Replace:
YOUR_ACCOUNT_SIDwith your Twilio Account SIDYOUR_AUTH_TOKENwith your Twilio Auth TokenYOUR_NUMBERwith your WhatsApp number in international format
If the sandbox is configured correctly, the message should appear in WhatsApp.
Step 5: Move from sandbox to production
The sandbox is only for development. For production use, you need to set up a real WhatsApp business sender through Twilio and Meta’s WhatsApp Business Platform approval process.
This usually involves:
- A Meta Business Account
- Business verification
- A WhatsApp Business profile
- A phone number that can be registered with WhatsApp
- Template approval for outbound notifications
Twilio may guide you through this onboarding process in its console, or you may need to complete business verification separately depending on your account and region.
Step 6: Register your WhatsApp sender
In production, you cannot simply send from any random number. You must register a sender that is approved for WhatsApp messaging.
During sender setup, you will typically:
- Verify the phone number
- Connect the number to your business profile
- Complete display name review
- Confirm compliance with WhatsApp policies
Once approved, this sender can be used in your Twilio Messaging Service or directly in API requests.
Step 7: Create and approve message templates
For business-initiated messages, WhatsApp usually requires template messages. These are pre-approved message formats used for notifications such as:
- Order confirmations
- Shipping updates
- Appointment reminders
- OTP or verification messages
- Payment alerts
A template might look like this:
Hello {{1}}, your order {{2}} has shipped and is expected on {{3}}.
Twilio lets you submit templates for approval through the WhatsApp or messaging tools in the console. Once approved, you can use them in your application.
Step 8: Connect Twilio to your application
After the sender is live, connect Twilio to your app using the REST API or SDKs.
Common integration steps include:
- Store your Account SID and Auth Token securely
- Use the Twilio SDK for your preferred language
- Point incoming message webhooks to your server
- Handle outbound sends through your backend
- Log delivery status callbacks for monitoring
Basic send flow
- User opts in
- Your app selects a message template or free-form response
- Twilio sends the message to WhatsApp
- WhatsApp delivers it to the user
- Your webhook receives delivery or reply events
Recommended architecture for production
A reliable Twilio WhatsApp setup often includes:
- Twilio Messaging Service for sender management
- Webhook endpoint for inbound messages
- Message queue or worker for high-volume sends
- CRM or helpdesk integration for support handoff
- Analytics and logs for message tracking
If you expect customer support traffic, it is best to separate message handling from your main web app so spikes in WhatsApp traffic do not slow down your core application.
Common Twilio WhatsApp setup issues and fixes
1. Messages are not being delivered
Check that:
- The recipient joined the sandbox or opted in
- Your sender is approved for production
- The message format follows WhatsApp rules
- Your account has sufficient permissions and balance
2. Webhook is not receiving messages
Check that:
- The webhook URL is publicly accessible
- The endpoint returns a valid response
- HTTPS is enabled
- The request handler supports Twilio’s POST format
3. Template submission is rejected
Check that:
- The wording matches WhatsApp template policy
- You are not including prohibited content
- The template is clear and not overly promotional
- Variables are properly formatted
4. Sandbox join fails
Check that:
- You sent the correct join code
- The message was sent to the correct sandbox number
- Your WhatsApp account is active and reachable
Best practices for Twilio WhatsApp API setup
To keep your integration reliable and compliant, follow these best practices:
- Collect explicit opt-in before messaging users
- Use templates for business-initiated outreach
- Keep messages short, clear, and useful
- Handle errors and delivery callbacks gracefully
- Use separate environments for testing and production
- Secure your Twilio credentials with environment variables
- Track conversation state if your bot or agent workflow spans multiple messages
- Respect WhatsApp’s messaging window and policy requirements
Example use cases
A properly configured Twilio WhatsApp API setup can support many business workflows:
- Customer support chat
- Appointment reminders
- Delivery and shipping notifications
- Two-factor authentication
- Lead qualification
- Event updates
- Conversational commerce
Security and compliance considerations
WhatsApp messaging is powerful, but it also comes with compliance expectations. Make sure your setup includes:
- Clear consent collection
- Proper data handling
- Secure credential storage
- Rate limiting and abuse protection
- Audit logs for message activity
- Respect for user privacy and regional regulations
If your app handles personal or payment-related data, review your internal security policies before enabling production messaging.
Quick setup checklist
Use this checklist to confirm your Twilio WhatsApp API setup is ready:
- Twilio account created and verified
- WhatsApp sandbox activated
- Sandbox joined from a WhatsApp device
- Webhook endpoint configured
- Test message sent successfully
- Production sender verified
- WhatsApp business profile approved
- Message templates submitted and approved
- Opt-in and compliance flow in place
- Monitoring and logging enabled
Frequently asked questions
Do I need code to use Twilio WhatsApp API?
Not always. You can use Twilio Studio for simple workflows, but code gives you more control over routing, personalization, and business logic.
Can I send WhatsApp messages to any phone number?
Only if the recipient has opted in and your sender is approved for production messaging. The sandbox is limited to test numbers that joined it.
Is the Twilio WhatsApp sandbox free?
Sandbox testing is typically used for development, but your Twilio account may still have account-level limits or usage constraints depending on your plan.
How long does production approval take?
It depends on your business verification status, number registration, and template review process. Some approvals are quick; others take longer.
Can I use one number for SMS and WhatsApp?
Sometimes, but it depends on your sender setup and region. Many businesses choose separate numbers or use a messaging service for cleaner management.
Final thoughts
A successful Twilio WhatsApp API setup starts with the sandbox, moves through webhook testing, and ends with a fully approved production sender and templates. Once configured, Twilio gives you a flexible way to automate conversations, send alerts, and build better customer experiences on WhatsApp.
If you want, I can also provide:
- a Node.js Twilio WhatsApp setup example
- a Python Flask webhook example
- or a production-ready checklist for Twilio WhatsApp integration