Programmable Voice APIs with AgentStream
When you build bot-driven voice experiences, reducing perceived latency is critical. With Exotel ExoML, you can control call legs and media actions in real time. This guide explains two recommended approaches:
- Approach 1: Create a customer leg and start a stream as soon as the call is answered.
- Approach 2: Create a leg, start a stream, and play a short greeting (say/play) in parallel while the bot connects.
Both flows use ExoML: every action is an ExoML <Flow> sent to one endpoint, POST /v2/accounts/{account_sid}/legs/{leg_sid}/actions.
Important: There are no separate per-action endpoints such as /actions/start_stream, /actions/start_say, /actions/start_play, /actions/stop_say or /actions/stop_play. Those URLs return 404 (code 1000). Send every action as ExoML to /actions, as shown below.
Prerequisites
- An Exotel account with ExoML (Legs) enabled, and an API key and token that have Legs access. Contact Exotel support to enable it.
- An ExoPhone enabled for ExoML (Legs) calls. Ask Exotel support which number to use.
- An event endpoint to receive leg events. See Implementing your HTTPS event endpointImplementing your HTTPS event endpoint.
- A WebSocket bot server (wss://) that supports Exotel streaming and events.
Authentication and Base URL
All ExoML APIs are served from the Exotel CPaaS API host. Use HTTP Basic authentication (API key as the username, API token as the password) and Content-Type: application/json.
Region | Base URL |
|---|---|
Mumbai | https://cpaas-api.in.exotel.com/v2/accounts/{account_sid} |
Approach 1: Create Leg + Start Stream (Bot-First)
Step 1. Create the leg
curl -u '<api_key>:<api_token>' -X POST \
'https://cpaas-api.in.exotel.com/v2/accounts/<account_sid>/legs' \
-H 'Content-Type: application/json' \
-d '{
"contact_uri": "+919876543210",
"exophone": "<your_exophone>",
"leg_event_endpoint": "https://your-server.example.com/exotel/events",
"timeout": 30
}'Response (202 Accepted):
202 means the call was queued, not answered. Your event endpoint will receive events such as leg_connecting, leg_ringing, and finally leg_answered.
Step 2. Start the stream
On leg_answered, start the stream:
curl -u '<api_key>:<api_token>' -X POST \
'https://cpaas-api.in.exotel.com/v2/accounts/<account_sid>/legs/<leg_sid>/actions' \
-H 'Content-Type: application/json' \
-d '{"exoml": "<Flow><StartStream streamType=\"bidirectional\" streamUrl=\"wss://bot.example.com/stream\"/></Flow>"}'ExoML attribute | Required | Description |
|---|---|---|
streamType | Yes | bidirectional for a voice bot (the caller hears the bot); unidirectional to only receive audio |
streamUrl | Yes | Your bot's wss:// URL. Write & in the URL as &" |
A 202 response with an action_sid means the action was accepted. At this point, audio flows between the customer and your bot.
Send actions only after leg_answered. Actions sent while the phone is still ringing fail (400 code 1013, or 500 code 1004) or are accepted and have no effect, which results in a silent call.
Approach 2: Create Leg + Start Stream + Say/Play (Parallel)
Step 1. Create the leg
Same as Approach 1.
Step 2. Start actions in parallel
On leg_answered, send two actions to the same endpoint.
Start the stream:
curl -u '<api_key>:<api_token>' -X POST \
'https://cpaas-api.in.exotel.com/v2/accounts/<account_sid>/legs/<leg_sid>/actions' \
-H 'Content-Type: application/json' \
-d '{"exoml": "<Flow><StartStream streamType=\"bidirectional\" streamUrl=\"wss://bot.example.com/stream\"/></Flow>"}'Start the greeting (choose one):
Say:
curl -u '<api_key>:<api_token>' -X POST \
'https://cpaas-api.in.exotel.com/v2/accounts/<account_sid>/legs/<leg_sid>/actions' \
-H 'Content-Type: application/json' \
-d '{"exoml": "<Flow><Say>Hello</Say></Flow>"}'Play:
curl -u '<api_key>:<api_token>' -X POST \
'https://cpaas-api.in.exotel.com/v2/accounts/<account_sid>/legs/<leg_sid>/actions' \
-H 'Content-Type: application/json' \
-d '{"exoml": "<Flow><StartPlay>https://example.com/contact-center-tone.wav</StartPlay></Flow>"}'Step 3. Stop the greeting when the bot is ready
As soon as your bot is ready to speak (for example, when it receives the stream's start message), stop the placeholder:
To stop | ExoML |
|---|---|
Say | <Flow><StopSay/></Flow> |
Play | <Flow><StopPlay/></Flow> |
curl -u '<api_key>:<api_token>' -X POST \
'https://cpaas-api.in.exotel.com/v2/accounts/<account_sid>/legs/<leg_sid>/actions' \
-H 'Content-Type: application/json' \
-d '{"exoml": "<Flow><StopSay/></Flow>"}'This ensures the bot audio takes over without overlap.
When to Use Each Approach
Scenario | Recommended Flow |
|---|---|
Bot-first, IVR-style journeys | Approach 1 |
Sales / Collections (instant feel) | Approach 2 |
Contact center simulation | Approach 2 |
Tech support bot only | Approach 1 |
Key Notes
- One endpoint: every action (stream, say, play, stop) is ExoML on POST /legs/{leg_sid}/actions.
- Timing: send actions only after leg_answered.
- Parallelism: you can send <StartStream> and <Say> / <StartPlay> on the same event.
- Greeting length: keep placeholder audio short so the bot can take over naturally.
- Stop timing: stop say/play as soon as the bot is ready to speak.
Errors
HTTP | Code | Message | Cause and fix |
|---|---|---|---|
401 | 1010 | Authentication failed | API key without ExoML (Legs) access, or the leg belongs to another account. Use a Legs-enabled key. |
503 | 1012 | no route found | The ExoPhone is not enabled for ExoML calls. Use the number Exotel support gave you. |
404 | 1000 | Not Found | Unknown leg SID, or a per-action URL such as /actions/start_stream. Use /actions with ExoML. |
400 | 1013 | Hostname is nil for legsid | Action sent before the call was answered. Wait for leg_answered. |
500 | 1004 | redis: nil | Action sent before the leg was ready. Wait for leg_answered. |
Next Steps
- Get ExoML enabled for your account.
- See ExoMLExoML — Introduction for all actions (Say, Play, Gather, Record, Dial, bridges and more).
- Explore StopSay/StopPlay timing to fine-tune transitions.