Skip to main content

Sessions

A session is one conversation between a visitor and an agent.

tip

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​

WS/widget/v1/realtime

Opens 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​

keystringrequired
Your project API key.
agent_idintegerrequired
The agent to talk to. Get IDs from GET /widget/v1/config.
visitor_idstringoptional
An anonymous ID for this browser, up to 64 characters. Used by Agent Memory when there is no user_id.
user_idstringoptional
Your own ID for a signed-in visitor, up to 128 characters. Used by Agent Memory to recognise returning visitors. Send it with user_hash.
user_hashstringoptional
The HMAC-SHA256 signature of user_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​

typeFieldsWhen to send it
greetnoneOnce, after connected, if the visitor has not spoken yet. The agent says its greeting first.
audio_chunkaudio: base64 PCM16Continuously while the microphone is on, about every 40 ms. VoisX detects when the visitor starts and stops speaking.
user_messagetextThe visitor typed a message instead of speaking.

Messages you receive​

typeFieldsWhat to do
connectedsession_idThe session is ready. Start sending audio. Keep session_id to find the session in the console.
assistant_deltatext, item_id, response_idA piece of the agent's reply as text. Append it to the agent's message for item_id.
assistant_audio_deltaaudio, item_idA piece of the agent's reply as base64 PCM16. Queue it for playback.
user_turnitem_id, previous_item_idThe visitor finished a spoken turn. Add a placeholder message for item_id, after previous_item_id. The text follows in user_transcript.
user_transcripttext, item_idWhat the visitor said. Fill in the message for item_id.
interruptednoneThe visitor started talking over the agent. Stop playback and clear queued audio at once.
assistant_truncateditem_id, textAfter 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_donenoneThe agent's reply is complete.
tool_callnameThe agent is running a tool. You can show a "working on it" state.
tool_resultname, successThe tool finished.
errormessage, optional codeSomething 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.

CodeMeaningWhat to do
1000The 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.
1008All your organization's voice lines are in use.Wait for a call to end, then retry.
1013Try again later. The voice agent or the server is temporarily unavailable.Reconnect after a short wait, with backoff.
4401The key is invalid or revoked, lacks widget:session, or the Origin is not allowed for the key.Check the key and its allowed domains.
4403The 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.
4404The agent was not found.Check agent_id and the region.
4429The key's rate limit was reached.Wait and retry. See Rate limits.
4503The agent service is switched off for a short time.Retry later.
tip

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.

Was this page helpful?