Sessions
A session is one conversation between a visitor and an agent.
The quickest way to put an agent on your website is the widget embed code from a website channel. It opens sessions for you. See Website widget channel. Use the WebSocket below only if you build your own client.
How a voice session works
Your client opens one WebSocket to VoisX and keeps it open for the whole conversation. Your client sends microphone audio and plays back the audio the agent sends. VoisX does the rest:
- connects to the agent's voice provider
- runs the agent's tools, with the credentials and settings you saved in the console. See Tools.
- checks credit and voice lines, and bills the session
- saves the session's length and transcript, so it shows in the console
Your client never connects to the voice provider itself. There is no token to request and no tool call to run.
Open a voice session
/widget/v1/realtimeOpens a voice session with an agent. Connect with wss://, for example wss://in.api.voisx.ai/widget/v1/realtime?key=vois_pk_...&agent_id=2210.
Browsers cannot set headers on a WebSocket, so the project API key goes in the key query parameter instead of X-API-Key. The key needs the widget:session permission. If the key is limited to domains, the page's Origin must be one of them. See Domain restrictions.
Use this endpoint for every agent, whichever voice provider it uses: OpenAI, Gemini, ElevenLabs, Sarvam or VoisX speech.
Query parameters
keystringrequiredagent_idintegerrequiredGET /widget/v1/config.visitor_idstringoptionaluser_id.user_idstringoptionaluser_hash.user_hashstringoptionaluser_id, as hex. See Agent Memory for how to compute it on your server.Audio format
Audio in both directions is 16-bit PCM, 24 kHz, mono, little-endian, sent as base64 inside JSON messages. Send microphone audio in chunks of about 40 ms (960 samples, 1,920 bytes before base64).
Messages you send
type | Fields | When to send it |
|---|---|---|
greet | none | Once, after connected, if the visitor has not spoken yet. The agent says its greeting first. |
audio_chunk | audio: base64 PCM16 | Continuously while the microphone is on, about every 40 ms. VoisX detects when the visitor starts and stops speaking. |
user_message | text | The visitor typed a message instead of speaking. |
Messages you receive
type | Fields | What to do |
|---|---|---|
connected | session_id | The session is ready. Start sending audio. Keep session_id to find the session in the console. |
assistant_delta | text, item_id, response_id | A piece of the agent's reply as text. Append it to the agent's message for item_id. |
assistant_audio_delta | audio, item_id | A piece of the agent's reply as base64 PCM16. Queue it for playback. |
user_turn | item_id, previous_item_id | The visitor finished a spoken turn. Add a placeholder message for item_id, after previous_item_id. The text follows in user_transcript. |
user_transcript | text, item_id | What the visitor said. Fill in the message for item_id. |
interrupted | none | The visitor started talking over the agent. Stop playback and clear queued audio at once. |
assistant_truncated | item_id, text | After an interruption, the part of the agent's reply the visitor actually heard. Replace the message for item_id with text. If text is empty, remove the message. |
assistant_done | none | The agent's reply is complete. |
tool_call | name | The agent is running a tool. You can show a "working on it" state. |
tool_result | name, success | The tool finished. |
error | message, optional code | Something went wrong. Show message. With code: "agent_unavailable" the socket then closes with 1013. |
Ignore message types you do not recognise. New ones can be added to v1 at any time.
Close codes
When VoisX closes the socket, the close code and reason say why.
| Code | Meaning | What to do |
|---|---|---|
1000 | The session ended normally, for example the conversation finished, the line was silent too long or the maximum call length was reached. The reason says which. | Show the reason. Start a new session if the visitor wants to talk again. |
1008 | All your organization's voice lines are in use. | Wait for a call to end, then retry. |
1013 | Try again later. The voice agent or the server is temporarily unavailable. | Reconnect after a short wait, with backoff. |
4401 | The key is invalid or revoked, lacks widget:session, or the Origin is not allowed for the key. | Check the key and its allowed domains. |
4403 | The key cannot use this agent, the agent is in another project or not live, the channel is inactive, or your plan does not include a feature the agent uses. | Read the reason. Do not retry unchanged. |
4404 | The agent was not found. | Check agent_id and the region. |
4429 | The key's rate limit was reached. | Wait and retry. See Rate limits. |
4503 | The agent service is switched off for a short time. | Retry later. |
Stop playback as soon as you get interrupted, not when assistant_truncated arrives. Audio that keeps playing after the visitor starts talking makes the agent sound like it is not listening.