Ultravox

WebSocket API client for Ultravox real-time agent scenarios.

View as Markdown

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.

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:

1require(Modules.Ultravox);

Create the client, bridge media, and listen for Ultravox WebSocket API events.

Factory functions

createWebSocketAPIClient

Creates a new Ultravox.WebSocketAPIClient instance.

1createWebSocketAPIClient(parameters: WebSocketAPIClientParameters): Promise<Ultravox.WebSocketAPIClient>

Parameters

ParameterTypeReq.Description
parametersWebSocketAPIClientParameters

Returns

TypeDescription
Promise<Ultravox.WebSocketAPIClient>Resolves to the Ultravox.WebSocketAPIClient instance.

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.

1addEventListener(event: Ultravox.Events | Ultravox.WebSocketAPIEvents | string, callback: (event: object) => any): void

Parameters

ParameterTypeReq.Description
eventUltravox.Events | Ultravox.WebSocketAPIEvents | stringEvent constant or event name to subscribe to.
callback(event: object) => anyFunction called when the event is emitted.

Returns

TypeDescription
voidDoes not return a value.

clearMediaBuffer

Clears the Ultravox WebSocket media buffer.

1clearMediaBuffer(parameters?: ClearMediaBufferParameters): void

Parameters

ParameterTypeReq.Description
parametersClearMediaBufferParameters

Returns

TypeDescription
voidDoes not return a value.

clientToolResult

Contains the result of a client tool invocation. https://docs.ultravox.ai/apps/datamessages#clienttoolresult-and-dataconnectiontoolresult

1clientToolResult(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

close

Closes the Ultravox connection (over WebSocket) or connection attempt.

1close(): void

Parameters

This method does not accept parameters.

Returns

TypeDescription
voidDoes not return a value.

dataConnectionToolResult

Contains the result of a client tool invocation. https://docs.ultravox.ai/apps/datamessages#clienttoolresult-and-dataconnectiontoolresult

1dataConnectionToolResult(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

forcedAgentMessage

Forces the agent to say a specific message or invoke tools. https://docs.ultravox.ai/apps/datamessages#forcedagentmessage

1forcedAgentMessage(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

hangUp

Instructs the agent to end the call with an optional farewell message. https://docs.ultravox.ai/apps/datamessages#hangup

1hangUp(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

id

Returns the WebSocketAPIClient id.

1id(): string

Parameters

This method does not accept parameters.

Returns

TypeDescription
stringThe requested string value.

inputTextMessage

Deprecated

Used to send a user message to the agent via text. NOTE: this method is deprecated, use userTextMessage instead.

1inputTextMessage(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

removeEventListener

Removes a handler for the specified Ultravox.WebSocketAPIEvents or Ultravox.Events event.

1removeEventListener(event: Ultravox.Events | Ultravox.WebSocketAPIEvents | string, callback?: (event: object) => any): void

Parameters

ParameterTypeReq.Description
eventUltravox.Events | Ultravox.WebSocketAPIEvents | stringEvent constant or event name to subscribe to.
callback(event: object) => anyFunction called when the event is emitted.

Returns

TypeDescription
voidDoes not return a value.

sendMediaTo

Starts sending media from the Ultravox (via WebSocket) to the media unit. Ultravox works in real time.

1sendMediaTo(mediaUnit: VoxMediaUnit, parameters?: SendMediaParameters): void

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit
parametersSendMediaParameters

Returns

TypeDescription
voidDoes not return a value.

setOutputMedium

Sets server’s output medium to text or voice. https://docs.ultravox.ai/datamessages#setoutputmedium

1setOutputMedium(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

stopMediaTo

Stops sending media from the Ultravox (via WebSocket) to the media unit.

1stopMediaTo(mediaUnit: VoxMediaUnit): void

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit

Returns

TypeDescription
voidDoes not return a value.

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

1userTextMessage(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

webSocketId

Returns the Ultravox WebSocket id.

1webSocketId(): string

Parameters

This method does not accept parameters.

Returns

TypeDescription
stringThe requested string value.

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

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
tagstringSpecial tag to name audio streams sent over one WebSocket connection. With it, one can send 2 audios to 2 different media units at the same time.
encodingstringAudio encoding formats.
customParameters{ [key: string]: string }Custom parameters.

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

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
tagstringSpecial tag to name audio streams sent over one WebSocket connection. With it, one can send 2 audios to 2 different media units at the same time.
mediaInfoWebSocketMediaInfoInformation about the audio stream that can be obtained after the stream stops or pauses (1 second of silence).

WebSocketAPIEvents

These events mirror server messages from the Ultravox WebSocket API. The data field contains the provider event payload.

The unknown event.

Event constant: WebSocketAPIEvents.Unknown

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

The HTTP response event.

Event constant: WebSocketAPIEvents.HTTPResponse

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

Indicates the server’s current state. https://docs.ultravox.ai/apps/datamessages#state

Event constant: WebSocketAPIEvents.State

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

Contains text for an utterance made during the call. https://docs.ultravox.ai/apps/datamessages#transcript

Event constant: WebSocketAPIEvents.Transcript

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

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

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

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

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThe event’s data.

Useful for application debugging. https://docs.ultravox.ai/apps/datamessages#debug

Event constant: WebSocketAPIEvents.Debug

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThe event’s data.

Used to clear buffered output audio. WebSocket only. https://docs.ultravox.ai/apps/datamessages#playbackclearbuffer

Event constant: WebSocketAPIEvents.PlaybackClearBuffer

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThe event’s data.

The WebSocket error response event.

Event constant: WebSocketAPIEvents.WebSocketError

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThe event’s data.

Contains information about connector.

Event constant: WebSocketAPIEvents.ConnectorInformation

Payload

FieldTypeReq.Description
clientWebSocketAPIClientThe Ultravox.WebSocketAPIClient instance.
dataObjectThe event’s data.