📦 Production deployment with advanced configuration - Complete guide with security, monitoring, and scaling best practices.
For a quick 5-minute trial, see quick-start.md
- Prerequisites
- Environment Setup
- Twilio Serverless Deployment
- Segment CDP Configuration
- Running the Generator
- Monitoring and Validation
- Troubleshooting
- Node.js: v18 or higher (download)
- Twilio CLI: Latest version
- Twilio Account: Active account with credits
- Segment Account: Workspace with write access
npm install -g twilio-clitwilio plugins:install @twilio-labs/plugin-serverlesstwilio --version
twilio pluginsgit clone <repository-url>
cd twilio-synthetic-call-data-generatornpm installCreate a .env file in the project root:
cp .env.example .envEdit .env with your credentials:
# Twilio Credentials (from console.twilio.com)
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
# Segment CDP (from app.segment.com)
SEGMENT_WRITE_KEY=your_segment_write_key_here
# Optional: Agent Phone Number for Testing
AGENT_PHONE_NUMBER=+15551234567- Go to console.twilio.com
- Copy Account SID and Auth Token from the dashboard
- Purchase a phone number if you don't have one: Phone Numbers → Buy a Number
See segment-setup-guide.md for detailed instructions.
The system uses Twilio Functions for webhooks and call control.
twilio loginThis opens a browser for authentication.
twilio serverless:deployThis deploys:
conference-status-webhook.js- Tracks conference lifecycle eventsconference-timer.js- Manages conference durationtranscription-webhook.js- Processes call transcriptionserror-handler.js- Real-time error monitoring and remediation
After deployment, you'll see URLs like:
https://your-service-xxxx.twil.io/conference-status-webhook
https://your-service-xxxx.twil.io/conference-timer
https://your-service-xxxx.twil.io/transcription-webhook
https://your-service-xxxx.twil.io/error-handler
Save these URLs - you'll need them for webhook configuration.
Set up real-time error monitoring with Twilio Debugger webhook:
npm run configure-debuggerThis will display instructions for manual configuration. Follow these steps:
- Go to console.twilio.com/monitor/debugger
- Click Settings (gear icon in top right)
- Under Webhook, enter:
https://your-service-xxxx.twil.io/error-handler - Click Save
What this does:
- Captures ALL Twilio service errors/warnings automatically
- Real-time notifications via webhook POST
- Automated error severity classification
- Structured JSON error logging
- Automated remediation actions based on error codes
Test the error handler:
Create an intentional error to verify the webhook is working:
# Try calling an invalid phone number via Twilio API
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Calls.json" \
-u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN" \
--data-urlencode "To=+15551234567" \
--data-urlencode "From=$AGENT_PHONE_NUMBER" \
--data-urlencode "Url=http://demo.twilio.com/docs/voice.xml"Then check:
- Twilio Debugger shows the error
- Function logs show error-handler webhook execution
- Error is classified and logged correctly
For more details, see error-handling-guide.md.
Update your deployed functions with environment variables:
twilio serverless:env:set \
SEGMENT_WRITE_KEY=your_segment_write_key_here- Go to app.segment.com
- Navigate to Connections → Sources
- Click Add Source
- Choose Node.js source
- Name it "Twilio Call Data Generator"
- Copy the Write Key
Add destinations to receive the synthetic data:
Recommended Destinations:
- Segment Debugger (built-in) - Real-time event inspection
- Google Sheets - Data export and analysis
- Webhook - Custom integrations
- Twilio Engage - Customer engagement campaigns
See segment-setup-guide.md for detailed setup.
Run the smoke test to ensure Segment is receiving data:
npm run smoke-testCheck the Segment Debugger for events:
identifycalls with customer profilescall_completedtrack events with analytics
Run the smoke test to ensure everything is connected:
npm run smoke-testExpected output:
✓ Connected to Twilio account
✓ Segment identify() call succeeded
✓ Created Segment profile
✓ Updated Segment profile with call analytics
✓ ALL TESTS PASSED! (8/8)
node scripts/validate-customers.js
node scripts/validate-agents.jsOption A: Single Conference
node src/main.jsOption B: Batch Generation
# Generate 10 conferences
for i in {1..10}; do node src/main.js; sleep 5; doneOption C: Scheduled Generation
Use a cron job to generate calls periodically:
# Run every hour
0 * * * * cd /path/to/project && node src/main.jsWatch the console output for:
- Customer-agent pairing
- Conference creation
- Participant addition
- Webhook events
- Segment profile updates
Conference Logs:
- Go to console.twilio.com
- Navigate to Monitor → Logs → Conferences
- View active and completed conferences
Call Logs:
- Navigate to Monitor → Logs → Calls
- Filter by conference SID
- Check participant call statuses
Function Logs:
- Navigate to Functions & Assets → Services
- Click your service
- View function execution logs
Debugger:
- Go to Connections → Sources → Twilio Call Data Generator
- Click Debugger tab
- View real-time events
Event Delivery:
- Check Event Delivery tab
- Verify successful delivery to destinations
- Investigate any errors
Run E2E tests to validate the complete pipeline:
npm run test:e2eCheck test results:
- Conference creation
- Profile updates
- ML score calculations
- Event tracking
Solution: Ensure you're using the correct import syntax:
const { Analytics } = require('@segment/analytics-node');Not:
const Analytics = require('@segment/analytics-node');Solution:
- Verify credentials in
.env - Re-authenticate Twilio CLI:
twilio login - Check account status at console.twilio.com
Solution:
- Verify
SEGMENT_WRITE_KEYin.env - Check Segment Debugger for errors
- Ensure source is enabled
- Run:
npm run smoke-testto test connection
Solution:
- Check Twilio CLI version:
twilio --version - Update plugin:
twilio plugins:update - Verify function syntax in
functions/directory - Check deployment logs for specific errors
Solution:
- Verify phone number is provisioned and active
- Check account credits at console.twilio.com
- Ensure webhook URLs are accessible
- Review function logs for errors
- Check Debugger webhook for error notifications
Solution:
- Verify Debugger webhook is configured at console.twilio.com/monitor/debugger
- Check webhook URL matches deployed function:
https://your-service-xxxx.twil.io/error-handler - Test with intentional error (see deployment section)
- Verify function is deployed:
twilio serverless:list - Check function logs for webhook execution
Solution:
- Review error code in Twilio Debugger
- Check error-handler.js classification logic
- Verify structured error logging in function logs
- See error-handling-guide.md for error code reference
Solution:
rm -rf node_modules package-lock.json
npm installSolution:
- Clear Jest cache:
npm run test:clear - Run tests individually to isolate issue
- Check for environment variable conflicts
- Ensure all mocks are properly configured
- Never commit
.envfile - Add to.gitignore - Rotate credentials regularly
- Use environment-specific keys (dev/staging/prod)
- Enable two-factor authentication on Twilio and Segment
- Monitor Twilio usage at console.twilio.com/usage
- Set usage alerts to avoid unexpected charges
- Use short conference durations for testing
- Clean up old conferences regularly
- Validate customer/agent data before generation
- Monitor Segment event delivery rates
- Review ML scores for realism
- Archive historical data for analysis
- Batch conference creation with delays to avoid rate limits
- Use Twilio's bulk API for large-scale generation
- Implement retry logic for transient failures
- Monitor API rate limits
- Anonymize PII in logs and monitoring
- Follow TCPA regulations for phone calls
- Review data retention policies
- Document data lineage for synthetic data
- Review segment-setup-guide.md for Segment configuration
- Check API documentation for integration details
- Run smoke test to validate deployment
- Generate test conferences to verify end-to-end flow
- Monitor Segment Debugger for event delivery
- Twilio Support: https://support.twilio.com
- Segment Support: https://segment.com/help
- GitHub Issues: Create an issue
- Documentation: See
docs/directory
Deployment Checklist:
- Node.js v18+ installed
- Twilio CLI installed and authenticated
- Dependencies installed (
npm install) -
.envfile configured - Twilio credentials verified
- Segment write key added
- Serverless functions deployed
- Error handler webhook configured
- Debugger webhook tested
- Smoke test passed
- Customer/agent data validated
- Test conference created successfully
- Segment events visible in Debugger
- Monitoring configured
- Production best practices reviewed
You're ready to generate synthetic call data! 🎉