React SDK
Conversational AI SDK: deploy customized, interactive voice agents in minutes.
Refer to the Conversational AI overview for an explanation of how Conversational AI works.
Installation
Install the package in your project through package manager.
Usage
useConversation
A React hook for managing connection and audio usage for ElevenLabs Conversational AI.
Initialize conversation
First, initialize the Conversation instance.
Note that Conversational AI requires microphone access. Consider explaining and allowing access in your app’s UI before the Conversation starts.
Options
The Conversation can be initialized with certain options
- 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.
Methods
startSession
The startConversation
method kicks off the WebSocket or WebRTC connection and starts using the microphone to communicate with the ElevenLabs Conversational AI 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.
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.