Skip to main content

Getting started with AgentStream

Get audio flowing between a live call and your server in minutes.

Prerequisites​

  • Exotel account with AgentStream enabled (sign up)
  • API Key, API Token, Account SID — Authentication
  • An ExoPhone (virtual number)
  • A publicly reachable WebSocket endpoint (wss://)

Step 1 — Run an echo server​

# echo.py — works on Python 3.8+
import asyncio, json, websockets

async def handle(ws):
stream_sid = None
async for msg in ws:
ev = json.loads(msg)
event = ev.get("event")
if event == "start":
stream_sid = ev["start"]["stream_sid"]
print("stream started", stream_sid)
elif event == "media":
await ws.send(json.dumps({
"event": "media",
"stream_sid": stream_sid,
"media": {"payload": ev["media"]["payload"]},
}))
elif event == "stop":
break

async def main():
async with websockets.serve(handle, "0.0.0.0", 5001):
print("listening on :5001")
await asyncio.Future()

asyncio.run(main())
pip install websockets
python echo.py
# expose with ngrok (or similar): ngrok http 5001
# use the wss:// URL as StreamUrl

Step 2 — Connect Voice AI (outbound)​

export EXOTEL_API_KEY="your_api_key"
export EXOTEL_API_TOKEN="your_api_token"
export EXOTEL_ACCOUNT_SID="your_account_sid"
# India: api.in.exotel.com · Singapore: api.exotel.com
export EXOTEL_SUBDOMAIN="api.in.exotel.com"

curl -u "$EXOTEL_API_KEY:$EXOTEL_API_TOKEN" -X POST \
"https://$EXOTEL_SUBDOMAIN/v1/Accounts/$EXOTEL_ACCOUNT_SID/Calls/connect" \
-F "From=+919876543210" \
-F "CallerId=YOUR_EXOPHONE" \
-F "StreamUrl=wss://YOUR_PUBLIC_HOST/media" \
-F "StreamType=bidirectional"

Answer the phone — you should hear your own audio echoed back.

Step 3 — Confirm success​

CheckExpected
API responsecall.sid present, status queued / in-progress
Server logsstart then continuous media events
Call audioEcho (or your TTS) audible

Next paths​

GoalGo to
Unsure which API to useWhat to use when
IVR / agent handoffConnect with Flow
Build a real botWebSocket protocol
Monitor live streamsWSS errors & monitoring
Samples

Example servers and SDKs: github.com/exotel