Deepgram

Voice agent client for Deepgram Voice Agent WebSocket scenarios.
View as Markdown

Deepgram provides a VoxEngine client for connecting a call or media unit to the Deepgram Voice Agent API over WebSocket.

Use Deepgram.createVoiceAgentClient(...) to create a VoiceAgentClient for the current scenario.

Contents

  • Usage: required module import and basic flow.
  • Factory functions: create the Deepgram Voice Agent client.
  • Methods: media and conversation control methods.
  • Events: WebSocket media bridge events.
  • VoiceAgentEvents: Deepgram Voice Agent event names and payload fields.

Usage

Add the module before using the namespace:

1require(Modules.Deepgram);

Create the client, bridge media, and listen for Voice Agent events.

Factory functions

createVoiceAgentClient

Creates a Deepgram.VoiceAgentClient instance.

1createVoiceAgentClient(parameters: VoiceAgentClientParameters): Promise<VoiceAgentClient>

Parameters

ParameterTypeReq.Description
parametersVoiceAgentClientParameters

Returns

TypeDescription
Promise<VoiceAgentClient>Resolves to the Deepgram.VoiceAgentClient instance.

VoiceAgentClient

Methods

addEventListener

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

Parameters

ParameterTypeReq.Description
eventEvents | VoiceAgentEvents | 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 VoiceAgent WebSocket media buffer.

1clearMediaBuffer(parameters?: ClearMediaBufferParameters): void

Parameters

ParameterTypeReq.Description
parametersClearMediaBufferParameters

Returns

TypeDescription
voidDoes not return a value.

close

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

1close(): void

Parameters

This method does not accept parameters.

Returns

TypeDescription
voidDoes not return a value.

id

Returns the VoiceAgentClient id.

1id(): string

Parameters

This method does not accept parameters.

Returns

TypeDescription
stringThe requested string value.

removeEventListener

Removes a handler for the specified Deepgram.VoiceAgentEvents or Deepgram.Events event.

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

Parameters

ParameterTypeReq.Description
eventEvents | VoiceAgentEvents | 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.

sendFunctionCallResponse

Send a message to provide a function call response in the middle of a conversation. https://developers.deepgram.com/reference/voice-agent/voice-agent#send.AgentV1SendFunctionCallResponse

1sendFunctionCallResponse(input: Object): void

Parameters

ParameterTypeReq.Description
inputObject

Returns

TypeDescription
voidDoes not return a value.

sendInjectAgentMessage

Send a message to immediately trigger an Agent statement. https://developers.deepgram.com/reference/voice-agent/voice-agent#send.AgentV1InjectAgentMessage

1sendInjectAgentMessage(input: Object): void

Parameters

ParameterTypeReq.Description
inputObject

Returns

TypeDescription
voidDoes not return a value.

sendInjectUserMessage

Send a text based message to the agent. https://developers.deepgram.com/reference/voice-agent/voice-agent#send.AgentV1InjectUserMessage

1sendInjectUserMessage(input: Object): void

Parameters

ParameterTypeReq.Description
inputObject

Returns

TypeDescription
voidDoes not return a value.

sendMediaTo

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

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

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit
parametersSendMediaParameters

Returns

TypeDescription
voidDoes not return a value.

sendUpdatePrompt

Send a message to update the system prompt of the agent. https://developers.deepgram.com/reference/voice-agent/voice-agent#send.AgentV1UpdatePrompt

1sendUpdatePrompt(input: Object): void

Parameters

ParameterTypeReq.Description
inputObject

Returns

TypeDescription
voidDoes not return a value.

sendUpdateSpeak

Send a message to change the Speak model in the middle of a conversation. https://developers.deepgram.com/reference/voice-agent/voice-agent#send.AgentV1UpdateSpeak

1sendUpdateSpeak(input: Object): void

Parameters

ParameterTypeReq.Description
inputObject

Returns

TypeDescription
voidDoes not return a value.

stopMediaTo

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

1stopMediaTo(mediaUnit: VoxMediaUnit): void

Parameters

ParameterTypeReq.Description
mediaUnitVoxMediaUnit

Returns

TypeDescription
voidDoes not return a value.

webSocketId

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

WebSocketMediaStarted

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

Event constant: Events.WebSocketMediaStarted

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient 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 Deepgram WebSocket (1 second of silence).

Event constant: Events.WebSocketMediaEnded

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient 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).

VoiceAgentEvents

These events mirror server messages from the Deepgram Voice Agent API. The data field contains the provider event payload.

The unknown event.

Event constant: VoiceAgentEvents.Unknown

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectThis event payload is provider-specific. See the partner event documentation for the full payload shape.

The HTTP response event.

Event constant: VoiceAgentEvents.HTTPResponse

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectThe event’s data.

Receive a welcome message from the server to confirm the websocket has opened. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1Welcome

Event constant: VoiceAgentEvents.Welcome

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive welcome message from Deepgram’s Voice Agent API See the partner event documentation.
data.type"Welcome"Message type identifier for welcome message
data.request_idstringUnique identifier for the request

Example data:

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

Confirms the server has successfully received and applied the Settings message. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1SettingsApplied

Event constant: VoiceAgentEvents.SettingsApplied

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive settings applied message from Deepgram’s Voice Agent API See the partner event documentation.
data.type”SettingsApplied”Message type identifier for settings applied confirmation

Example data:

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

Facilitates real-time communication by relaying spoken statements from both the user and the agent. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1ConversationText

Event constant: VoiceAgentEvents.ConversationText

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive conversation text from Deepgram’s Voice Agent API See the partner event documentation.
data.type”ConversationText”Message type identifier for conversation text
data.roleunknownIdentifies who spoke the statement
data.contentstringThe actual statement that was spoken
data.languages_hintedstring[]The language hints that were active at the time of the turn. Only present on user-role messages when the listen model is flux-general-multi. Optional.
data.languagesstring[]Languages detected in the user’s speech, sorted by word count (descending). Only present on user-role messages when the listen model is flux-general-multi. Optional.

Example data:

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

Notifies the client that the user has begun speaking. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1UserStartedSpeaking

Event constant: VoiceAgentEvents.UserStartedSpeaking

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive user started speaking message from Deepgram’s Voice Agent API See the partner event documentation.
data.type”UserStartedSpeaking”Message type identifier indicating that the user has begun speaking

Example data:

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

Informs the client when the agent is processing information. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1AgentThinking

Event constant: VoiceAgentEvents.AgentThinking

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive agent thinking message from Deepgram’s Voice Agent API See the partner event documentation.
data.type”AgentThinking”Message type identifier for agent thinking
data.contentstringThe text of the agent’s thought process

Example data:

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

Server-initiated message requesting a function call, to be handled by either client or server. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1FunctionCallRequest

Event constant: VoiceAgentEvents.FunctionCallRequest

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive function call request from Deepgram’s Voice Agent API See the partner event documentation.
data.type”FunctionCallRequest”Message type identifier for function call requests
data.functionsunknown[]Array of functions to be called

Example data:

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

Message containing the result of a function call, sent by client or server. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1ReceiveFunctionCallResponse

Event constant: VoiceAgentEvents.FunctionCallResponse

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectThe event’s data.

Confirms that a Prompt Configuration change has been applied. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1PromptUpdated

Event constant: VoiceAgentEvents.PromptUpdated

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive prompt update from Deepgram’s Voice Agent API See the partner event documentation.
data.type”PromptUpdated”Message type identifier for prompt update confirmation

Example data:

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

Confirms that a Speak Configuration change has been applied. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1SpeakUpdated

Event constant: VoiceAgentEvents.SpeakUpdated

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive speak update from Deepgram’s Voice Agent API See the partner event documentation.
data.type”SpeakUpdated”Message type identifier for speak update confirmation

Example data:

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

Get signals that the server has finished sending the final audio segment to the client. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1AgentAudioDone

Event constant: VoiceAgentEvents.AgentAudioDone

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive agent audio done message from Deepgram’s Voice Agent API See the partner event documentation.
data.type”AgentAudioDone”Message type identifier indicating the agent has finished sending audio

Example data:

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

Receive errors from the server if an issue has occurred. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1Error

Event constant: VoiceAgentEvents.Error

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive error response from Deepgram’s Voice Agent API See the partner event documentation.
data.typeunknownMessage type identifier for error responses
data.descriptionstringA description of what went wrong
data.codestringError code identifying the type of error

Example data:

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

Receive warnings from the server if an issue has occurred. https://developers.deepgram.com/reference/voice-agent/voice-agent#receive.AgentV1Warning

Event constant: VoiceAgentEvents.Warning

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive warning messages from Deepgram’s Voice Agent API See the partner event documentation.
data.typeunknownMessage type identifier for warnings
data.descriptionstringDescription of the warning
data.codestringWarning code identifier

Example data:

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

Provide conversation and function call history when starting a new Voice Agent session. https://developers.deepgram.com/docs/voice-agent-history

Event constant: VoiceAgentEvents.History

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectReceive a conversation history message from Deepgram’s Voice Agent API. Each message is either a conversation text (with role and content) or a function call record (with function_calls array). See the partner event documentation.
data.typestringEvent type identifier.

Example data:

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

The WebSocket error response event.

Event constant: VoiceAgentEvents.WebSocketError

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectThe event’s data.

Contains information about connector.

Event constant: VoiceAgentEvents.ConnectorInformation

Payload

FieldTypeReq.Description
clientVoiceAgentClientThe Deepgram.VoiceAgentClient instance.
dataObjectThe event’s data.