Voice Agents

Developer

API documentation and access keys

API Reference

Connect to your agents programmatically via WebSocket for real-time voice and text interactions.

Voice WebSocket

Stream bidirectional audio for real-time voice conversations.

Endpoint

WebSocket URL
wss://<your-api-host>/agents/<agent-id>/voice?token=<api-key>

Protocol

  1. Connect to the WebSocket URL with your API key as the token query parameter
  2. Wait for { "type": "proxy-ready" }
  3. Send a session-start JSON message with your audio sample rate
  4. Wait for { "type": "session-ready", "outputSampleRate": 24000 }
  5. Stream PCM Int16 LE audio as binary WebSocket frames
  6. Receive binary PCM audio (24kHz Int16 LE) and JSON transcript messages

Session Start

session-start.json
{
  "type": "session-start",
  "sampleRate": 48000,
  "phoneNumber": "+12125551234",
  "externalId": "SF-001234",
  "salesforce": {
    "accessToken": "00D...!AQ...",
    "refreshToken": "5Aep...",
    "instanceUrl": "https://kws.my.salesforce.com",
    "userId": "005..."
  }
}

phoneNumber and externalId are optional. If provided, the agent uses caller identity context.

salesforce is how the Salesforce widget hands over the signed-in rep's own session (tokens issued to the platform's Connected App for that user). With it, every Salesforce read and write of the session runs as the rep — Salesforce enforces the rep's sharing, and CreatedBy/LastModifiedBy are the rep.refreshToken lets the agent refresh when the access token expires; without it an expired token ends Salesforce access for the session (the agent tells the rep to reconnect).userId is optional (resolved via /oauth2/userinfo otherwise). Text sessions accept the same frame as their first message. Without salesforce, production agents offer no Salesforce tools; test agents fall back to the platform's integration user.

Example

voice-example.js
const ws = new WebSocket(
  'wss://your-api-host/agents/AGENT_ID/voice?token=YOUR_API_KEY'
);

ws.onopen = () => console.log('Connected');

ws.onmessage = (event) => {
  if (typeof event.data === 'string') {
    const msg = JSON.parse(event.data);
    if (msg.type === 'proxy-ready') {
      ws.send(JSON.stringify({
        type: 'session-start',
        sampleRate: 16000
      }));
    } else if (msg.type === 'session-ready') {
      console.log('Ready! Output sample rate:', msg.outputSampleRate);
      // Start streaming PCM audio...
    } else if (msg.type === 'transcript') {
      console.log('Agent said:', msg.text);
    }
  } else {
    // Binary frame: PCM Int16 LE audio at 24kHz
    playAudio(event.data);
  }
};

Audio Format

  • Input: PCM Int16 LE at any sample rate (specify in session-start)
  • Output: PCM Int16 LE at 24kHz

Text Chat WebSocket

Send and receive text messages for chat-based interactions.

Endpoint

WebSocket URL
wss://<your-api-host>/agents/<agent-id>/chat?token=<api-key>

Protocol

  1. Connect to the WebSocket URL with your API key as the token query parameter
  2. Wait for { "type": "proxy-ready" }
  3. Send messages as { "type": "message", "text": "your message" }
  4. Receive streamed text chunks as { "type": "text", "text": "..." }
  5. Response complete when { "type": "turn-complete" } arrives
  6. Tool calls appear as tool-call followed by tool-result

Example

chat-example.js
const ws = new WebSocket(
  'wss://your-api-host/agents/AGENT_ID/chat?token=YOUR_API_KEY'
);

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  if (msg.type === 'proxy-ready') {
    ws.send(JSON.stringify({ type: 'message', text: 'Hello!' }));
  } else if (msg.type === 'text') {
    process.stdout.write(msg.text);
  } else if (msg.type === 'turn-complete') {
    console.log('\n--- Agent finished responding ---');
  } else if (msg.type === 'tool-call') {
    console.log('Tool called:', msg.name, msg.args);
  } else if (msg.type === 'tool-result') {
    console.log('Tool result:', msg.name, msg.result);
  }
};

Identity Parameters

Both voice and text endpoints accept optional identity query parameters for caller context resolution.

  • phoneNumber -- resolves identity via caller mapping
  • externalId -- direct identity injection (bypasses mapping)
  • language -- preferred response language
Identity Parameters Example
wss://<host>/agents/<id>/chat?token=<key>&externalId=SF-001234&language=German
KWS Voice Agents