ElevenLabs

Agents WebSocket client for ElevenLabs conversational AI scenarios.
View as Markdown

ElevenLabs provides a VoxEngine client for connecting a call or media unit to the ElevenLabs Agents WebSocket API.

Use ElevenLabs.createAgentsClient(...) to create an AgentsClient for the current scenario.

Contents

  • Usage: required module import and basic flow.
  • Factory functions: create the ElevenLabs Agents client.
  • Methods: conversation, tools, and contextual client messages.
  • Events: WebSocket media bridge events.
  • AgentsEvents: ElevenLabs Agents event names and payload fields.

Usage

Add the module before using the namespace:

1require(Modules.ElevenLabs);

Create the client, bridge media, and listen for ElevenLabs Agents events.

Factory functions

createAgentsClient

Creates a new ElevenLabs.AgentsClient instance.

1createAgentsClient(parameters: AgentsClientParameters): Promise<ElevenLabs.AgentsClient>

Parameters

ParameterTypeReq.Description
parametersAgentsClientParameters

Returns

TypeDescription
Promise<ElevenLabs.AgentsClient>Resolves to the ElevenLabs.AgentsClient instance.

createRealtimeTTSPlayer

Creates a new ElevenLabs.RealtimeTTSPlayer instance with the specified text (TTS is used to play the text). You can attach media streams later via the ElevenLabs.RealtimeTTSPlayer.sendMediaTo or VoxEngine.sendMediaBetween methods. NOTE: this method uses 11labs initializeConnection(https://elevenlabs.io/docs/api-reference/text-to-speech/v-1-text-to-speech-voice-id-stream-input#send.initializeConnection) method internally.

1createRealtimeTTSPlayer(text: string, parameters?: RealtimeTTSPlayerParameters): RealtimeTTSPlayer

Parameters

ParameterTypeReq.Description
textstring
parametersRealtimeTTSPlayerParameters

Returns

TypeDescription
RealtimeTTSPlayerThe requested RealtimeTTSPlayer value.

AgentsClient

Methods

addEventListener

Adds a handler for the specified ElevenLabs.AgentsEvents or ElevenLabs.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: ElevenLabs.Events | ElevenLabs.AgentsEvents | string, callback: (event: object) => any): void

Parameters

ParameterTypeReq.Description
eventElevenLabs.Events | ElevenLabs.AgentsEvents | 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 ElevenLabs WebSocket media buffer.

1clearMediaBuffer(parameters?: ClearMediaBufferParameters): void

Parameters

ParameterTypeReq.Description
parametersClearMediaBufferParameters

Returns

TypeDescription
voidDoes not return a value.

clientToolResult

Result of the client tool call. https://elevenlabs.io/docs/agents-platform/api-reference/agents-platform/websocket#send.Client-Tool-Result

1clientToolResult(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

close

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

1close(): void

Parameters

This method does not accept parameters.

Returns

TypeDescription
voidDoes not return a value.

contextualUpdate

Allows to send non-interrupting background information to the conversation. https://elevenlabs.io/docs/agents-platform/api-reference/agents-platform/websocket#send.Contextual-Update

1contextualUpdate(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

conversationInitiationClientData

Defines what can be customized when starting a conversation. https://elevenlabs.io/docs/agents-platform/api-reference/agents-platform/websocket#send.Conversation-Initiation-Client-Data

1conversationInitiationClientData(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

id

Returns the AgentsClient id.

1id(): string

Parameters

This method does not accept parameters.

Returns

TypeDescription
stringThe requested string value.

removeEventListener

Removes a handler for the specified ElevenLabs.AgentsEvents or ElevenLabs.Events event.

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

Parameters

ParameterTypeReq.Description
eventElevenLabs.Events | ElevenLabs.AgentsEvents | 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 ElevenLabs (via WebSocket) to the media unit. ElevenLabs works in real time.

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

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit
parametersSendMediaParameters

Returns

TypeDescription
voidDoes not return a value.

stopMediaTo

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

1stopMediaTo(mediaUnit: VoxMediaUnit): void

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit

Returns

TypeDescription
voidDoes not return a value.

userMessage

Allows to send user text messages to the conversation. https://elevenlabs.io/docs/agents-platform/customization/events/client-to-server-events#user-messages

1userMessage(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject

Returns

TypeDescription
voidDoes not return a value.

webSocketId

Returns the ElevenLabs 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 ElevenLabs WebSocket media bridge.

WebSocketMediaStarted

Triggered when the audio stream sent by a third party through an ElevenLabs WebSocket is started playing.

Event constant: Events.WebSocketMediaStarted

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient 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 ElevenLabs WebSocket (1 second of silence).

Event constant: Events.WebSocketMediaEnded

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient 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).

AgentsEvents

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

The unknown event.

Event constant: AgentsEvents.Unknown

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

The HTTP response event.

Event constant: AgentsEvents.HTTPResponse

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

Automatically sent when starting a conversation. Initializes conversation settings and parameters. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#conversation_initiation_metadata

Event constant: AgentsEvents.ConversationInitiationMetadata

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectInitial connection metadata See the partner event documentation.
data.type”conversation_initiation_metadata”Provider-defined field.
data.conversation_initiation_metadata_eventunknownProvider-defined field.

Example data:

1{
2 "type": "conversation_initiation_metadata"
3}

Health check event requiring immediate response. Used to maintain WebSocket connection. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#ping

Event constant: AgentsEvents.Ping

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectKeepalive ping from server See the partner event documentation.
data.type"ping"Provider-defined field.
data.ping_eventunknownProvider-defined field.

Example data:

1{
2 "type": "ping"
3}

Contains finalized speech-to-text results. Represents complete user utterances. Used for conversation history. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#user_transcript

Event constant: AgentsEvents.UserTranscript

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectFinal transcription of user speech See the partner event documentation.
data.type”user_transcript”Provider-defined field.
data.user_transcription_eventunknownProvider-defined field.

Example data:

1{
2 "type": "user_transcript"
3}

Contains complete agent message. Sent with first audio chunk. Used for display and history. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#agent_response

Event constant: AgentsEvents.AgentResponse

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectAgent’s text response See the partner event documentation.
data.type”agent_response”Provider-defined field.
data.agent_response_eventunknownProvider-defined field.

Example data:

1{
2 "type": "agent_response"
3}

Contains truncated response after interruption. Updates displayed message. Maintains conversation accuracy. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#agent_response_correction

Event constant: AgentsEvents.AgentResponseCorrection

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectCorrected response after interruption See the partner event documentation.
data.type”agent_response_correction”Provider-defined field.
data.agent_response_correction_eventunknownProvider-defined field.

Example data:

1{
2 "type": "agent_response_correction"
3}

Contains event id of interrupted event. https://elevenlabs.io/docs/agents-platform/api-reference/agents-platform/websocket#receive.Interruption

Event constant: AgentsEvents.Interruption

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectUser interrupted agent speech See the partner event documentation.
data.type”interruption”Provider-defined field.
data.interruption_eventunknownProvider-defined field.

Example data:

1{
2 "type": "interruption"
3}

Contains contextual information to be added to the conversation state. https://elevenlabs.io/docs/agents-platform/api-reference/agents-platform/websocket#receive.Contextual-Update

Event constant: AgentsEvents.ContextualUpdate

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectThe event’s data.

Represents a function call the agent wants the client to execute. Contains tool name, tool call ID, and parameters. Requires client-side execution of the function and sending the result back to the server. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#client_tool_call

Event constant: AgentsEvents.ClientToolCall

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectTool for client to execute See the partner event documentation.
data.type”client_tool_call”Provider-defined field.
data.client_tool_callunknownProvider-defined field.

Example data:

1{
2 "type": "client_tool_call"
3}

Voice Activity Detection score event. Indicates the probability that the user is speaking. Values range from 0 to 1, where higher values indicate higher confidence of speech. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#vad_score

Event constant: AgentsEvents.VadScore

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectThe event’s data.

Contains preliminary text from the agent. https://elevenlabs.io/docs/agents-platform/api-reference/agents-platform/websocket#receive.Internal-Tentative-Agent-Response

Event constant: AgentsEvents.InternalTentativeAgentResponse

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectInternal tentative agent response (not for public use) See the partner event documentation.
data.type”internal_tentative_agent_response”Provider-defined field.
data.tentative_agent_response_internal_eventunknownProvider-defined field.

Example data:

1{
2 "type": "internal_tentative_agent_response"
3}

The WebSocket error response event.

Event constant: AgentsEvents.WebSocketError

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectThe event’s data.

Contains information about connector.

Event constant: AgentsEvents.ConnectorInformation

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectThe event’s data.

Indicates when the agent has executed a tool function. Contains tool metadata and execution status. Provides visibility into agent tool usage during conversations. https://elevenlabs.io/docs/agents-platform/customization/events/client-events#agent_tool_response

Event constant: AgentsEvents.AgentToolResponse

Payload

FieldTypeReq.Description
clientAgentsClientThe ElevenLabs.AgentsClient instance.
dataObjectResult of agent tool execution See the partner event documentation.
data.type”agent_tool_response”Provider-defined field.
data.agent_tool_responseunknownProvider-defined field.

Example data:

1{
2 "type": "agent_tool_response"
3}