agentleFS
wittyreferencewittyreference/twilio-claude-plugin/skills/event-streams/SKILL.md

event-streams

Skill2 starsChanged 8 months agoMITView on GitHub

Twilio development skill: event-streams

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    All REST API calls: https://events.twilio.com/v1/

    line 72
  2. Distinctive:

    When routing Twilio Event Streams to a Segment sink, the CloudEvents envelope maps to Segment track calls:

    line 143
  3. Very specific:

    Deduplication: Use id as messageId to prevent duplicate processing.

    line 155
  4. Distinctive:

    Every event is wrapped in a CloudEvents v1.0 envelope:

    line 161
  5. Very specific:

    Schema versions auto-assigned to latest: When creating a subscription, omitting schema_version subscribes to the latest version.

    line 313

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

name: event-streams description: Twilio Event Streams development guide. Use when building event-driven integrations, streaming Twilio events to webhooks/Kinesis/Segment, setting up sinks and subscriptions, or debugging event delivery.

Event Streams Development Skill

Guide for building event-driven integrations with Twilio Event Streams. Load this skill when streaming platform events (calls, messages, recordings, TaskRouter, etc.) to external systems via webhooks, AWS Kinesis, or Segment.

Evidence date: 2026-03-29 | Account: ACxx...xx

What Event Streams Cannot Do

Explicit list of things developers commonly assume work but don't:

  • Cannot guarantee ordering — Events may arrive out of order. A delivered event can arrive before sent. Design consumers to be order-independent.
  • Cannot guarantee exactly-once delivery — At-least-once semantics. You WILL receive duplicates. Deduplicate using the id field (SID format varies by product: EZ for messaging, VW for voice, NO for error logs).
  • Cannot provide latency SLAs — Typical webhook delivery is 1-5 seconds, but no contractual guarantee. Voice Insights call summaries take 10-30 minutes.
  • Cannot attach multiple subscriptions to one sink — Each sink supports exactly ONE subscription. Error: "Sink with SID DGxxx is in use". Create separate sinks for each subscription. [Evidence: DG1e0cf4..., 2026-03-29]
  • Cannot subscribe to discontinued event types — API rejects with "Type is discontinued". Deprecated types ARE subscribable (they just won't fire for new activity). [Evidence: 2026-03-29]
  • Cannot subscribe to com.twilio.voice.twiml.requested — Listed in docs but API rejects: "Type not found in the system". [Evidence: 2026-03-29]
  • Cannot use the Node SDK types property — client.events.v1.types is undefined in the Twilio Node SDK. Use REST API directly for event type listing/fetching. [Evidence: 2026-03-29]
  • Cannot rely on queued events for API-originated messages — com.twilio.messaging.message.queued is subscribable but never fires for messages sent via the API. Only sent and delivered arrive. [Evidence: SMaf53f7..., 2026-03-29]
  • Cannot use EventBridge as a native sink type — AWS EventBridge requires routing through a Segment sink as intermediary.
  • Cannot receive transcript content via Event Streams — Use the Transcript API or status callbacks for actual transcript text.

Architecture

Event Streams is a publish-subscribe system built on CloudEvents v1.0. It decouples event producers (Twilio platform) from consumers (your systems).

Twilio Platform Events
         │
    ┌────┴────┐
    │  Event  │  CloudEvents v1.0 envelope
    │ Streams │  At-least-once delivery
    │  (free) │  4-hour retry window
    └────┬────┘
         │
    ┌────┴────────────────┐
    │                     │
    ▼                     ▼
┌────────┐          ┌──────────┐
│  Sink  │          │   Sink   │   1 sink = 1 subscription
│(webhook)│         │(kinesis) │   100 sinks per account
└────┬───┘          └────┬─────┘   100 subscriptions per account
     │                   │
     ▼                   ▼
┌──────────┐       ┌──────────┐
│Subscript.│       │Subscript.│   Subscribe to specific event types
│(msg evts)│       │(voice)   │   Schema versioning per type
└──────────┘       └──────────┘

Key Resources

ResourceSID PrefixPurpose
SinkDGDestination endpoint (webhook URL, Kinesis stream, Segment write key)
SubscriptionDFLinks event types to a sink
Event Type—Catalog entry (e.g., com.twilio.messaging.message.delivered)
Schema—JSON Schema definition (e.g., Messaging.MessageStatus)
EventvariesIndividual delivered event (dedup key; prefix varies: EZ messaging, VW voice, NO errors)

Base URL

All REST API calls: https://events.twilio.com/v1/

Quick Decision

NeedUseWhy
React to individual call/message events in real-timeStatus callbacks (webhooks on each resource)Lower latency, per-resource granularity, no setup overhead
Stream ALL events of a type to an analytics pipelineEvent Streams → Kinesis/SegmentCentralized, no per-resource webhook config, schema-versioned
Feed events to a webhook for processingEvent Streams → Webhook sinkSingle endpoint receives all subscribed events across the account
Audit trail / compliance loggingEvent Streams → Kinesis → S3Durable, ordered (within partition), full account coverage
Monitor for errors across all productsEvent Streams → com.twilio.error-logs.error.loggedReal-time error alerts without polling the Debugger API
Voice call quality monitoringEvent Streams → Voice Insights eventsFaster than REST polling (15-30+ min), push-based

Event Streams vs Status Callbacks

DimensionEvent StreamsStatus Callbacks
ScopeAccount-wide, all resourcesPer-resource (set on each call/message)
SetupOne-time: sink + subscriptionPer-resource: StatusCallback URL parameter
FormatCloudEvents JSON with schema versioningForm-encoded (application/x-www-form-urlencoded)
DeliveryAt-least-once, 4-hour retry windowAt-least-once, shorter retry
Latency1-5s typical (30+ min for Insights)Sub-second for most events
OrderingNo guaranteeNo guarantee
Coverage120+ event types across 17 productsProduct-specific callbacks only
CostFree (no Event Streams charges)Free

Decision Frameworks

Choosing a Sink Type

ScenarioSink TypeWhy
Prototype / simple webhook consumerwebhookFastest setup, inspect events immediately
Production analytics pipelinekinesisDurable, scalable, partition by key
Customer data platform integrationsegmentDirect CDP ingestion, no middleware
Need EventBridgesegment → EventBridgeNo native EventBridge sink; route via Segment
Email notifications (testing only)emailSink test events sent to email; not for production

Webhook Sink Configuration

ParameterRequiredDefaultNotes
destinationYes—HTTPS URL. Must respond within 5 seconds.
methodNoPOSTHTTP method
batch_eventsNofalsefalse = individual delivery (1 event per request). true = batched (JSON array).

Webhook Response Handling

Response CodeEvent Streams Behavior
200, 204Success — event acknowledged
400Discard — event is NOT retried
429Retry — rate limiting, event re-queued
500Retry — transient error, exponential backoff

Kinesis Sink Configuration

ParameterRequiredNotes
arnYesKinesis stream ARN
role_arnYesIAM role ARN for cross-account access
external_idYesExternal ID for STS AssumeRole

Segment Sink Configuration

ParameterRequiredNotes
write_keyYesSegment source write key

CloudEvents to Segment Spec Field Mapping

When routing Twilio Event Streams to a Segment sink, the CloudEvents envelope maps to Segment track calls:

CloudEvents FieldSegment FieldNotes
type (e.g., com.twilio.messaging.message.sent)eventUse the full CloudEvents type as the Segment event name
data.*propertiesThe entire data object becomes Segment event properties
time (ISO 8601)timestampDirect mapping — both use ISO 8601
idmessageIdUse as deduplication key — CloudEvents IDs are unique per event
sourcecontext.source_idIdentifies the Twilio account/resource that emitted the event
dataschemaproperties._schemaOptional — preserves schema version for downstream consumers
specversion(omit)CloudEvents metadata — not meaningful in Segment context

Deduplication: Use id as messageId to prevent duplicate processing. Event Streams guarantees at-least-once delivery, so duplicates are expected.

Identity resolution: CloudEvents do not include a Segment userId or anonymousId. Set anonymousId to the resource SID (e.g., CallSid, MessageSid) from data.*, or use a Segment Function to resolve identity from your user database.

CloudEvents Envelope

Every event is wrapped in a CloudEvents v1.0 envelope:

{
  "id": "EZf40f223873cb33b29e536dbcc34fb98b",
  "type": "com.twilio.messaging.message.sent",
  "specversion": "1.0",
  "source": "/2010-04-01/Accounts/ACxxx/Messages/SMxxx.json",
  "time": "2026-03-29T04:00:56.753Z",
  "datacontenttype": "application/json",
  "dataschema": "https://events-schemas.twilio.com/Messaging.MessageStatus/7",
  "data": {
    "messageSid": "SMaf53f755fc01b434d69700511984da79",
    "messageStatus": "SENT",
    "from": "+15551234567",
    "to": "+15559876543"
  }
}

Key fields:

  • id: Deduplication key. SID prefix varies by product: EZ (messaging), VW (voice), NO (error logs). Test events use Account SID.
  • source: REST API resource path (not a full URL).
  • dataschema: Schema URL with version number. Use to handle schema evolution.
  • data: The actual event payload. Structure varies by event type.

Usage Patterns

Create a Webhook Sink + Subscription (Node.js)

const client = require('twilio')(accountSid, authToken);

// Step 1: Create a sink
const sink = await client.events.v1.sinks.create({
  description: 'my-webhook-sink',
  sinkConfiguration: {
    destination: 'https://example.com/events',
    method: 'POST',
    batch_events: false
  },
  sinkType: 'webhook'
});
// sink.sid = "DGxxx"

// Step 2: Test the sink (sends a test event)
await client.events.v1.sinks(sink.sid).sinkTest().create();
// Returns { result: "submitted" }

// Step 3: Validate (requires test_id from received test event)
await client.events.v1.sinks(sink.sid).sinkValidate().create({
  testId: 'received-test-id-uuid'
});
// Returns { result: "valid" }

// Step 4: Create subscription (ONE subscription per sink)
const subscription = await client.events.v1.subscriptions.create({
  description: 'messaging-events',
  sinkSid: sink.sid,
  types: [
    { type: 'com.twilio.messaging.message.sent' },
    { type: 'com.twilio.messaging.message.delivered' },
    { type: 'com.twilio.messaging.message.failed' },
    { type: 'com.twilio.messaging.inbound-message.received' }
  ]
});

Add/Remove Event Types from Existing Subscription

// Add a new event type
await client.events.v1.subscriptions(subscriptionSid)
  .subscribedEvents.create({ type: 'com.twilio.messaging.message.undelivered' });

// Remove an event type
await client.events.v1.subscriptions(subscriptionSid)
  .subscribedEvents('com.twilio.messaging.message.undelivered').remove();

Webhook Receiver (Twilio Functions)

// Event Streams sends JSON, not form-encoded data.
// Use a public (.js) endpoint — see Gotcha #8 for .protected.js considerations.
exports.handler = async function(context, event, callback) {
  // event contains the CloudEvents object (or array if batch_events=true)
  const cloudEvent = event;

  console.log('Event type:', cloudEvent.type);
  console.log('Event ID:', cloudEvent.id);
  console.log('Data:', JSON.stringify(cloudEvent.data));

  // Deduplicate using cloudEvent.id (EZ SID)
  // Store/process cloudEvent.data

  const response = new Twilio.Response();
  response.setStatusCode(200);
  response.appendHeader('Content-Type', 'application/json');
  response.setBody(JSON.stringify({ received: 1 }));
  callback(null, response);
};

Deduplication Pattern

const processedIds = new Set(); // Use Redis/DB in production

function handleEvent(cloudEvent) {
  if (processedIds.has(cloudEvent.id)) {
    return; // Already processed
  }
  processedIds.add(cloudEvent.id);
  // Process the event
}

Query Event Types and Schemas (REST API)

# List available event types
curl "https://events.twilio.com/v1/Types?PageSize=20" \
  -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

# Get specific event type details
curl "https://events.twilio.com/v1/Types/com.twilio.messaging.message.delivered" \
  -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

# List schema versions
curl "https://events.twilio.com/v1/Schemas/Messaging.MessageStatus/Versions" \
  -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

Note: The Node SDK does not expose client.events.v1.types — use REST API for type listing. Sinks, subscriptions, and subscribed events work via SDK.

Gotchas

Setup

  1. One sink per subscription: A sink can only be attached to ONE subscription. Creating a second subscription on the same sink returns "Sink with SID DGxxx is in use". Create separate sinks even if they point to the same URL. [Evidence: DG1e0cf4..., 2026-03-29]
  2. Orphaned sinks on subscription delete: Deleting a subscription does NOT delete its sink. Sinks remain active and count toward the 100-sink limit. Clean up sinks explicitly after removing subscriptions. [Evidence: DG718b1f..., 2026-03-29]
  3. Validate requires receiving the test event first: The sink validate endpoint requires a TestId parameter. This ID comes from the data.test_id field in the test event delivered to your sink. You must POST to /Test first, receive the test event, extract the UUID, then POST to /Validate. [Evidence: DG1e0cf4..., test_id: ac09e217..., 2026-03-29]
  4. 100 sinks + 100 subscriptions per account: Hard limit. Since each subscription needs its own sink, you can have at most 100 active event subscriptions per account.

Configuration

  1. batch_events defaults to false: Despite documentation suggesting batching is the default, creating a sink without specifying batch_events returns batch_events: false. If you DO set it to true, events arrive as JSON arrays, and Twilio Functions spread arrays into numeric keys on the event parameter (event['0'], event['1'], etc.). [Evidence: 2026-03-29]
  2. Schema versions auto-assigned to latest: When creating a subscription, omitting schema_version subscribes to the latest version. This means your payload format can change when Twilio releases a new schema version. Pin versions in production: { type: 'com.twilio.messaging.message.sent', schema_version: 7 }.
  3. Deprecated vs discontinued event types: Deprecated types can be subscribed to (they accept but may not fire). Discontinued types are rejected with an error. Check the /Types endpoint for current status before subscribing. [Evidence: 2026-03-29]

Runtime

  1. Webhook signature validation with Functions: Event Streams signs webhooks using standard Twilio request validation (HMAC-SHA1 via X-Twilio-Signature + bodySHA256 query parameter). Protected Twilio Functions (.protected.js) should validate this, but since the body is JSON (not form-encoded), test thoroughly before relying on .protected.js for Event Streams webhooks.
  2. Error-log feedback loop with Functions webhooks: Subscribing to com.twilio.error-logs.error.logged while using a Twilio Functions webhook creates an infinite feedback loop. Your function's console.log() generates INFO-level alerts → those become error-log events → delivered to your function → more logs → more events. Use a non-Functions endpoint for error-log subscriptions, or filter by data.level early and suppress logging for loop events. [Evidence: DG718b1f..., 2026-03-29]
  3. Error logs include INFO level: The event type com.twilio.error-logs.error.logged fires for ALL debugger alert levels, including INFO. The name is misleading — it's not limited to errors. Check the data.level field (ERROR, WARNING, INFO). [Evidence: 2026-03-29]
  4. Error log payload is a JSON string: The data.payload field in error-log events contains a JSON string, not a parsed object. You must JSON.parse(data.payload) to access the structured content. [Evidence: NO8b9489..., 2026-03-29]
  5. messageStatus is UPPERCASE in Event Streams: Event Streams returns "SENT", "DELIVERED", "FAILED". Status callbacks return lowercase "sent", "delivered", "failed". If you're migrating from status callbacks, update your comparisons. [Evidence: EZf40f22..., 2026-03-29]
  6. Field naming inconsistency between inbound and outbound messaging: Outbound messages use numberOfSegments; inbound messages use numSegments. Different schema names too: Messaging.MessageStatus/7 vs Messaging.InboundMessageV1/6. [Evidence: 2026-03-29]

Observability

  1. Voice event responseBody is base64-encoded: The data.response.responseBody field in voice API-request and TwiML events contains the full response body base64-encoded. Decode with Buffer.from(responseBody, 'base64').toString(). [Evidence: VW094d1e..., 2026-03-29]
  2. CloudEvents id prefix varies by product: Messaging events use EZ, voice webhook events use VW, error log events use NO, and test events use the Account SID. Do not assume EZ prefix for dedup logic — key on the full id string. [Evidence: 2026-03-29]
  3. rawDlrDoneDate format is YYMMDDHHMM: The delivery receipt timestamp in messaging events uses compact format (2603290400), not ISO 8601. Parse accordingly. [Evidence: EZc834b3..., 2026-03-29]
  4. Webhook retry window is 4 hours: Failed deliveries (5xx responses) retry with exponential backoff + jitter for up to 4 hours. After 4 hours, the event is dropped. Return 400 to explicitly discard without retry; return 429 to signal rate limiting.
  5. Webhook timeout is 5 seconds: Your endpoint must respond within 5 seconds or the delivery is treated as failed and retried. For heavy processing, acknowledge immediately (200) and process asynchronously.
  6. Webhook source CIDR: 35.90.102.128/25: Whitelist this CIDR range for firewall rules. Events originate from this IP range.

Error Codes

CodeContextDescription
93101KinesisError writing to stream
93101WebhookRate limited (429), bad request (400), or server error (500)
93102KinesisIAM AssumeRole error
93103KinesisError getting shard count
93104KinesisError getting stream name/region — event discarded

Related Resources

  • Voice Insights events: /skills/voice-insights/SKILL.md — Conference/call summary events via Event Streams
  • Conference Insights: /skills/conference/references/insights-and-validation.md — Real-time conference events
  • Segment integration: /skills/segment/SKILL.md — Segment sink destination setup
  • Deep crawl reference: /references/event-streams-deep-crawl.md — Full 1692-line API reference with all payload schemas
  • Webhook receiver function: event-stream.js — Live webhook that stores events in Sync

Reference Files

TopicFileWhen to read
Live test resultsreferences/test-results.mdWhen verifying specific behaviors or checking evidence SIDs
Event types catalogreferences/event-types-catalog.mdWhen choosing which event types to subscribe to
Deep crawl (full API)/references/event-streams-deep-crawl.mdWhen needing full payload schemas or REST API parameter details

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.