Telephony
- Voice AI and telephony
- Communications Infrastructure
- 2025
- Private
NestJS, TypeScript, Twilio, Drizzle and 1 more
Telephony is the communications layer behind my voice agents: SMS, Verify, Lookup, phone numbers, and the A2P 10DLC registrations that make texting deliverable. Product code depends on four capability tokens and never names a vendor. Before it, product code imported a vendor SDK directly, which meant the vendor was named in every feature that sent a message.
The obvious answer, wrapping the SDK in a service class, only moves the coupling one file deeper. The provider still leaks through its response shapes and its error codes, and switching one capability means touching everything that touched that service. It began as a standalone service and now runs as one module of the platform backend, sharing its process, database and authentication.
# the capability seam: four tokens, one bound providergrep -noE '\w+_PROVIDER, use\w+: \w+' *runtime.module.ts133:SMS_PROVIDER, useExisting: TwilioSmsProvider134:VERIFY_PROVIDER, useExisting: TwilioVerifyProvider135:LOOKUP_PROVIDER, useExisting: TwilioLookupProvider136:NUMBERS_PROVIDER, useExisting: TwilioNumbersProviderls common/interfacesindex.ts lookup.provider.ts numbers.provider.tssms.provider.ts verify.provider.ts
2 commands6 lines of output
4capability tokens, each bound to a Twilio provider
- 2
- tiers: portable capabilities and Twilio routes
- 3
- stub adapters in the standalone version, since removed
- Chapter
- Voice AI and telephony
- Category
- Communications Infrastructure
- Year
- Availability
- Private
- NestJS
- TypeScript
- Twilio
- Drizzle
- PostgreSQL
Capabilities not vendors
Each capability defines its own interface in common/interfaces/: SmsProvider, VerifyProvider, LookupProvider and NumbersProvider. Services inject the matching token and never import Twilio. The standalone version went further. A ProviderRegistryService read one PROVIDER_<CAPABILITY> variable per capability and resolved the adapter at request time. When I deployed that service beside the platform in August 2026, the registry and its 3 stub adapters were removed. Today TelephonyRuntimeModule binds the 4 tokens straight to the Twilio providers. Changing carrier is a new provider class and a changed binding, not a configuration flip. The pattern came from running Twilio, Vonage and Telnyx behind one interface for a set of voice agents. Voice itself left the portable core that August, because call-time voice lives in the platform's own call stack.
Where portability stops
The classic way a provider abstraction fails is designing the interface around what every vendor has in common. You then cannot use the reasons you picked a vendor in the first place. A2P 10DLC registration, TrustHub and subaccounts have provider-specific lifecycles that do not compress into a generic send method. So the layer is two tiers on purpose. The capability tier covers traffic that is genuinely portable. Alongside it, Twilio keeps first-class staff routes under /v1/telephony/compliance for A2P 10DLC, TrustHub, messaging services, subaccounts, addresses and regulatory bundles. A sync job pulls the resulting Twilio state back into our tables. The standalone service also exposed TaskRouter, Studio, Proxy and Fax; those did not make the move. The dependency stays visible in one place rather than smeared through the codebase.
The inbound half
Sending is the easy direction. Every SDK send call maps onto a capability method. The hard direction is everything Twilio sends back: message status callbacks, inbound SMS and call events, each with its own payload shape and retry behavior. Every webhook route is built by prepending the signature policy, so a route added later cannot skip verification. The key is resolved from the payload's AccountSid, because a subaccount number is signed with that subaccount's token. Inbound SMS is persisted before the empty TwiML reply, and a duplicate delivery returns the existing row. Status callbacks acknowledge even when the write fails, since a Twilio retry cannot fix a database outage. Voice, recording and verification callbacks only log and acknowledge, because nothing consumes them yet.
An ambiguous send
The dangerous call is the outbound request whose response never arrives. The carrier may already have accepted it. Retrying through a second provider would then create two real messages for one product action. Today the code covers only part of this. sms.http-service.ts resolves the tenant's sender, sends once, then writes the message log. A failed log write is logged and the provider response still returned, so a send is never repeated to repair a record. SendSmsParams carries no operation ID, and the layer never retries, so it cannot double a send itself. A caller that retries still can. A stable operation ID and reconciliation before any retry are what a second bound provider would require first.
Decisions and costs
- Two tiers rather than a lowest common denominator. Provider-specific routes stay first class, which means the layer does not promise portability it cannot deliver. The cost is that a workflow can quietly become Twilio-only.
- One module, not a second service. Folding it into the backend removed an HTTP hop, a second ORM and a second connection pool, and staff now use a session instead of a shared secret. The cost is that telephony deploys with everything else.
- Stubs, then none. The standalone version shipped Vonage, Plivo and MessageBird as stubs that threw
NotImplementedException, so an unimplemented capability was a stated fact. They went with the registry. What I would revisit is shipping only one real adapter, because a second implementation is the only honest test of an abstraction.
Limits and status
Twilio is the only bound provider. Adding another means implementing the capability interfaces I care about and changing the bindings, while the public routes and the product workflows above them stay where they are. The module registers only when TWILIO_ACCOUNT_SID is set. Status values are stored as Twilio sends them, with no normalized vocabulary yet. Failover is bounded by reality rather than by code. An alternate provider would still need suitable numbers, credentials, capacity and registrations. Configuration cannot move an active call, port an inbound number instantly or transfer a campaign approval. With one provider, one use case and no volume, this whole layer is ceremony. It starts paying rent at the second provider, the second region, or the first outage.