Skip to main content

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

  1. Get your Unhook URL: https://unhook.sh/wh_YOUR_ID
  2. Configure in Twilio Console: console.twilio.com
  3. Start receiving events locally: unhook listen

Configuration Steps

1. Access Twilio Console

Navigate to the Twilio Console:

2. Configure Webhooks by Service

Twilio webhooks are configured per service and resource:

SMS/MMS Webhooks

  1. Navigate to Phone Numbers → Manage → Active Numbers
  2. Click on the phone number you want to configure
  3. In the Messaging section:
    • Webhook URL for incoming messages:
    • Status callback URL:
  4. Select HTTP POST as the method
  5. Click Save

Voice Call Webhooks

  1. In the same phone number configuration
  2. In the Voice & Fax section:
    • Webhook URL for incoming calls:
    • Status callback URL:
  3. Select HTTP POST as the method
  4. Click Save

WhatsApp Webhooks

  1. Navigate to Messaging → Services
  2. Select your WhatsApp sender
  3. Configure webhooks:
    • Incoming Message URL:
    • Status Callback URL:

Programmable Voice (SIP)

  1. Navigate to Voice → Manage → SIP Domains
  2. Click on your SIP domain
  3. Set the Voice URL:

3. Event Types

SMS/MMS Events

Incoming Message Parameters:
  • MessageSid - Unique message identifier
  • From - Sender’s phone number
  • To - Recipient’s phone number
  • Body - Message content
  • NumMedia - Number of media attachments
  • MediaUrl{N} - URLs for media attachments
  • FromCity, FromState, FromCountry - Sender location
Status Callback Events:
  • sent - Message sent to carrier
  • delivered - Message delivered to device
  • undelivered - Message failed to deliver
  • failed - Message could not be sent
  • receiving - Inbound message received
  • received - Inbound message processed

Voice Call Events

Incoming Call Parameters:
  • CallSid - Unique call identifier
  • From - Caller’s phone number
  • To - Called phone number
  • CallStatus - Current call status
  • Direction - inbound/outbound-api/outbound-dial
  • CallerName - Caller ID name (if available)
Call Status Events:
  • initiated - Call started
  • ringing - Phone is ringing
  • answered - Call answered
  • completed - Call ended
  • busy - Busy signal
  • failed - Call failed
  • no-answer - No answer

WhatsApp Events

Similar to SMS with additional parameters:
  • ProfileName - WhatsApp profile name
  • WaId - WhatsApp ID
  • ButtonText - Text of button pressed (interactive messages)
  • ButtonPayload - Payload of button pressed

Testing Webhooks

Using Twilio Test Credentials

  1. Find test credentials in Console → Account → API keys & tokens
  2. Use test phone numbers:
    • PHONE_NUMBER - Valid number for testing
    • PHONE_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:

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 processed
  • 204 No Content - Acknowledged, no response needed
  • 400 Bad Request - Invalid request
  • 403 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

Support

Need help with Twilio webhooks?