React SDK
Agents Platform SDK: deploy customized, interactive voice agents in minutes.
Refer to the Agents Platform overview for an explanation of how Agents Platform works.
Installation
Install the package in your project through package manager.
Usage
useConversation
A React hook for managing connection and audio usage for ElevenLabs Agents.
Initialize conversation
First, initialize the Conversation instance.
Note that Agents Platform requires microphone access. Consider explaining and allowing access in your app’s UI before the Conversation starts.
Options
The Conversation can be optionally initialized with certain parameters.
Options include:
- clientTools - object definition for client tools that can be invoked by agent. See below for details.
- overrides - object definition conversations settings overrides. See below for details.
- textOnly - whether the conversation should run in text-only mode. See below for details.
- serverLocation - specify the server location (
"us"
,"eu-residency"
,"in-residency"
,"global"
). Defaults to"us"
.
Callbacks Overview
- 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 message is received. These can be tentative or final transcriptions of user voice, replies produced by LLM, or debug message when a debug option is enabled.
- onError - handler called when a error is encountered.
- onAudio - handler called when audio data is received.
- onModeChange - handler called when the conversation mode changes (speaking/listening).
- onStatusChange - handler called when the connection status changes.
- onCanSendFeedbackChange - handler called when the ability to send feedback changes.
- onDebug - handler called when debug information is available.
- onUnhandledClientToolCall - handler called when an unhandled client tool call is encountered.
- onVadScore - handler called when voice activity detection score changes.
Client Tools
Client tools are a way to enable agent to invoke client-side functionality. This can be used to trigger actions in the client, such as opening a modal or doing an API call on behalf of the user.
Client tools definition is an object of functions, and needs to be identical with your configuration within the ElevenLabs UI, where you can name and describe different tools, as well as set up the parameters passed by the agent.
In case function returns a value, it will be passed back to the agent as a response.
Note that the tool needs to be explicitly set to be blocking conversation in ElevenLabs UI for the agent to await and react to the response, otherwise agent assumes success and continues the conversation.
Conversation overrides
You may choose to override various settings of the conversation and set them dynamically based other user interactions.
We support overriding various settings. These settings are optional and can be used to customize the conversation experience.
The following settings are available:
Text only
If your agent is configured to run in text-only mode, i.e. it does not send or receive audio messages, you can use this flag to use a lighter version of the conversation. In that case, the user will not be asked for microphone permissions and no audio context will be created.
Controlled State
You can control certain aspects of the conversation state directly through the hook options:
Data residency
You can specify which ElevenLabs server region to connect to. For more information see the data residency guide.
Methods
startSession
The startConversation
method kicks off the WebSocket or WebRTC connection and starts using the microphone to communicate with the ElevenLabs Agents agent. The method accepts an options object, with the signedUrl
, conversationToken
or agentId
option being required.
The Agent ID can be acquired through ElevenLabs UI.
We also recommended passing in your own end user IDs to map conversations to your users.
For public agents (i.e. agents that don’t have authentication enabled), only the agentId
is required.
In case the conversation requires authorization, use the REST API to generate signed links for a WebSocket connection or a conversation token for a WebRTC connection.
startSession
returns a promise resolving a conversationId
. The value is a globally unique conversation ID you can use to identify separate conversations.
WebSocket connection
WebRTC connection
endSession
A method to manually end the conversation. The method will disconnect and end the conversation.
setVolume
Sets the output volume of the conversation. Accepts an object with a volume
field between 0 and 1.
status
A React state containing the current status of the conversation.
isSpeaking
A React state containing information on whether the agent is currently speaking. This is useful for indicating agent status in your UI.
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.
sendContextualUpdate
Sends contextual information to the agent that won’t trigger a response.
sendFeedback
Provide feedback on the conversation quality. This helps improve the agent’s performance.
sendUserActivity
Notifies the agent about user activity to prevent interruptions. Useful for when the user is actively using the app and the agent should pause speaking, i.e. when the user is typing in a chat.
The agent will pause speaking for ~2 seconds after receiving this signal.
canSendFeedback
A React state indicating whether feedback can be submitted for the current conversation.
changeInputDevice
Switch the audio input device during an active voice conversation. This method is only available for voice conversations.
changeOutputDevice
Switch the audio output device during an active voice conversation. This method is only available for voice conversations.
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.
getId
Returns the current conversation ID.
getInputVolume / getOutputVolume
Methods that return the current input/output volume levels (0-1 scale).
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.
sendMCPToolApprovalResult
Sends approval result for MCP (Model Context Protocol) tool calls.