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:

1require(Modules.Cartesia);

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

Factory functions

createAgentsClient

Creates a Cartesia.AgentsClient instance.

1createAgentsClient(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.

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 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.

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

Parameters

ParameterTypeReq.Description
eventCartesia.Events | Cartesia.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 Cartesia WebSocket media buffer.

1clearMediaBuffer(parameters?: ClearMediaBufferParameters): void

Parameters

ParameterTypeReq.Description
parametersClearMediaBufferParameters

Returns

TypeDescription
voidDoes not return a value.

close

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

1close(): 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/web-calls#custom-event

1custom(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/web-calls#dtmf-event

1dtmf(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 Cartesia.AgentsEvents or Cartesia.Events event.

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

Parameters

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

1sendMediaTo(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/web-calls#start-event

1start(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.

1stopMediaTo(mediaUnit: VoxMediaUnit): void

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit

Returns

TypeDescription
voidDoes not return a value.

webSocketId

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

WebSocketMediaStarted

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

Event constant: Events.WebSocketMediaStarted

Payload

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

Event constant: Events.WebSocketMediaEnded

Payload

FieldTypeReq.Description
clientAgentsClientThe Cartesia.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 Cartesia Agents WebSocket API. The data field contains the provider event payload.

The unknown event.

Event constant: AgentsEvents.Unknown

Payload

FieldTypeReq.Description
clientAgentsClientThe Cartesia.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 Cartesia.AgentsClient instance.
dataObjectThis 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/web-calls#ack-event

Event constant: AgentsEvents.ACK

Payload

FieldTypeReq.Description
clientAgentsClientThe Cartesia.AgentsClient instance.
dataObjectThis 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/web-calls#clear-event

Event constant: AgentsEvents.Clear

Payload

FieldTypeReq.Description
clientAgentsClientThe Cartesia.AgentsClient instance.
dataObjectThis 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/web-calls#dtmf-event-2

Event constant: AgentsEvents.DTMF

Payload

FieldTypeReq.Description
clientAgentsClientThe Cartesia.AgentsClient instance.
dataObjectThis 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/web-calls#custom-event-2

Event constant: AgentsEvents.Custom

Payload

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

The WebSocket error response event.

Event constant: AgentsEvents.WebSocketError

Payload

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

Contains information about connector.

Event constant: AgentsEvents.ConnectorInformation

Payload

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