Twilio Webhook Integration
Twilio webhooks enable real-time notifications for SMS messages, voice calls, WhatsApp messages, and other communication events. Configure webhooks to receive updates about message delivery, call status, and user interactions.Quick Start
- Get your Unhook URL:
https://unhook.sh/wh_YOUR_ID - Configure in Twilio Console: console.twilio.com
- Start receiving events locally:
unhook listen
Configuration Steps
1. Access Twilio Console
Navigate to the Twilio Console:- Direct Link: console.twilio.com
- Sign in with your Twilio account
2. Configure Webhooks by Service
Twilio webhooks are configured per service and resource:SMS/MMS Webhooks
- Navigate to Phone Numbers → Manage → Active Numbers
- Click on the phone number you want to configure
- In the Messaging section:
- Webhook URL for incoming messages:
- Status callback URL:
- Webhook URL for incoming messages:
- Select HTTP POST as the method
- Click Save
Voice Call Webhooks
- In the same phone number configuration
- In the Voice & Fax section:
- Webhook URL for incoming calls:
- Status callback URL:
- Webhook URL for incoming calls:
- Select HTTP POST as the method
- Click Save
WhatsApp Webhooks
- Navigate to Messaging → Services
- Select your WhatsApp sender
- Configure webhooks:
- Incoming Message URL:
- Status Callback URL:
- Incoming Message URL:
Programmable Voice (SIP)
- Navigate to Voice → Manage → SIP Domains
- Click on your SIP domain
- Set the Voice URL:
3. Event Types
SMS/MMS Events
Incoming Message Parameters:MessageSid- Unique message identifierFrom- Sender’s phone numberTo- Recipient’s phone numberBody- Message contentNumMedia- Number of media attachmentsMediaUrl{N}- URLs for media attachmentsFromCity,FromState,FromCountry- Sender location
sent- Message sent to carrierdelivered- Message delivered to deviceundelivered- Message failed to deliverfailed- Message could not be sentreceiving- Inbound message receivedreceived- Inbound message processed
Voice Call Events
Incoming Call Parameters:CallSid- Unique call identifierFrom- Caller’s phone numberTo- Called phone numberCallStatus- Current call statusDirection- inbound/outbound-api/outbound-dialCallerName- Caller ID name (if available)
initiated- Call startedringing- Phone is ringinganswered- Call answeredcompleted- Call endedbusy- Busy signalfailed- Call failedno-answer- No answer
WhatsApp Events
Similar to SMS with additional parameters:ProfileName- WhatsApp profile nameWaId- WhatsApp IDButtonText- Text of button pressed (interactive messages)ButtonPayload- Payload of button pressed
Testing Webhooks
Using Twilio Test Credentials
- Find test credentials in Console → Account → API keys & tokens
- Use test phone numbers:
PHONE_NUMBER- Valid number for testingPHONE_NUMBER- Invalid number (triggers error)
Send Test Messages
Request Examples
Incoming SMS
SMS Status Callback
Incoming Voice Call
Webhook Security
Validate Requests with X-Twilio-Signature
Twilio signs all webhook requests. Verify them:IP Allowlisting (Optional)
For additional security, allowlist Twilio’s IP ranges:- North America:
54.172.60.0/23 - Europe:
54.171.127.192/26 - Australia:
54.65.63.192/26 - Full list: twilio.com/docs/sip/ip-addresses
Response Format
TwiML Responses
For voice and messaging webhooks, respond with TwiML:Status Callbacks
For status callbacks, return HTTP 200 OK:Best Practices
1. Handle Retries
Twilio retries failed webhooks with exponential backoff:2. Respond Quickly
Twilio expects a response within 15 seconds:3. Error Handling
Implement proper error handling:4. Use Appropriate Status Codes
200 OK- Successfully processed204 No Content- Acknowledged, no response needed400 Bad Request- Invalid request403 Forbidden- Failed authentication
Common Issues
Webhook URL Not Reachable
- Ensure your Unhook tunnel is running
- Check the URL format is correct
- Verify no typos in the webhook ID
Signature Validation Failing
- Use the exact URL configured in Twilio
- Include query parameters in validation
- Ensure auth token is correct
Missing Parameters
- Different event types have different parameters
- Use optional chaining for safety:
Timeout Errors
- Respond within 15 seconds
- Process heavy operations asynchronously
- Return minimal TwiML if needed
Environment Variables
Useful Links
- Twilio Console: console.twilio.com
- Webhook Documentation: twilio.com/docs/usage/webhooks
- TwiML Reference: twilio.com/docs/voice/twiml
- Security Best Practices: twilio.com/docs/usage/security
- Status Callback Events: twilio.com/docs/sms/api/message-resource#message-status-values
- Test Credentials: twilio.com/docs/iam/test-credentials
Support
Need help with Twilio webhooks?- Join our Discord community
- Visit Twilio Support
- Email us at support@unhook.sh