← Twilio Developer KitCONTENT HISTORY

Update to Twilio Developer Kit

Snapshot Sep 30, 2026 · 22:50 UTC · version 0.2.2

Collection source: not recorded for this historical snapshot.

WHAT CHANGED · RULE-BASED ANALYSIS

First saved snapshot

No earlier snapshot is available to establish a change.

Compare saved observations

Download comparison JSON
Full technical diff · 0 changed fields
Full snapshot data
{
  "name": "twilio-webhook-architecture",
  "description": "Design, secure, and operate Twilio webhook endpoints. Covers inbound event handling, status callbacks, signature validation, connection overrides for retry and timeout tuning, local development tunneling, and production hardening. Use this skill whenever an agent needs to receive HTTP callbacks from Twilio for any product -- messaging, voice, verify, or event streams.",
  "included_files": [
    {
      "relative_path": "agents/openai.yaml",
      "size_in_bytes": 235
    }
  ],
  "skill_md_contents": "---\nname: twilio-webhook-architecture\ndescription: >\n  Design, secure, and operate Twilio webhook endpoints. Covers inbound event\n  handling, status callbacks, signature validation, connection overrides for\n  retry and timeout tuning, local development tunneling, and production\n  hardening. Use this skill whenever an agent needs to receive HTTP callbacks\n  from Twilio for any product -- messaging, voice, verify, or event streams.\n---\n\n## Overview\n\nTwilio delivers events to your application via HTTP callbacks (webhooks). Inbound messages and calls trigger webhooks that expect a TwiML response; status callbacks and event streams push delivery and lifecycle data asynchronously. This skill covers the cross-product patterns that apply to every webhook integration.\n\n---\n\n## Prerequisites\n\n- Twilio account with a phone number or service configured with a webhook URL\n  -- New to Twilio? See `twilio-account-setup`\n- `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` -- see `twilio-iam-auth-setup`\n- SDK: `pip install twilio flask` / `npm install twilio express`\n- Publicly accessible HTTPS endpoint (see Local Development section below)\n\n---\n\n## Quickstart\n\nReceive an inbound SMS and validate the request signature before replying.\n\n**Python (Flask)**\n```python\nimport os\nfrom flask import Flask, request, abort\nfrom twilio.request_validator import RequestValidator\nfrom twilio.twiml.messaging_response import MessagingResponse\n\napp = Flask(__name__)\nvalidator = RequestValidator(os.environ[\"TWILIO_AUTH_TOKEN\"])\n\n@app.route(\"/sms\", methods=[\"POST\"])\ndef incoming_sms():\n    sig = request.headers.get(\"X-Twilio-Signature\", \"\")\n    if not validator.validate(request.url, request.form, sig):\n        abort(403)\n    resp = MessagingResponse()\n    resp.message(f\"Got: {request.form.get('Body')}\")\n    return str(resp), 200, {\"Content-Type\": \"text/xml\"}\n```\n\n**Node.js (Express)**\n```node\nconst express = require(\"express\");\nconst twilio = require(\"twilio\");\nconst app = express();\napp.use(express.urlencoded({ extended: false }));\n\napp.post(\"/sms\", (req, res) => {\n    const valid = twilio.validateRequest(\n        process.env.TWILIO_AUTH_TOKEN,\n        req.headers[\"x-twilio-signature\"],\n        `https://${req.headers.host}${req.originalUrl}`,\n        req.body\n    );\n    if (!valid) return res.status(403).send(\"Forbidden\");\n    const twiml = new twilio.twiml.MessagingResponse();\n    twiml.message(`Got: ${req.body.Body}`);\n    res.type(\"text/xml\").send(twiml.toString());\n});\n```\n\nSet your webhook URL in Console: **Phone Numbers > Active Numbers > (your number) > Messaging > \"A Message Comes In\"**.\n\n---\n\n## Key Patterns\n\n### 1. Webhook Types Across Products\n\n| Webhook type | Trigger | Expected response | Products |\n|---|---|---|---|\n| Inbound event | Message received / call answered | TwiML (XML) | Messaging, Voice |\n| Status callback | Resource state change | `200` or `204` (no body required) | Messaging, Voice, Verify, Video |\n| Action URL | TwiML verb completes (`<Gather>`, `<Record>`) | Next TwiML | Voice |\n| Recording status | Recording processing completes | `200` or `204` | Voice |\n| Debugger event | Error or warning on account | `200` or `204` | All |\n| Event Streams | Any subscribed event | `200` or `204` | All (via Sink) |\n\n### 2. Signature Validation\n\nTwilio signs every webhook with an `X-Twilio-Signature` header (HMAC-SHA1 using your Auth Token). Always validate before processing.\n\n**Form-encoded requests (`application/x-www-form-urlencoded`):**\n\nPass the full URL and POST body parameters to the validator.\n\n**Python**\n```python\nfrom twilio.request_validator import RequestValidator\n\nvalidator = RequestValidator(os.environ[\"TWILIO_AUTH_TOKEN\"])\nis_valid = validator.validate(request.url, request.form, request.headers.get(\"X-Twilio-Signature\", \"\"))\n```\n\n**Node.js**\n```node\nconst { validateRequest } = require(\"twilio\");\n\nconst isValid = validateRequest(\n    process.env.TWILIO_AUTH_TOKEN,\n    req.headers[\"x-twilio-signature\"],\n    `https://${req.headers.host}${req.originalUrl}`,\n    req.body\n);\n```\n\n**JSON requests (`application/json`):**\n\nTwilio appends a `bodySHA256` query parameter to your URL. Use the SDK's JSON-specific validation.\n\n**Python**\n```python\nfrom twilio.request_validator import RequestValidator\n\nvalidator = RequestValidator(os.environ[\"TWILIO_AUTH_TOKEN\"])\nis_valid = validator.validate_body(\n    request.url,\n    request.get_data(as_text=True),\n    request.headers.get(\"X-Twilio-Signature\", \"\")\n)\n```\n\n**Node.js**\n```node\nconst twilio = require(\"twilio\");\n\n// Use express.raw() or a verify callback to preserve the raw body\nconst isValid = twilio.validateRequestWithBody(\n    process.env.TWILIO_AUTH_TOKEN,\n    req.headers[\"x-twilio-signature\"],\n    `https://${req.headers.host}${req.originalUrl}`,\n    req.rawBody  // must be the exact bytes Twilio sent, not JSON.stringify(req.body)\n);\n```\n\n**Critical:** Use the SDK validator. Do not implement your own -- Twilio may add parameters without notice, and the exact algorithm (including port handling) has edge cases the SDK handles.\n\n### 3. Status Callback Handling\n\nStatus callbacks are asynchronous POST requests Twilio sends when a resource changes state. They do not expect TwiML -- return `200` or `204`.\n\n**Messaging status flow:** `queued` -> `sent` -> `delivered` (or `undelivered` / `failed`)\n\nWhen using Messaging Services, the flow starts with `accepted` -> `queued` -> ...\n\n**Voice status events:** `initiated`, `ringing`, `answered`, `completed`\n\nSubscribe to specific events via `StatusCallbackEvent` parameter.\n\nStatus callbacks are signed with `X-Twilio-Signature` like all Twilio webhooks. Validate before acting on the payload -- an unvalidated endpoint lets anyone forge delivery status and drive downstream logic.\n\n**Python (Flask) -- messaging status handler**\n```python\n@app.route(\"/status\", methods=[\"POST\"])\ndef message_status():\n    sig = request.headers.get(\"X-Twilio-Signature\", \"\")\n    if not validator.validate(request.url, request.form, sig):\n        return \"Forbidden\", 403\n    sid = request.form.get(\"MessageSid\")\n    status = request.form.get(\"MessageStatus\")\n    error_code = request.form.get(\"ErrorCode\")\n    if status in (\"failed\", \"undelivered\") and error_code:\n        print(f\"Delivery failed {sid}: error {error_code}\")\n    return \"\", 204\n```\n\n**Node.js (Express) -- voice status handler**\n```node\napp.post(\"/call-status\", (req, res) => {\n    const valid = twilio.validateRequest(\n        process.env.TWILIO_AUTH_TOKEN,\n        req.headers[\"x-twilio-signature\"],\n        `https://${req.headers.host}${req.originalUrl}`,\n        req.body\n    );\n    if (!valid) return res.status(403).send(\"Forbidden\");\n    const { CallSid, CallStatus, Duration } = req.body;\n    console.log(`${CallSid}: ${CallStatus} (${Duration}s)`);\n    res.sendStatus(204);\n});\n```\n\n**Attach status callbacks when creating resources:**\n\n```python\n# Messaging\nmessage = client.messages.create(\n    to=\"+15558675310\", from_=\"+15017122661\", body=\"Hello!\",\n    status_callback=\"https://yourapp.com/status\"\n)\n\n# Voice\ncall = client.calls.create(\n    to=\"+15558675310\", from_=\"+15017122661\",\n    url=\"https://yourapp.com/voice\",\n    status_callback=\"https://yourapp.com/call-status\",\n    status_callback_event=[\"initiated\", \"ringing\", \"answered\", \"completed\"],\n    status_callback_method=\"POST\"\n)\n```\n\n### 4. Connection Overrides (Retry and Timeout Tuning)\n\nAppend URL fragments to any webhook URL to override default connection behavior. Fragments are not included in signature computation.\n\n**Format:** `https://yourapp.com/webhook#key=value&key=value`\n\n| Parameter | Key | Default | Range | Description |\n|---|---|---|---|---|\n| Connect Timeout | `ct` | 5000ms | 100-10000 | TCP connection timeout |\n| Read Timeout | `rt` | 15000ms | 100-15000 | Time to wait for first response byte |\n| Total Time | `tt` | 15000ms | 100-15000 | Total time for all retries |\n| Retry Count | `rc` | 1 | 0-5 | Number of retry attempts |\n| Retry Policy | `rp` | `ct` | `4xx`, `5xx`, `ct`, `rt`, `all` | What triggers a retry |\n| Edge Location | `e` | `ashburn` | `ashburn`, `dublin`, `frankfurt`, `sao-paulo`, `singapore`, `sydney`, `tokyo`, `umatilla` | Egress edge |\n\n**Examples:**\n\n```text\n# Retry up to 3 times on connection or read timeout\nhttps://yourapp.com/sms#rc=3&rp=ct,rt\n\n# Fast failover: 1s connect timeout, 2 retries\nhttps://yourapp.com/voice#ct=1000&rc=2\n\n# Rotate edge locations on retry\nhttps://yourapp.com/status#e=ashburn,dublin&rc=1\n```\n\nTwilio adds an `I-Twilio-Idempotency-Token` header on retries for deduplication.\n\n**Limitations:** Connection overrides are not available on Twilio Conversations or Frontline webhooks. Voice webhooks have a hard 15-second ceiling regardless of override values.\n\n### 5. Configure Webhook URLs via API\n\n**Python**\n```python\n# Phone number -- messaging\nclient.incoming_phone_numbers(\"PNxxxxxxxxxx\").update(\n    sms_url=\"https://yourapp.com/sms\",\n    sms_method=\"POST\",\n    sms_fallback_url=\"https://yourapp.com/sms-fallback\",\n    sms_fallback_method=\"POST\"\n)\n\n# Phone number -- voice\nclient.incoming_phone_numbers(\"PNxxxxxxxxxx\").update(\n    voice_url=\"https://yourapp.com/voice\",\n    voice_method=\"POST\",\n    voice_fallback_url=\"https://yourapp.com/voice-fallback\",\n    voice_fallback_method=\"POST\",\n    status_callback=\"https://yourapp.com/call-status\",\n    status_callback_method=\"POST\"\n)\n```\n\n**Node.js**\n```node\n// Phone number -- messaging\nawait client.incomingPhoneNumbers(\"PNxxxxxxxxxx\").update({\n    smsUrl: \"https://yourapp.com/sms\",\n    smsMethod: \"POST\",\n    smsFallbackUrl: \"https://yourapp.com/sms-fallback\",\n    smsFallbackMethod: \"POST\",\n});\n\n// Phone number -- voice\nawait client.incomingPhoneNumbers(\"PNxxxxxxxxxx\").update({\n    voiceUrl: \"https://yourapp.com/voice\",\n    voiceMethod: \"POST\",\n    voiceFallbackUrl: \"https://yourapp.com/voice-fallback\",\n    voiceFallbackMethod: \"POST\",\n    statusCallback: \"https://yourapp.com/call-status\",\n    statusCallbackMethod: \"POST\",\n});\n```\n\n### 6. Local Development with Tunnels\n\nTwilio cannot reach `localhost`. Use a tunnel to expose your local server.\n\n**ngrok (recommended for development):**\n```bash\nngrok http 5000\n# Copy the HTTPS URL, e.g. https://abc123.ngrok-free.app\n```\n\nThen set the ngrok URL as your webhook in Console or via API.\n\n**Twilio CLI:**\n```bash\n# Install and use the CLI webhook plugin\ntwilio phone-numbers:update +15017122661 \\\n  --sms-url=\"https://abc123.ngrok-free.app/sms\"\n```\n\n**ngrok caveats:**\n- Free tier URLs change on restart -- update Twilio config each time\n- Free tier sessions expire after hours -- use a stable host for anything beyond quick tests\n- For persistent local dev, use ngrok with a custom domain (paid) or deploy to a cloud host\n\n### 7. Event Streams (Webhook Sink)\n\nFor high-volume or cross-product event delivery, use Event Streams instead of per-resource status callbacks. Event Streams deliver events to a Sink (webhook, Kinesis, or Segment). The Twilio SDK does not wrap Event Streams -- use `requests` / `fetch` directly.\n\n**Python -- create a webhook sink and subscribe to error events**\n```python\nimport os, requests\n\naccount_sid = os.environ[\"TWILIO_ACCOUNT_SID\"]\nauth_token = os.environ[\"TWILIO_AUTH_TOKEN\"]\n\n# Create a webhook sink\nsink = requests.post(\n    \"https://events.twilio.com/v1/Sinks\",\n    auth=(account_sid, auth_token),\n    data={\n        \"Description\": \"Error log sink\",\n        \"SinkType\": \"webhook\",\n        \"SinkConfiguration\": '{\"destination\": \"https://yourapp.com/events\", \"method\": \"POST\"}'\n    }\n).json()\n\n# Subscribe to error log events\nrequests.post(\n    \"https://events.twilio.com/v1/Subscriptions\",\n    auth=(account_sid, auth_token),\n    data={\n        \"Description\": \"Error log subscription\",\n        \"SinkSid\": sink[\"sid\"],\n        \"Types\": '[{\"type\": \"com.twilio.error-logs.error.logged\"}]'\n    }\n)\n```\n\nSink types: `webhook`, `kinesis`, `segment`. Subscriptions filter which event types route to which sinks.\n\n### 8. HTTP Authentication for Webhook URLs\n\nTwilio supports HTTP Basic and Digest authentication. Embed credentials in the URL:\n\n```text\nhttps://username:password@yourapp.com/sms\n```\n\nThis provides an additional layer of protection beyond signature validation. Note: these credentials are visible in Console webhook configuration and may appear in server access logs -- rotate them independently of your Auth Token.\n\n---\n\n## Common Webhook Parameters\n\n### Inbound SMS\n\n| Parameter | Description |\n|---|---|\n| `MessageSid` | Unique message identifier |\n| `AccountSid` | Your Twilio account SID |\n| `From` | Sender phone number (E.164) |\n| `To` | Your Twilio number |\n| `Body` | Message text |\n| `NumMedia` | Number of media attachments |\n| `MediaUrl0..N` | URL of each media attachment |\n| `MediaContentType0..N` | MIME type of each attachment |\n\n### Inbound Voice Call\n\n| Parameter | Description |\n|---|---|\n| `CallSid` | Unique call identifier |\n| `AccountSid` | Your Twilio account SID |\n| `From` | Caller phone number (E.164) |\n| `To` | Your Twilio number |\n| `CallStatus` | `queued`, `ringing`, `in-progress`, `completed`, `busy`, `failed`, `no-answer`, `canceled` |\n| `Direction` | `inbound` |\n| `ForwardedFrom` | Number that forwarded the call (if applicable) |\n\n### Message Status Callback\n\n| Parameter | Description |\n|---|---|\n| `MessageSid` | Unique message identifier |\n| `MessageStatus` | `accepted`, `queued`, `sending`, `sent`, `delivered`, `undelivered`, `failed`, `read` |\n| `ErrorCode` | Twilio error code (present on `failed`/`undelivered`) |\n| `ErrorMessage` | Human-readable error description |\n\n### Debugger Event Callback\n\n| Parameter | Description |\n|---|---|\n| `Sid` | Debugger event identifier |\n| `AccountSid` | Account that generated the event |\n| `Level` | `Error` or `Warning` |\n| `Timestamp` | ISO 8601 time of occurrence |\n| `Payload` | JSON with `resource_sid`, `error_code`, `more_info`, `webhook` (request/response details) |\n\n---\n\n## CANNOT\n\n- **Cannot exceed 15-second voice webhook response time** — Twilio hangs up or falls back. Messaging webhooks retry on timeout.\n- **Cannot use HTTP in production** — HTTPS required. No self-signed certificates. Do not pin Twilio certificates — they rotate without notice.\n- **Cannot allowlist Twilio by IP** — Webhooks come from dynamic IPs. Use signature validation instead.\n- **Cannot guarantee status callback delivery or order** — Best-effort. Implement idempotency using `MessageSid` + `MessageStatus` or `CallSid` + `CallStatus` as composite keys.\n- **Cannot redirect without losing POST parameters** — HTTP 301/302 redirects cause Twilio to follow with GET, dropping `Digits`, `RecordingUrl`, etc.\n- **Cannot use connection overrides on Conversations or Frontline webhooks** — Not supported for these products\n\n---\n\n## Next Steps\n\n- **Receive inbound SMS:** `twilio-messaging-webhooks`\n- **Voice call handling:** `twilio-voice-twiml`\n- **Scale webhook handling:** `twilio-reliability-patterns`\n- **Debug webhook failures:** `twilio-debugging-observability`\n- **Secure credentials:** `twilio-iam-auth-setup`\n"
}

SHA-256: 609ead038ef3ed6190ddb2fa3bf2df29b4525fc83d251a991cec4cef011afb9b