Ultravox
WebSocket API client for Ultravox real-time agent scenarios.
Ultravox provides a VoxEngine client for connecting a call or media unit to the Ultravox WebSocket API.
Use Ultravox.createWebSocketAPIClient(...) to create a WebSocketAPIClient for the current scenario.
Related guides
Contents
- Usage: required module import and basic flow.
- Factory functions: create the Ultravox WebSocket API client.
- Methods: client-to-server data messages and media helpers.
- Events: WebSocket media bridge events.
- WebSocketAPIEvents: Ultravox WebSocket API event names and payload fields.
Usage
Add the module before using the namespace:
Create the client, bridge media, and listen for Ultravox WebSocket API events.
Factory functions
createWebSocketAPIClient
Creates a new Ultravox.WebSocketAPIClient instance.
Parameters
Returns
WebSocketAPIClient
Methods
addEventListener
Adds a handler for the specified Ultravox.WebSocketAPIEvents or Ultravox.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 Ultravox WebSocket media buffer.
Parameters
Returns
clientToolResult
Contains the result of a client tool invocation. https://docs.ultravox.ai/apps/datamessages#clienttoolresult-and-dataconnectiontoolresult
Parameters
Returns
close
Closes the Ultravox connection (over WebSocket) or connection attempt.
Parameters
This method does not accept parameters.
Returns
dataConnectionToolResult
Contains the result of a client tool invocation. https://docs.ultravox.ai/apps/datamessages#clienttoolresult-and-dataconnectiontoolresult
Parameters
Returns
forcedAgentMessage
Forces the agent to say a specific message or invoke tools. https://docs.ultravox.ai/apps/datamessages#forcedagentmessage
Parameters
Returns
hangUp
Instructs the agent to end the call with an optional farewell message. https://docs.ultravox.ai/apps/datamessages#hangup
Parameters
Returns
id
Returns the WebSocketAPIClient id.
Parameters
This method does not accept parameters.
Returns
inputTextMessage
Deprecated
Used to send a user message to the agent via text. NOTE: this method is deprecated, use userTextMessage instead.
Parameters
Returns
removeEventListener
Removes a handler for the specified Ultravox.WebSocketAPIEvents or Ultravox.Events event.
Parameters
Returns
sendMediaTo
Starts sending media from the Ultravox (via WebSocket) to the media unit. Ultravox works in real time.
Parameters
Returns
setOutputMedium
Sets server’s output medium to text or voice. https://docs.ultravox.ai/datamessages#setoutputmedium
Parameters
Returns
stopMediaTo
Stops sending media from the Ultravox (via WebSocket) to the media unit.
Parameters
Returns
userTextMessage
A user message sent via text. The message appears to the agent as if it came from the user. https://docs.ultravox.ai/apps/datamessages#usertextmessage
Parameters
Returns
webSocketId
Returns the Ultravox WebSocket id.
Parameters
This method does not accept parameters.
Returns
Events
These events describe audio received through the Ultravox WebSocket media bridge.
WebSocketMediaStarted
Triggered when the audio stream sent by a third party through an Ultravox WebSocket is started playing.
Event constant: Events.WebSocketMediaStarted
Payload
WebSocketMediaEnded
Triggers after the end of the audio stream sent by a third party through an Ultravox WebSocket (1 second of silence).
Event constant: Events.WebSocketMediaEnded
Payload
WebSocketAPIEvents
These events mirror server messages from the Ultravox WebSocket API. The data field contains the provider event payload.
Unknown
The unknown event.
Event constant: WebSocketAPIEvents.Unknown
Payload
HTTPResponse
The HTTP response event.
Event constant: WebSocketAPIEvents.HTTPResponse
Payload
State
Indicates the server’s current state. https://docs.ultravox.ai/apps/datamessages#state
Event constant: WebSocketAPIEvents.State
Payload
Transcript
Contains text for an utterance made during the call. https://docs.ultravox.ai/apps/datamessages#transcript
Event constant: WebSocketAPIEvents.Transcript
Payload
ClientToolInvocation
Sent by the server to ask the client or data connection to invoke a tool with the given parameters. https://docs.ultravox.ai/apps/datamessages#clienttoolinvocation-and-dataconnectiontoolinvocation
Event constant: WebSocketAPIEvents.ClientToolInvocation
Payload
DataConnectionToolInvocation
Sent by the server to ask the client or data connection to invoke a tool with the given parameters. https://docs.ultravox.ai/apps/datamessages#clienttoolinvocation-and-dataconnectiontoolinvocation
Event constant: WebSocketAPIEvents.DataConnectionToolInvocation
Payload
Debug
Useful for application debugging. https://docs.ultravox.ai/apps/datamessages#debug
Event constant: WebSocketAPIEvents.Debug
Payload
PlaybackClearBuffer
Used to clear buffered output audio. WebSocket only. https://docs.ultravox.ai/apps/datamessages#playbackclearbuffer
Event constant: WebSocketAPIEvents.PlaybackClearBuffer
Payload
WebSocketError
The WebSocket error response event.
Event constant: WebSocketAPIEvents.WebSocketError
Payload
ConnectorInformation
Contains information about connector.
Event constant: WebSocketAPIEvents.ConnectorInformation
Payload