AsyncFlow API
  1. Integrations
AsyncFlow API
  • Welcome to AsyncFlow API
  • Get Started
  • API Reference
    • API Status
      • API Status Check
    • Text-to-Speech
      • Text to Speech (WebSocket)
      • Text to Speech
      • Text to Speech with Word Timestamps
      • Text to Speech (Stream)
    • Voice Management
      • Clone Voice
      • List Voices
      • Get Voice
      • Update Voice
      • Delete Voice
      • Get Voice Preview
  • Integrations
    • Integrate with Twilio
  1. Integrations

Integrate with Twilio

Bring low‑latency, high‑quality speech from Async into any Twilio voice call. This guide shows how to:
1.
Connect to Async Text‑to‑Speech (WebSocket)
2.
Spin up a local WebSocket server for Twilio <Stream/> media
3.
Expose that server through ngrok
4.
Dial a phone call and pipe the generated audio into it

1  Prerequisites#

ToolNotes
Node.js 18+ES module syntax and modern WS APIs
Twilio accountCopy your Account SID and Auth Token; buy/verify numbers
Async accountCopy your API key and pick a Voice ID
ngrok (free)Exposes your local WS server to Twilio’s cloud

2  Environment variables#

Create a .env file next to the script:
You can also supply any of these via command‑line, e.g. node async-twilio.js OUTBOUND_NUMBER=+1555….

3  How the bridge works#

StepFlow
1Script connects to Async over WebSocket, sends an init frame (model, voice, codec).
2A lightweight HTTP + WS server starts locally (ws://localhost:<port>).
3ngrok publishes that port; you get a public wss:// URL.
4Script tells Twilio to dial <OUTBOUND_NUMBER> and stream call audio to that URL.
5On Twilio start, script streams text → Async.
6Async replies with μ‑law PCM chunks; script forwards each chunk to Twilio as media frames.
7After all chunks (or on timeout) script ends the call.

4  Sample code#

1
Setup the required variables and helper functions
2
Connect to async TTS
3
Local ws setup for twilio
4
Pipe async → twilio
5
Twilio helper functions
6
Running the functionality

5  Customising the integration#

GoalWhere to change
Different voiceCFG.ASYNC_VOICE_ID
Different codec / rateoutput_format in connectAsyncTTS()
Stream arbitrary textReplace CFG.TEST_SENTENCE, or feed user input into asyncWs.send()
Keep the call openRemove the chunksSeen guard and endCall() timer

6  Debugging tips#

No audio? Make sure Twilio can reach your ngrok URL (port 443, wss).
Choppy playback? Forward Async chunks to Twilio as soon as they arrive—don’t buffer them.
Delay before speech starts? Use force: true in the transcript frame to synthesize short text immediately.

7  Next steps#

1.
Bidirectional audio – Send caller speech to Async STT and build IVR bots.
2.
Fail‑over logic – Retry with a new ngrok tunnel or switch data centres automatically.
3.
Serverless deployment – Move the bridge to AWS Lambda or Fly.io and drop ngrok for a fixed WSS endpoint.

Modified at 2025-07-15 14:36:53
Previous
Get Voice Preview
Built with