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
- ✅ 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
Step 3: Create Database Table
Run the SQL in Supabase:Step 4: Deploy the Endpoint
The endpoint is already created at/app/api/twilio/events/route.ts.
Deploy to production:
Step 5: Run Setup Script
- Create a webhook sink pointing to your endpoint
- Subscribe to messaging error events
- Subscribe to voice error events
- Display the Sink SID for reference
Step 6: Test It
Trigger a test error: Send an SMS to an invalid number or trigger any Twilio error. Check if it worked: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: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:-
Start ngrok:
-
Update the script:
Edit
scripts/setup-twilio-event-streams.sh: -
Run setup:
-
Trigger an error and watch logs:
Troubleshooting
Events not arriving
-
Check sink status:
-
Check subscriptions:
-
Test the endpoint:
-
Check Vercel logs:
Go to Vercel Dashboard → Logs → Filter by
/api/twilio/events
Signature validation failing
- Ensure
TWILIO_AUTH_TOKENis 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_logstable 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
- ✅ Create database table
- ✅ Deploy endpoint to production
- ✅ Run setup script
- ✅ Test with a triggered error
- ⏭️ Add alerting (optional)
- ⏭️ Create dashboard for error visualization (optional)
- ⏭️ Set up automated reports (optional)
Resources
Status: Ready to deploy and configure