XAI
XAI provides VoxEngine clients for the xAI Grok Voice Agent API and Realtime TTS over WebSocket.
Use XAI.createVoiceAgentAPIClient(...) to create a VoiceAgentAPIClient for voice-agent scenarios. After the client is created, call methods such as sendMediaTo, responseCreate, and addEventListener on that client instance.
Use XAI.createRealtimeTTSPlayer(...) when you only need realtime text-to-speech playback.
Related guides
Contents
- Usage: required module import and basic flow.
- Factory functions: create the XAI Voice Agent client.
- VoiceAgentAPIClientParameters: API key, tracing, privacy, and WebSocket options.
- VoiceAgentAPIClient: runtime client object returned by the factory.
- Methods: media, response, and connection control methods.
- Events: WebSocket media start and end payloads.
- VoiceAgentAPIEvents: xAI Voice Agent API event names and payload fields.
Usage
Add the module before using the namespace:
Create the client, bridge media, and listen for both WebSocket media events and Voice Agent API events.
Factory functions
createRealtimeTTSPlayer
Creates a new XAI.RealtimeTTSPlayer instance. You can attach media streams later via the XAI.RealtimeTTSPlayer.sendMediaTo or VoxEngine.sendMediaBetween methods.
The optional parameters object is typed as XAI.RealtimeTTS.
Parameters
Returns
createVoiceAgentAPIClient
Creates a new XAI.VoiceAgentAPIClient instance.
The required parameters object is typed as XAI.VoiceAgent.
Parameters
Returns
VoiceAgentAPIClient
Methods
addEventListener
Adds a handler for the specified XAI.VoiceAgentAPIEvents or XAI.Events event. Use only functions as handlers; anything except a function leads to the error and scenario termination when a handler is called.
Parameters
Returns
clearMediaBuffer
Clears the XAI WebSocket media buffer.
Parameters
Returns
close
Closes the XAI connection (over WebSocket) or connection attempt.
Parameters
This method does not accept parameters.
Returns
conversationItemCreate
Create a new conversation item. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.create
Parameters
Returns
Example parameters:
conversationItemDelete
Delete a conversation item by ID. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.delete
Parameters
Returns
conversationItemTruncate
Truncate a previous assistant audio message item. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.truncate
Parameters
Returns
id
Returns the VoiceAgentAPIClient id.
Parameters
This method does not accept parameters.
Returns
inputAudioBufferClear
Clear input audio buffer. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.clear
Parameters
Returns
Example parameters:
inputAudioBufferCommit
Commit the input audio buffer as a user message. Only available when turn_detection type is null. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.commit
Parameters
Returns
removeEventListener
Removes a handler for the specified XAI.VoiceAgentAPIEvents or XAI.Events event.
Parameters
Returns
responseCancel
Send this event to cancel an in-progress response. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.cancel
Parameters
Returns
responseCreate
Request the server to create a new assistant response when using client side vad. (This is handled automatically when using server side vad.) Prefer nested { response: { ... } } body per xAI docs; flat fields are also accepted by the connector. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.create
Parameters
Returns
Example parameters:
sendMediaTo
Starts sending media from the XAI (via WebSocket) to the media unit. XAI works in real time.
Parameters
Returns
sessionUpdate
Send this event to update the session’s configuration. https://docs.x.ai/developers/rest-api-reference/inference/voice#session.update
Parameters
Returns
Example parameters:
stopMediaTo
Stops sending media from the XAI (via WebSocket) to the media unit.
Parameters
Returns
webSocketId
Returns the XAI WebSocket id.
Parameters
This method does not accept parameters.
Returns
Events
These events describe audio received through the XAI WebSocket media bridge.
Events.WebSocketMediaStarted
Triggered when the audio stream sent by a third party through an xAI WebSocket starts playing.
Event constant: Events.WebSocketMediaStarted
Payload
Events.WebSocketMediaEnded
Triggered after the end of the audio stream sent by a third party through an xAI WebSocket (1 second of silence).
Event constant: Events.WebSocketMediaEnded
Payload
VoiceAgentAPIEvents
These events mirror server messages from the xAI Voice Agent API. The data field contains the provider event payload.
All VoiceAgentAPIEvents callbacks receive these common fields:
Per-event payload tables below show only event-specific fields (and any provider payload enrichment for data).
Unknown
The unknown event.
Event constant: VoiceAgentAPIEvents.Unknown
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
Error
Sent when an error occurs. Contains error code and message. https://docs.x.ai/developers/rest-api-reference/inference/voice#error
Event constant: VoiceAgentAPIEvents.Error
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
SessionCreated
Returned when a session is created. https://docs.x.ai/developers/rest-api-reference/inference/voice#session.created
Event constant: VoiceAgentAPIEvents.SessionCreated
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ConversationCreated
The first message at connection. Notifies the client that a conversation session has been created. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.created
Event constant: VoiceAgentAPIEvents.ConversationCreated
Payload
Example data:
SessionUpdated
Acknowledge the client’s “session.update” message that the session has been updated. https://docs.x.ai/developers/rest-api-reference/inference/voice#session.updated
Event constant: VoiceAgentAPIEvents.SessionUpdated
Payload
Example data:
ConversationItemAdded
Responding to the client that a new user message has been added to conversation history, or if an assistance response has been added to conversation history. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.added
Event constant: VoiceAgentAPIEvents.ConversationItemAdded
Payload
Example data:
ConversationItemDeleted
Returned when an item in the conversation is deleted. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.deleted
Event constant: VoiceAgentAPIEvents.ConversationItemDeleted
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ConversationItemTruncated
Returned when a conversation item is truncated. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.truncated
Event constant: VoiceAgentAPIEvents.ConversationItemTruncated
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ConversationItemInputAudioTranscriptionCompleted
Notify the client the audio transcription for input has been completed. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.input_audio_transcription.completed
Event constant: VoiceAgentAPIEvents.ConversationItemInputAudioTranscriptionCompleted
Payload
Example data:
ConversationItemInputAudioTranscriptionUpdated
Returned when input audio transcription is updated. https://docs.x.ai/developers/rest-api-reference/inference/voice#conversation.item.input_audio_transcription.updated
Event constant: VoiceAgentAPIEvents.ConversationItemInputAudioTranscriptionUpdated
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
InputAudioBufferCommitted
Input audio buffer has been committed. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.committed
Event constant: VoiceAgentAPIEvents.InputAudioBufferCommitted
Payload
Example data:
InputAudioBufferCleared
Input audio buffer has been cleared. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.cleared
Event constant: VoiceAgentAPIEvents.InputAudioBufferCleared
Payload
Example data:
InputAudioBufferSpeechStarted
Notify the client the server’s VAD has detected the start of a speech. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.speech_started
Event constant: VoiceAgentAPIEvents.InputAudioBufferSpeechStarted
Payload
Example data:
InputAudioBufferSpeechStopped
Notify the client the server’s VAD has detected the end of a speech. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.speech_stopped
Event constant: VoiceAgentAPIEvents.InputAudioBufferSpeechStopped
Payload
Example data:
InputAudioBufferTimeoutTriggered
Returned when the input audio buffer idle timeout is triggered. https://docs.x.ai/developers/rest-api-reference/inference/voice#input_audio_buffer.timeout_triggered
Event constant: VoiceAgentAPIEvents.InputAudioBufferTimeoutTriggered
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseCreated
A new assistant response turn is in progress. Audio delta created from this assistant turn will have the same response id. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.created
Event constant: VoiceAgentAPIEvents.ResponseCreated
Payload
Example data:
ResponseDone
The assistant’s response is completed. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.done
Event constant: VoiceAgentAPIEvents.ResponseDone
Payload
Example data:
ResponseOutputItemAdded
A new assistant response is added to message history. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_item.added
Event constant: VoiceAgentAPIEvents.ResponseOutputItemAdded
Payload
Example data:
ResponseOutputItemDone
A new assistant response is done. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_item.done
Event constant: VoiceAgentAPIEvents.ResponseOutputItemDone
Payload
Example data:
ResponseOutputTextDelta
Text delta of the assistant response. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_text.delta
Event constant: VoiceAgentAPIEvents.ResponseOutputTextDelta
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseOutputTextDone
The text of the assistant response has finished generating. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_text.done
Event constant: VoiceAgentAPIEvents.ResponseOutputTextDone
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseOutputAudioTranscriptDelta
Audio transcript delta of the assistant response. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_audio_transcript.delta
Event constant: VoiceAgentAPIEvents.ResponseOutputAudioTranscriptDelta
Payload
Example data:
ResponseOutputAudioTranscriptDone
The audio transcript delta of the assistant response has finished generating. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_audio_transcript.done
Event constant: VoiceAgentAPIEvents.ResponseOutputAudioTranscriptDone
Payload
Example data:
ResponseOutputAudioDone
Notifies client that the audio for this turn has finished generating. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.output_audio.done
Event constant: VoiceAgentAPIEvents.ResponseOutputAudioDone
Payload
Example data:
ResponseContentPartAdded
Notifies client that the content part added. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.content_part.added
Event constant: VoiceAgentAPIEvents.ResponseContentPartAdded
Payload
Example data:
ResponseContentPartDone
Notifies client that the content part done. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.content_part.done
Event constant: VoiceAgentAPIEvents.ResponseContentPartDone
Payload
Example data:
ResponseFunctionCallArgumentsDelta
Function call arguments delta. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.function_call_arguments.delta
Event constant: VoiceAgentAPIEvents.ResponseFunctionCallArgumentsDelta
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseFunctionCallArgumentsDone
Function call triggered with complete arguments. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.function_call_arguments.done
Event constant: VoiceAgentAPIEvents.ResponseFunctionCallArgumentsDone
Payload
Example data:
ResponseMCPCallArgumentsDelta
MCP call arguments delta. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_call_arguments.delta
Event constant: VoiceAgentAPIEvents.ResponseMCPCallArgumentsDelta
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseMCPCallArgumentsDone
MCP call arguments completed. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_call_arguments.done
Event constant: VoiceAgentAPIEvents.ResponseMCPCallArgumentsDone
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseMCPCallInProgress
MCP call is in progress. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_call.in_progress
Event constant: VoiceAgentAPIEvents.ResponseMCPCallInProgress
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseMCPCallCompleted
MCP call completed. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_call.completed
Event constant: VoiceAgentAPIEvents.ResponseMCPCallCompleted
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ResponseMCPCallFailed
MCP call failed. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_call.failed
Event constant: VoiceAgentAPIEvents.ResponseMCPCallFailed
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
MCPListToolsInProgress
MCP list tools is in progress. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_list_tools.in_progress
Event constant: VoiceAgentAPIEvents.MCPListToolsInProgress
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
MCPListToolsCompleted
MCP list tools completed. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_list_tools.completed
Event constant: VoiceAgentAPIEvents.MCPListToolsCompleted
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
MCPListToolsFailed
MCP list tools failed. https://docs.x.ai/developers/rest-api-reference/inference/voice#response.mcp_list_tools.failed
Event constant: VoiceAgentAPIEvents.MCPListToolsFailed
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
WebSocketError
The WebSocket error response event.
Event constant: VoiceAgentAPIEvents.WebSocketError
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.
ConnectorInformation
Contains information about connector.
Event constant: VoiceAgentAPIEvents.ConnectorInformation
Payload
No event-specific payload columns are listed here; this callback still receives the common client and data fields. For data, see the partner documentation for the exact JSON shape.