Skip to main content

Twilio Event Streams Setup Guide

Overview

Twilio Event Streams provides real-time error and warning notifications for all Twilio services. This replaces the legacy debugger webhook with a more scalable and reliable approach.

What We’re Monitoring

Based on your error analysis (twilio-error-analysis.txt), you have:
  • 666 failed messages
  • 666 undelivered messages
  • 52.6% delivery rate (should be 95%+)
  • 339 “unknown” errors with no details
Event Streams will help you:
  • ✅ Get details on those “unknown” errors
  • ✅ Catch errors in real-time
  • ✅ Monitor A2P 10DLC compliance
  • ✅ Track rate limits and API issues
  • ✅ Improve your delivery rate

Architecture

Files Created

1. /app/api/twilio/events/route.ts

Webhook endpoint for Event Streams
  • POST: Receives error events from Twilio
    • Validates Twilio signature
    • Parses CloudEvents format
    • Logs to database
    • Alerts on critical errors
  • GET: View recent error logs
    • Returns last 50 errors
    • Groups by type and level
    • Useful for monitoring

2. /sql-queries/create-twilio-error-logs-table.sql

Database schema for error logs

3. /scripts/setup-twilio-event-streams.sh

Automated setup script Creates sink and subscriptions via Twilio CLI.

Setup Instructions

Step 1: Install Twilio CLI (if not already installed)

Step 2: Login to Twilio

Enter your Account SID and Auth Token when prompted.

Step 3: Create Database Table

Run the SQL in Supabase:
Or use psql:

Step 4: Deploy the Endpoint

The endpoint is already created at /app/api/twilio/events/route.ts. Deploy to production:
Wait for Vercel to deploy.

Step 5: Run Setup Script

This will:
  1. Create a webhook sink pointing to your endpoint
  2. Subscribe to messaging error events
  3. Subscribe to voice error events
  4. Display the Sink SID for reference
Output:

Step 6: Test It

Trigger a test error: Send an SMS to an invalid number or trigger any Twilio error. Check if it worked:
Check database:

Event Types

Messaging Errors

Type: com.twilio.messaging.error-log Common errors you’ll see:
  • Error 30034: Exceeded max price
  • Error 30007: Carrier violation
  • Error 21610: User opted out (STOP)
  • Error 21617: Queue overflow
  • Error 21408: Permission denied
  • Error 12300: Invalid number format

Voice Errors

Type: com.twilio.voice.error-log Common errors:
  • Call failures
  • Invalid phone numbers
  • TwiML errors
  • Connection issues

CloudEvents Format

Events arrive in CloudEvents format:

Monitoring & Alerting

View Recent Errors

Via API:
Via Database:

Add Alerting (Optional)

Edit /app/api/twilio/events/route.ts and add alerting:

Managing Event Streams

List All Sinks

List All Subscriptions

Delete a Sink

Delete a Subscription

Update Sink URL

If you need to change the webhook URL:

Testing with ngrok

For local testing:
  1. Start ngrok:
  2. Update the script: Edit scripts/setup-twilio-event-streams.sh:
  3. Run setup:
  4. Trigger an error and watch logs:

Troubleshooting

Events not arriving

  1. Check sink status:
  2. Check subscriptions:
  3. Test the endpoint:
  4. Check Vercel logs: Go to Vercel Dashboard → Logs → Filter by /api/twilio/events

Signature validation failing

  • Ensure TWILIO_AUTH_TOKEN is set in environment variables
  • Check that the URL in Twilio matches exactly (including https://)
  • Verify no proxy or CDN is modifying the request

Database errors

  • Ensure twilio_error_logs table exists
  • Check Supabase service role key is set
  • Verify database connection

Benefits

Before Event Streams:

  • ❌ No visibility into account-level errors
  • ❌ Manual checking of Twilio Console
  • ❌ 339 “unknown” errors with no details
  • ❌ Reactive debugging after users complain

After Event Streams:

  • ✅ Real-time error notifications
  • ✅ Automatic logging to database
  • ✅ Detailed error information
  • ✅ Proactive monitoring and alerting
  • ✅ Historical error tracking
  • ✅ Ability to analyze trends

Cost

Event Streams is free for error logs! No additional charges beyond your normal Twilio usage.

Next Steps

  1. ✅ Create database table
  2. ✅ Deploy endpoint to production
  3. ✅ Run setup script
  4. ✅ Test with a triggered error
  5. ⏭️ Add alerting (optional)
  6. ⏭️ Create dashboard for error visualization (optional)
  7. ⏭️ Set up automated reports (optional)

Resources


Status: Ready to deploy and configure