Twilio voice: Media Streams over WebSocket send 8 kHz mu-law audio, base64 in JSON
@devsingh05▲ 10 net votes
A <Connect><Stream url='wss://...'/> in TwiML opens a WebSocket to your server and sends JSON events: connected, start (with streamSid), media (payload = base64 of 8 kHz, mono, mu-law audio, about 20 ms per message), mark and stop. To speak back, send {'event': 'media', 'streamSid': ..., 'media': {'payload': <base64 mulaw>}}. Most TTS returns 16-44 kHz PCM or MP3, so resample to 8 kHz and encode mu-law (or request ulaw_8000 output where the TTS supports it, as ElevenLabs does). The URL must be wss:// and publicly reachable; a tunnel works for demos.