Twilio API - Comprehensive Communication Platform
When to Use This Skill
Use this skill when working with Twilio's communication APIs for:
- SMS/MMS Messaging - Send and receive text messages programmatically
- Voice Communication - Build voice calling applications with TwiML
- Phone Number Management - Search, purchase, and configure phone numbers
- Webhook Integration - Handle real-time events and delivery notifications with TwiML responses
- Two-Way SMS Conversations - Build interactive SMS experiences
- Bulk SMS Sending - Send messages to multiple recipients with rate limiting
- Message Scheduling - Schedule messages for future delivery
- Production Deployment - Deploy messaging features with error handling and monitoring
- A2P 10DLC Registration - Register brands and campaigns for US A2P messaging compliance
- Provider-Agnostic Architecture - Build systems that support multiple SMS providers (Twilio + Telnyx)
This skill applies to building communication features in applications, setting up SMS notification systems, creating voice IVR systems, or integrating telephony capabilities.
Quick Reference
1. Send Simple SMS (Node.js SDK)
const twilio = require('twilio');
const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
async function sendSMS(to, from, body) {
const message = await client.messages.create({
to: to,
from: from,
body: body
});
return message;
}
// Usage
await sendSMS('+14155552671', '+14155559999', 'Hello from Twilio!');
2. Send SMS with HTTP (No SDK)
const https = require('https');
function sendSMS(to, from, body) {
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const auth = Buffer.from(`${accountSid}:${authToken}`).toString('base64');
const postData = new URLSearchParams({
To: to,
From: from,
Body: body
}).toString();
const options = {
hostname: 'api.twilio.com',
port: 443,
path: `/2010-04-01/Accounts/${accountSid}/Messages.json`,
method: 'POST',
headers: {
'Authorization': `Basic ${auth}`,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => resolve(JSON.parse(data)));
});
req.on('error', reject);
req.write(postData);
req.end();
});
}
3. Validate Phone Numbers (E.164 Format)
function validateE164(phoneNumber) {
const e164Regex = /^\+[1-9]\d{1,14}$/;
if (!e164Regex.test(phoneNumber)) {
return {
valid: false,
error: 'Phone number must be in E.164 format (e.g., +14155552671)'
};
}
return { valid: true };
}
// Normalize US phone numbers to E.164
function formatToE164(number) {
let digits = number.replace(/\D/g, '');
if (!digits.startsWith('1')) {
digits = '1' + digits;
}
return '+' + digits;
}
4. Handle Incoming Messages (Webhook with TwiML)
const express = require('express');
app.use(express.urlencoded({ extended: false }));
app.post('/webhooks/twilio', (req, res) => {
const from = req.body.From;
const body = req.body.Body;
const to = req.body.To;
console.log(`Received: "${body}" from ${from}`);
// Respond with TwiML
const twiml = `<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Message>Thanks for your message!</Message>
</Response>`;
res.set('Content-Type', 'text/xml');
res.send(twiml);
});
5. Verify Webhook Signatures (HMAC-SHA1)
const crypto = require('crypto');
function verifyTwilioSignature(url, params, signature, authToken) {
// Build data string from sorted params
const data = Object.keys(params)
.sort()
.reduce((acc, key) => acc + key + params[key], url);
// Generate HMAC-SHA1 signature
const expectedSignature = crypto
.createHmac('sha1', authToken)
.update(Buffer.from(data, 'utf-8'))
.digest('base64');
return signature === expectedSignature;
}
// Usage in Express with body-parser
app.post('/webhooks/twilio', (req, res) => {
const signature = req.headers['x-twilio-signature'];
const url = `https://${req.headers.host}${req.url}`;
if (!verifyTwilioSignature(url, req.body, signature, process.env.TWILIO_AUTH_TOKEN)) {
return res.status(403).send('Forbidden');
}
// Process webhook...
const twiml = '<Response></Response>';
res.set('Content-Type', 'text/xml');
res.send(twiml);
});
6. Twilio SDK Signature Validation
const twilio = require('twilio');
app.post('/webhooks/twilio', (req, res) => {
const signature = req.headers['x-twilio-signature'];
const url = `https://${req.headers.host}${req.url}`;
if (!twilio.validateRequest(
process.env.TWILIO_AUTH_TOKEN,
signature,
url,
req.body
)) {
return res.status(403).send('Forbidden');
}
// Process webhook...
const twiml = new twilio.twiml.MessagingResponse();
twiml.message('Thanks for your message!');
res.set('Content-Type', 'text/xml');
res.send(twiml.toString());
});
7. Send with Error Handling and Retry
async function sendWithRetry(to, from, body, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await client.messages.create({ to, from, body });
} catch (error) {
if (error.status >= 500 && attempt < maxRetries) {
// Server error - retry with exponential backoff
const delayMs = Math.pow(2, attempt) * 1000;
console.log(`Retry ${attempt} in ${delayMs}ms...`);
await new Promise(resolve => setTimeout(resolve, delayMs));
} else {
throw error;
}
}
}
}
8. Bulk Sending with Rate Limiting
async function sendBulkSMS(recipients, from, body) {
const delayMs = 100; // 10 messages/second
const results = [];
for (const recipient of recipients) {
try {
const result = await client.messages.create({ to: recipient, from, body });
results.push({ success: true, to: recipient, sid: result.sid });
} catch (error) {
results.push({ success: false, to: recipient, error: error.message });
}
await new Promise(resolve => setTimeout(resolve, delayMs));
}
return results;
}
9. Provider-Agnostic Webhook Handler (Twilio + Telnyx)
// From Twilio-Aldea production codebase
function detectProvider(payload: any): 'twilio' | 'telnyx' {
// Telnyx uses JSON with data.event_type
if (payload.data && payload.data.event_type) {
return 'telnyx';
}
// Twilio uses form-urlencoded with MessageSid
if (payload.MessageSid || payload.From) {
return 'twilio';
}
throw new Error('Unknown SMS provider');
}
// Unified webhook handler
app.post('/api/sms/webhook', async (req, res) => {
const providerType = detectProvider(req.body);
if (providerType === 'twilio') {
// Validate Twilio signature
// Return TwiML response
const twiml = '<?xml version="1.0"?><Response></Response>';
res.set('Content-Type', 'text/xml');
res.send(twiml);
} else {
// Validate Telnyx Ed25519 signature
// Return JSON response
res.status(200).json({ status: 'ok' });
}
});
10. Handle Common Errors
function handleTwilioError(error) {
if (!error.status) {
return { type: 'NETWORK_ERROR', retriable: true };
}
switch (error.status) {
case 400:
case 422:
// Validation error
return {
type: 'VALIDATION_ERROR',
message: error.message,
code: error.code,
retriable: false
};
case 401:
// Check Account SID and Auth Token
return { type: 'AUTH_ERROR', retriable: false };
case 429:
// Rate limit
retur