Connect your own call leg to an agent
Summary
An agent session runs an agent’s live version on a call leg that you already own, over a media WebSocket. This guide starts a session, streams audio both ways, follows the call and ends it, and lists the setup mistakes that make a session fail or sound wrong. It does not cover calls that Rasen dials; see Place calls and read results.
Facts
Steps
Set AGENT_ID to the id of an agent with a live published version.
1. Start the session
Declare in media the exact format of the audio that your call leg carries. The sample declares
raw 16 kHz PCM16 in binary frames.
The answer is 201 with the session. Check that media is the format you meant: Rasen freezes
it on the call. Set CALL_ID to call_id and WEBSOCKET_URL to websocket_url. Do not print or
log websocket_url in production: it is a credential.
If the answer is lost, send the same request with the same Idempotency-Key. You get the same
call, websocket_url and expires_at. A replay never extends expires_at.
2. Connect the socket and stream audio
Connect to websocket_url before expires_at. Then send the caller’s audio all the time, in real
time: one frame of 20 ms every 20 ms, with silence when the caller is quiet. The agent’s audio
comes back on the same socket. The call becomes active when your first audio frame arrives.
This step talks to the media socket, not to the REST API. The samples read a raw file,
caller.raw (16 kHz PCM16 mono), as a stand-in for your call leg, send silence after it, and save
the agent’s audio to agent.raw.
With media.transport json, send each frame as a text frame instead:
media.timestampis optional: the milliseconds of the frame since your stream started. With it, the recording places each frame at its own time.- A keypad digit is
{"event": "dtmf", "dtmf": {"digit": "5"}}. - Rasen sends the agent’s audio as
{"event": "media", "streamId": "...", "media": {"payload": "...", "encoding": "pcm16", "sampleRate": 16000}}. When the caller talks over the agent, Rasen sends{"event": "clear", "streamId": "..."}: drop the agent audio that you have buffered and not played yet.
3. Follow the call
Read the call while it runs, and after it ends, as any other call. Set CALL_ID to the call_id
from step 1.
The call is admitted until your first audio frame arrives, then active. After it ends, read it
until terminal is true, and wait poll_after_seconds between reads. The transcript, the
recording and the results work as for any call. To follow the words live, see
Follow a call’s transcript live.
4. End the session
End the call from your side in one of two ways:
- Close the socket when your call leg hangs up. Closing the socket ends the call.
- Call
endCallwhen you want the agent to stop but your leg stays up, for example to hand the caller to a person.
A live call answers {"terminal": false, "end_requested": true}, and the call ends shortly. When
the call ends, for any reason, Rasen stops the agent’s audio. Treat a socket that Rasen closes as
the end of the call, and hang up your leg.
Common mistakes
Rules
- MUST create the session on your server. MUST NOT put
RASEN_API_KEYin browser or mobile code. Give the browser only thewebsocket_urlof its own call. - MUST declare
mediaas the exact format of the leg, and MUST send and play audio in that format in both directions. - MUST connect to
websocket_urlexactly as returned, beforeexpires_at, with one socket for each call. MUST NOT log it. - MUST send audio continuously in real time, including silence, in frames of 20 ms. MUST NOT buffer the caller’s audio and send it in bursts.
- MUST drop buffered agent audio on a
clearevent when you usetransport: json. A switch that buffers the agent’s audio deeply SHOULD usejson, becausebinaryhas noclearevent. - MUST send a new
Idempotency-Keyfor each new session, and the same key and body to retry a lost answer. - MUST wait
Retry-Afterseconds after503 at_capacity,429 workspace_concurrency_fullor429 quota_exceeded, then send the identical request with the same key. Double the wait on each new refusal, with a random part, and stop after a few minutes. - MUST close the socket when your leg hangs up, so that the call ends and its capacity is free.
Verify
Expected output after the call: terminal is true, and end_reason is not never_connected.