JavaScript SDK
Agents Platform SDK: deploy customized, interactive voice agents in minutes.
Installation
Install the package in your project through package manager.
Usage
This library is primarily meant for development in vanilla JavaScript projects, or as a base for libraries tailored to specific frameworks. It is recommended to check whether your specific framework has its own library. However, you can use this library in any JavaScript-based project.
Initialize conversation
First, initialize the Conversation instance:
This will kick off the websocket connection and start using microphone to communicate with the ElevenLabs Agents agent. Consider explaining and allowing microphone access in your apps UI before the Conversation kicks off:
Session configuration
The options passed to startSession
specify how the session is established. Conversations can be started with public or private agents.
Public agents
Agents that don’t require any authentication can be used to start a conversation by using the agent ID and the connection type. The agent ID can be acquired through the ElevenLabs UI.
For public agents, you can use the ID directly:
Private agents
If the conversation requires authorization, you will need to add a dedicated endpoint to your server that will either request a signed url (if using the WebSockets connection type) or a conversation token (if using WebRTC) using the ElevenLabs API and pass it back to the client.
Here’s an example for a WebSocket connection:
Here’s an example for WebRTC:
Once you have the token, providing it to startSession
will initiate the conversation using WebRTC.
Optional callbacks
The options passed to startSession
can also be used to register optional callbacks:
- onConnect - handler called when the conversation websocket connection is established.
- onDisconnect - handler called when the conversation websocket connection is ended.
- onMessage - handler called when a new text message is received. These can be tentative or final transcriptions of user voice, replies produced by LLM. Primarily used for handling conversation transcription.
- onError - handler called when an error is encountered.
- onStatusChange - handler called whenever connection status changes. Can be
connected
,connecting
anddisconnected
(initial). - onModeChange - handler called when a status changes, eg. agent switches from
speaking
tolistening
, or the other way around. - onCanSendFeedbackChange - handler called when sending feedback becomes available or unavailable.
Not all client events are enabled by default for an agent. If you have enabled a callback but aren’t seeing events come through, ensure that your ElevenLabs agent has the corresponding event enabled. You can do this in the “Advanced” tab of the agent settings in the ElevenLabs dashboard.
Return value
startSession
returns a Conversation
instance that can be used to control the session. The method will throw an error if the session cannot be established. This can happen if the user denies microphone access, or if the connection
fails.
endSession
A method to manually end the conversation. The method will end the conversation and disconnect from websocket. Afterwards the conversation instance will be unusable and can be safely discarded.
getId
A method returning the conversation ID.
setVolume
A method to set the output volume of the conversation. Accepts object with volume field between 0 and 1.
getInputVolume / getOutputVolume
Methods that return the current input/output volume on a scale from 0
to 1
where 0
is -100 dB and 1
is -30 dB.
sendFeedback
A method for sending binary feedback to the agent. The method accepts a boolean value, where true
represents positive feedback and false
negative feedback.
Feedback is always correlated to the most recent agent response and can be sent only once per response.
You can listen to onCanSendFeedbackChange
to know if feedback can be sent at the given moment.
sendContextualUpdate
A method to send contextual updates to the agent. This can be used to inform the agent about user actions that are not directly related to the conversation, but may influence the agent’s responses.
sendUserMessage
Sends a text message to the agent.
Can be used to let the user type in the message instead of using the microphone. Unlike sendContextualUpdate
, this will be treated as a user message and will prompt the agent to take its turn in the conversation.
sendUserActivity
Notifies the agent about user activity.
The agent will not attempt to speak for at least 2 seconds after the user activity is detected.
This can be used to prevent the agent from interrupting the user when they are typing.
setMicMuted
A method to mute/unmute the microphone.
changeInputDevice
Allows you to change the audio input device during an active voice conversation. This method is only available for voice conversations.
In WebRTC mode the input format and sample rate are hardcoded to pcm
and 48000
respectively.
Changing those values when changing the input device is a no-op.
If the device ID is invalid, the default device will be used instead.
changeOutputDevice
Allows you to change the audio output device during an active voice conversation. This method is only available for voice conversations.
In WebRTC mode the output format and sample rate are hardcoded to pcm
and 48000
respectively.
Changing those values when changing the output device is a no-op.
Device switching only works for voice conversations. If no specific deviceId
is provided, the
browser will use its default device selection. You can enumerate available devices using the
MediaDevices.enumerateDevices()
API.
getInputByteFrequencyData / getOutputByteFrequencyData
Methods that return Uint8Array
s containing the current input/output frequency data. See AnalyserNode.getByteFrequencyData for more information.
These methods are only available for voice conversations. In WebRTC mode the audio is hardcoded to
use pcm_48000
, meaning any visualization using the returned data might show different patterns
to WebSocket connections.