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.
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
{
"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
| Event | Meaning |
|---|---|
SESSION.WELCOME | The authenticated session is ready. |
CALL.ACCEPTED | The outbound request was accepted; this is not an answer. |
CALL.RINGING | The destination is ringing. |
CALL.ANSWERED | The call has been answered. |
CALL.MEDIA_READY | Connect to the provided media URL. |
CALL.MEDIA_ERROR | Media setup failed; inspect the error and clean up. |
CALL.FAILED | The call failed. |
CALL.ENDED | The call ended. |
SESSION.ERROR | A request or session error occurred. |
SESSION.DRAINING | Reconnect 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
{
"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.