Conversational AI SDK: deploy customized, interactive voice agents in minutes.

Installation

Install the package in your project through package manager.

$npm install @11labs/react
># or
>yarn add @11labs/react
># or
>pnpm install @11labs/react

Usage

useConversation

React hook for managing websocket connection and audio usage for ElevenLabs Conversational AI.

Initialize conversation

First, initialize the Conversation instance.

1const conversation = useConversation();

Note that Conversational AI requires microphone access. Consider explaining and allowing access in your apps UI before the Conversation kicks off.

1// call after explaning to the user why the microphone access is needed
2await navigator.mediaDevices.getUserMedia();

Options

The Conversation can be initialized with certain options. Those are all optional.

1const conversation = useConversation({
2 /* options object */
3});
  • 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

startConversation

startConversation method kick off the websocket connection and starts using microphone to communicate with the ElevenLabs Conversational AI agent.
The method accepts options object, with the url or agentId option being required.

Agent ID can be acquired through ElevenLabs UI and is always necessary.

1const conversation = useConversation();
2const conversationId = await conversation.startSession({ url });

For the public agents, define agentId - no signed link generation necessary.

In case the conversation requires authorization, use the REST API to generate signed links. Use the signed link as a url parameter.

startSession returns promise resolving to conversationId. The value is a globally unique conversation ID you can use to identify separate conversations.

1// your server
2const requestHeaders: HeadersInit = new Headers();
3requestHeaders.set("xi-api-key", process.env.XI_API_KEY); // use your ElevenLabs API key
4
5const response = await fetch(
6 "https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id={{agent id created through ElevenLabs UI}}",
7 {
8 method: "GET",
9 headers: requestHeaders,
10 }
11);
12
13if (!response.ok) {
14 return Response.error();
15}
16
17const body = await response.json();
18const url = body.signed_url; // use this URL for startConversation method.

endSession

A method to manually end the conversation. The method will end the conversation and disconnect from websocket.

1await conversation.endSession();

setVolume

A method to set the output volume of the conversation. Accepts object with volume field between 0 and 1.

1await conversation.setVolume({ volume: 0.5 });

status

A React state containing the current status of the conversation.

1const { status } = useConversation();
2console.log(status); // "connected" or "disconnected"

isSpeaking

A React state containing the information of whether the agent is currently speaking. This is helpful for indicating the mode in your UI.

1const { isSpeaking } = useConversation();
2console.log(isSpeaking); // boolean