TechFabricTechFabricHarness
EcosystemChannels

Twilio

Connect Twilio messaging and voice events to Fabric agents, with the signature check done before anything is dispatched.

Use the messaging adapter from @fabric-harness/channels/twilio.

fh add channel twilio
import { createTwilioChannel } from '@fabric-harness/channels/twilio';

export default createTwilioChannel({
  accountSid: process.env.TWILIO_ACCOUNT_SID!,
  authToken: process.env.TWILIO_AUTH_TOKEN!,
  webhookUrl: process.env.TWILIO_WEBHOOK_URL!,
  agent: 'assistant',
  tenantId: 'acme',
});

Mount POST /channels/twilio/messages. webhookUrl must be the exact public URL configured in Twilio when a proxy rewrites the internal request URL. The adapter validates X-Twilio-Signature with HMAC-SHA1 over the URL and sorted, deduplicated form values.

The port may differ. A signature made over the URL as given, over the same URL without its port, or over the same URL with its port spelled out (443 for HTTPS and WSS, 80 for HTTP and WS) is accepted, as Twilio's SDK does. Any other port is rejected. The SDK's legacy query string variant isn't implemented, so if Twilio signs a differently encoded query string, set webhookUrl to the exact string Twilio signed.

The adapter deduplicates by Message SID, records the sender as actor, and distinguishes SMS from whatsapp: addresses.

Use replyWithTwilio(ref, { authToken }) for outbound SMS or Twilio WhatsApp messages.

For phone calls, use @fabric-harness/channels/twilio-voice: it verifies the Voice webhook and the Media Streams upgrade, carries a signed call-bound token through <Parameter>, and bridges the stream to governed session.voice(). See Phone calls (Twilio). Gate outbound calls, transfers, and recordings separately and apply phone-number/recording privacy controls.

Twilio messaging client

Use createTwilioMessagingClient for an application-owned outbound SMS action without an agent. Resolve Twilio credentials at the trusted application boundary. For example:

import { createTwilioMessagingClient } from '@fabric-harness/channels/twilio';

const client = createTwilioMessagingClient({
  accountSid: 'AC-example',
  authToken: 'resolved-at-runtime',
  fetchImpl: async () => Response.json({ sid: 'SM-example', status: 'queued' }),
});
const result = await client.send({ to: '+15558675310', from: '+15557122661', body: 'Hello' });
console.log(result.sid); // SM-example (credential-free stub)

For a live send, omit the stub and supply account credentials and a permitted sender (from, messagingServiceSid, or both). The stub creates no resources to clean up. statusCallbackUrl sets the default absolute HTTP(S) callback URL; statusCallback overrides it per send when non-blank; a blank value falls back to the default. timeoutMs defaults to 10 seconds and covers response reading; signal supports caller cancellation.

Catch TwilioMessagingError: opt_out means Twilio reported code 21610; rate_limited is the only retryable kind. rejected reports other 4xx refusals. server_error, timeout, aborted, network and invalid_response have outcomeUnknown: true, so a message may have been created. HTTP status, Twilio code, providerMessage and moreInfo are available when supplied. Provider text is kept outside the generic error message; transport failures retain their cause. The client sends no idempotency key and does not retry. Applications own retries, deduplication, opt-out state and consent.

parseTwilioStatusCallback(request, { authToken, webhookUrl }) verifies a bounded body against the exact public webhook URL and returns { ok: true, record } or { ok: false, reason }. Reasons are too_large, invalid_signature, and not_a_status_callback. It does not dispatch to an agent. parseTwilioStatusParams(params) parses without verification. Records preserve unknown statuses, optional sender/recipient/service fields, integer error codes, optOut when code 21610 is reported, rawDlrDoneDate, a message/status dedupeKey, and first values of additional parameters in params.