WebSocket call control

Authenticate a control session, start an outbound call, follow its events, and end it explicitly.

Open developer playground

Open a control session

Use the control endpoint assigned to your account. The gateway route is /control/session; the public marketing site is not an API host. The Node starter uses the ws package so it can send the authorization header.

js
import WebSocket from 'ws';

const socket = new WebSocket(process.env.SIPMUNK_CONTROL_URL, {
  headers: { Authorization: `Bearer ${process.env.SIPMUNK_API_KEY}` },
});
socket.on('message', data => {
  const event = JSON.parse(data.toString());
  console.log(event.type); // Do not log keys or media tokens.
});

Wait for SESSION.WELCOME before sending call commands. Keep credentials on the server; browser WebSockets cannot set this authorization header.

Start an outbound call

json
{
  "action": "CALL.START",
  "payload": {
    "idempotency_key": "order-123-call-1",
    "did": "+15555550100",
    "destination": "+15555550101",
    "media_websocket": true,
    "media_sample_rate": 16000,
    "metadata": { "flow": "support" }
  }
}

Replace the example numbers with your configured caller ID and an authorized destination. The live command may incur charges.

idempotency_key is required, nonempty, and at most 200 characters. Reuse it when retrying the same request. Use a new key for a genuinely new call. With media_websocket: true, a separate media socket is created after the carrier answers. The default sample rate is 16,000 Hz; the implementation accepts integer rates from 8,000 to 48,000 Hz. Match the rate your application actually supports.

Do not combine media_websocket with agent_id. The source starter uses your own external audio application rather than the gateway's separate managed-agent path.

Handle events

EventMeaning
SESSION.WELCOMEThe authenticated session is ready.
CALL.ACCEPTEDThe outbound request was accepted; this is not an answer.
CALL.RINGINGThe destination is ringing.
CALL.ANSWEREDThe call has been answered.
CALL.MEDIA_READYConnect to the provided media URL.
CALL.MEDIA_ERRORMedia setup failed; inspect the error and clean up.
CALL.FAILEDThe call failed.
CALL.ENDEDThe call ended.
SESSION.ERRORA request or session error occurred.
SESSION.DRAININGReconnect before attempting new work.

This table lists possible events, not a guarantee that every call emits every event. Treat CALL.FAILED and CALL.ENDED as terminal. Do not automatically redial after a socket disconnect; reconcile the original request and its idempotency key first.

End the call

json
{
  "action": "CALL.END",
  "payload": {
    "call_id": "the-call-id-from-the-event",
    "reason": "agent_complete"
  }
}

Closing a local WebSocket is not a substitute for explicitly ending a live call. The starter's live example uses a deadline and sends CALL.END before cleanup where possible.

Keep the connection healthy

The control channel supports {"action":"PING","payload":{}}, which returns PONG. The Node client also responds to the WebSocket protocol ping frames handled by ws. Reconnect with bounded backoff in your production application; do not silently replay call starts.