Skip to navigation

Cartesia

Agents WebSocket client for Cartesia Line voice agent scenarios.
View as Markdown

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

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

Contents

  • Usage: required module import and basic flow.
  • Factory functions: create the Cartesia Agents client.
  • Methods: start, DTMF, and custom agent messages.
  • Events: WebSocket media bridge events.
  • AgentsEvents: Cartesia Agents event names and payload fields.

Usage

Add the module before using the namespace:

require(Modules.Cartesia);

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

Factory functions

createAgentsClient

Creates a Cartesia.AgentsClient instance.

createAgentsClient(parameters: AgentsClientParameters): Promise<Cartesia.AgentsClient>

Parameters

ParameterTypeReq.Description
parametersAgentsClientParameters✓

Returns

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

createRealtimeTTSPlayer

Creates a new Cartesia.RealtimeTTSPlayer instance with the specified text (TTS is used to play the text). You can attach media streams later via the Cartesia.RealtimeTTSPlayer.sendMediaTo or VoxEngine.sendMediaBetween methods.

createRealtimeTTSPlayer(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 Cartesia.AgentsEvents or Cartesia.Events event. Use only functions as handlers; anything except a function leads to the error and scenario termination when a handler is called.

addEventListener(event: Cartesia.Events | Cartesia.AgentsEvents | string, callback: (event: object) => any): void

Parameters

ParameterTypeReq.Description
eventCartesia.Events | Cartesia.AgentsEvents | string✓Event constant or event name to subscribe to.
callback(event: object) => any✓Function called when the event is emitted.

Returns

TypeDescription
voidDoes not return a value.

clearMediaBuffer

Clears the Cartesia WebSocket media buffer.

clearMediaBuffer(parameters?: ClearMediaBufferParameters): void

Parameters

ParameterTypeReq.Description
parametersClearMediaBufferParameters✗

Returns

TypeDescription
voidDoes not return a value.

close

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

close(): void

Parameters

This method does not accept parameters.

Returns

TypeDescription
voidDoes not return a value.

custom

Sends custom metadata to the agent. https://docs.cartesia.ai/line/integrations/websocket-api#custom-event

custom(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject✓

Returns

TypeDescription
voidDoes not return a value.

dtmf

Sends DTMF (dual-tone multi-frequency) tones. https://docs.cartesia.ai/line/integrations/websocket-api#dtmf-event

dtmf(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject✓

Returns

TypeDescription
voidDoes not return a value.

id

Returns the AgentsClient id.

id(): string

Parameters

This method does not accept parameters.

Returns

TypeDescription
stringThe requested string value.

removeEventListener

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

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

Parameters

ParameterTypeReq.Description
eventCartesia.Events | Cartesia.AgentsEvents | string✓Event constant or event name to subscribe to.
callback(event: object) => any✗Function called when the event is emitted.

Returns

TypeDescription
voidDoes not return a value.

sendMediaTo

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

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

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit✓
parametersSendMediaParameters✗

Returns

TypeDescription
voidDoes not return a value.

start

Initializes the audio stream configuration. https://docs.cartesia.ai/line/integrations/websocket-api#start-event

start(parameters: Object): void

Parameters

ParameterTypeReq.Description
parametersObject✓

Returns

TypeDescription
voidDoes not return a value.

stopMediaTo

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

stopMediaTo(mediaUnit: VoxMediaUnit): void

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit✓

Returns

TypeDescription
voidDoes not return a value.

webSocketId

Returns the Cartesia WebSocket id.

webSocketId(): string

Parameters

This method does not accept parameters.

Returns

TypeDescription
stringThe requested string value.

Events

These events describe audio received through the Cartesia WebSocket media bridge.

Events.WebSocketMediaStarted

Triggered when the audio stream sent by a third party through a Cartesia WebSocket starts playing.

Event constant: Events.WebSocketMediaStarted

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
tagstring✗Special 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.
encodingstring✗Audio encoding formats.
customParameters{ [key: string]: string }✗Custom parameters.

Events.WebSocketMediaEnded

Triggered after the end of the audio stream sent by a third party through a Cartesia WebSocket (1 second of silence).

Event constant: Events.WebSocketMediaEnded

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
tagstring✗Special 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.
mediaInfoWebSocketMediaInfo✗Information 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 Cartesia Agents WebSocket API. The data field contains the provider event payload.

The unknown event.

Event constant: AgentsEvents.Unknown

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗This 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
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗This event payload is provider-specific. See the partner event documentation for the full payload shape.

Server acknowledgment of the start event, confirming stream configuration. https://docs.cartesia.ai/line/integrations/websocket-api#ack-event

Event constant: AgentsEvents.ACK

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗This event payload is provider-specific. See the partner event documentation for the full payload shape.

Indicates the agent wants to clear/interrupt the current audio stream. https://docs.cartesia.ai/line/integrations/websocket-api#clear-event

Event constant: AgentsEvents.Clear

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗This event payload is provider-specific. See the partner event documentation for the full payload shape.

Server sends DTMF tones from the agent. https://docs.cartesia.ai/line/integrations/websocket-api#dtmf-event-2

Event constant: AgentsEvents.DTMF

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗This event payload is provider-specific. See the partner event documentation for the full payload shape.

Server sends custom metadata from the agent. https://docs.cartesia.ai/line/integrations/websocket-api#custom-event-2

Event constant: AgentsEvents.Custom

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗The ‘payload’ parameter contains the event’s data.

The WebSocket error response event.

Event constant: AgentsEvents.WebSocketError

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗The ‘payload’ parameter contains the event’s data.

Contains information about connector.

Event constant: AgentsEvents.ConnectorInformation

Payload

FieldTypeReq.Description
clientAgentsClient✓The Cartesia.AgentsClient instance.
data{ customEvent?: string; payload?: Object; }✗The ‘payload’ parameter contains the event’s data.