Twilio Media Streams with Giggy WebSocket STT
Twilio Media Streams delivers 8 kHz G.711 mu-law payloads over WebSockets. By decoding mu-law bytes to 8 kHz LINEAR16 PCM in memory (a 256-entry lookup table, no ffmpeg or resampling), you can stream live calls straight to wss://api.giggy.ai/v1/audio/transcriptions for $0.08 per audio hour in 27 languages.
Before you start
- Create a Giggy API key (giggy_sk_) and confirm your account has credits (Personal accounts include free credits; paid WebSocket sessions run up to 12 hours).
- Configure a Twilio TwiML webhook returning <Connect><Stream url="wss://your-bridge.example.com/twilio" />.
- Check whether your callers speak one of Giggy's 27 supported STT languages (including English, German, Spanish, French, Japanese and Korean).
Create it in Giggy
- Accept the Twilio Media Streams WebSocket connection and wait for the Twilio "start" event.
- Open an authenticated upstream WebSocket to wss://api.giggy.ai/v1/audio/transcriptions with xi-api-key: $GIGGY_API_KEY.
- Send the Giggy start message specifying 8000 Hz LINEAR16 and an optional ISO language code (such as en or de).
- For each Twilio "media" event, base64-decode media.payload, convert mu-law bytes to signed 16-bit little-endian PCM via a lookup table, and forward the binary buffer to Giggy.
- Consume cumulative transcript_chunk events from Giggy and send the stop/input_complete signal when Twilio sends its "stop" event.
Improve the result
- Keep your WebSocket bridge in a region close to Twilio media edge and api.giggy.ai to minimize round-trip latency.
- For post-call QA or call-recording archives where live text is not needed, submit the recorded WAV to POST /v1/audio/transcriptions?mode=batch ($0.02/hr) or mode=fast ($0.021/hr).
- Because Giggy does not perform speaker diarization, stream inbound and outbound Twilio tracks on separate channels when you need speaker separation.
Examples
- Start frame to Giggy WebSocket: {"type": "start", "sample_rate": 8000, "encoding": "LINEAR16", "language": "en"} (omit language for auto-detection).
- In-memory G.711 mu-law decode: base64-decode Twilio media.payload, map each byte through a 256-entry Int16 lookup table into a Buffer, and send binary frames directly to Giggy.
- Cost for 1,000 hours of live phone calls: $80 on Giggy Streaming ($0.08/hr) vs $120 est. on Soniox, $450 on AssemblyAI Realtime and $462–$468 on Deepgram.
Common mistakes
- Forwarding raw G.711 mu-law bytes to Giggy without decoding to LINEAR16 PCM16 first.
- Spawning ffmpeg per call to resample 8 kHz to 16 kHz when Giggy accepts 8 kHz LINEAR16 directly.
- Appending cumulative transcript_chunk text instead of replacing the current utterance buffer with the latest cumulative text.
Related guides
- How to create text to speech
- How to create a designed voice
- How to create a voice clone
- How Generation History works
- How My Voices and Voice Library work
- Migrate from OpenAI tts-1 before shutdown
- Build a Pipecat voice agent with Giggy
- Use Giggy TTS in LiveKit Agents
- Configure Giggy as a Jambonz custom STT provider
- Connect the Giggy MCP server to Claude Code, Cursor and Codex
- Automate voiceovers and transcription in n8n