Overview
CreditNexus integrates with Twilio to enable SMS and voice communication for loan recovery workflows. This guide walks you through the complete setup process. Code Reference:app/services/twilio_service.py, app/api/twilio_routes.py
Prerequisites
- Twilio Account: Sign up at https://www.twilio.com/
- Phone Number: Purchase a Twilio phone number with SMS and Voice capabilities
- Public URL: Your application must be accessible via HTTPS for webhooks
Step 1: Get Twilio Credentials
- Log in to Twilio Console: https://console.twilio.com/
- Navigate to Account Dashboard
- Copy your credentials:
- Account SID: Found on the dashboard
- Auth Token: Click “Show” to reveal (keep this secret!)
Step 2: Purchase a Phone Number
- Navigate to Phone Numbers: https://console.twilio.com/us1/develop/phone-numbers/manage/search
- Search for a number with:
- ✅ SMS capabilities
- ✅ Voice capabilities
- Purchase the number
- Copy the phone number (E.164 format, e.g.,
+1234567890)
Step 3: Configure Environment Variables
Add to your.env file:
TWILIO_AUTH_TOKEN to version control!
Step 4: Configure Webhooks
Twilio needs to send status updates to your application. Configure webhook URLs in the Twilio Console:4.1: SMS Status Webhook
- Navigate to: Phone Numbers → Manage → Active Numbers
- Click on your phone number
- Scroll to “Messaging” section
- Set “A MESSAGE COMES IN”: Leave empty (handled by API)
- Set “STATUS CALLBACK URL”:
https://your-domain.com/api/twilio/webhook/sms
4.2: Voice Status Webhook
- In the same phone number settings
- Scroll to “Voice & Fax” section
- Set “A CALL COMES IN”: Leave empty (handled by API)
- Set “STATUS CALLBACK URL”:
https://your-domain.com/api/twilio/webhook/voice
4.3: Generic Status Callback
For programmatic status updates, also set: Status Callback URL:https://your-domain.com/api/twilio/webhook/status
Step 5: Local Development Setup
For local development, use a tunneling service to expose your local server:Option 1: ngrok
https://abc123.ngrok.io) in your webhook URLs:
https://abc123.ngrok.io/api/twilio/webhook/smshttps://abc123.ngrok.io/api/twilio/webhook/voicehttps://abc123.ngrok.io/api/twilio/webhook/status
Option 2: localtunnel
Step 6: Verify Configuration
Test SMS Sending
Use the test script:Test Voice Call
Voice calls are automatically generated when recovery actions are triggered. Test by:- Creating a loan default in the system
- Triggering a recovery action with
action_type: "voice_call" - Verify the call is received
Webhook Endpoints
CreditNexus provides the following webhook endpoints: SMS status callback endpoint. Receives delivery status updates for SMS messages. Voice status callback endpoint. Receives call status updates. Generic status callback endpoint. Handles both SMS and voice status updates. TwiML response generator. Generates TwiML for voice call responses. Security: All webhook endpoints verify Twilio request signatures usingRequestValidator.
Code Reference: app/api/twilio_routes.py
Phone Number Format
Twilio requires phone numbers in E.164 format:- ✅ Correct:
+1234567890,+441234567890 - ❌ Incorrect:
(123) 456-7890,123-456-7890,1234567890
+[country code][number] (no spaces, dashes, or parentheses)
Troubleshooting
Issue: “Invalid phone number format”
Solution: Ensure phone numbers are in E.164 format (+1234567890)
Issue: “Webhook not received”
Solutions:- Verify webhook URL is publicly accessible (use ngrok for local testing)
- Check firewall allows incoming POST requests
- Verify webhook URLs are correctly configured in Twilio Console
- Check application logs for webhook errors
Issue: “SMS not delivered”
Solutions:- Verify phone number has SMS capabilities
- Check Twilio account balance
- Verify recipient phone number is valid
- Check Twilio Console for delivery status
Issue: “Voice call fails”
Solutions:- Verify phone number has Voice capabilities
- Check Twilio account balance
- Verify TwiML response is valid
- Check Twilio Console for call logs
Issue: “Webhook signature verification fails”
Solutions:- Verify
TWILIO_AUTH_TOKENmatches your Twilio account - Check webhook URL matches exactly (including HTTPS)
- Verify request body is not modified
Security Best Practices
- Never commit secrets: Keep
TWILIO_AUTH_TOKENin.envfile only - Use HTTPS: Always use HTTPS for webhook URLs in production
- Verify signatures: Webhook signature verification is automatic
- Rate limiting: Monitor Twilio usage to avoid rate limits
- Phone number validation: Always validate phone numbers before sending
API Usage
Send SMS
Make Voice Call
app/services/twilio_service.py
Rate Limits
Twilio has rate limits per account:- SMS: Varies by country and account type
- Voice: Varies by account type
- Implement retry logic with exponential backoff
- Queue actions if rate limit is hit
- Monitor usage in Twilio Console
Cost Considerations
Twilio charges per SMS and per minute for voice calls:- SMS: ~$0.0075 per message (varies by country)
- Voice: ~$0.013 per minute (varies by country)
- Use SMS for low-priority notifications
- Use voice calls for high-priority, urgent communications
- Monitor usage in Twilio Console
Additional Resources
- Twilio Documentation
- E.164 Phone Number Format
- Twilio API Reference
- Loan Recovery Feature Documentation
Last Updated: 2026-01-14
Code Reference:
app/services/twilio_service.py, app/api/twilio_routes.py