
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@11labs/react
Advanced tools
An SDK library for using ElevenLabs in React based applications. If you're looking for a Node.js library, please refer to the ElevenLabs Node.js Library.
Note that this library is launching to primarily support Conversational AI. The support for speech synthesis and other more generic use cases is planned for the future.
Install the package in your project through package manager.
npm install @11labs/react
# or
yarn add @11labs/react
# or
pnpm install @11labs/react
React hook for managing websocket connection and audio usage for ElevenLabs Conversational AI.
First, initialize the Conversation instance.
const conversation = useConversation();
Note that Conversational AI requires microphone access. Consider explaining and allowing access in your apps UI before the Conversation kicks off.
// call after explaning to the user why the microphone access is needed
await navigator.mediaDevices.getUserMedia();
The Conversation can be initialized with certain options. Those are all optional.
const conversation = useConversation({/* options object */});
Client tools are a way to enabled 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.
const conversation = useConversation({
clientTools: {
displayMessage: (parameters: {text: string}) => {
alert(text);
return "Message displayed";
}
}
});
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.
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:
const conversation = useConversation({
overrides: {
agent: {
prompt: {
prompt: "My custom prompt",
},
firstMessage: "My custom first message",
language: "en",
},
tts: {
voiceId: "custom voice id"
},
},
});
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.
const conversation = useConversation();
const 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.
// your server
const requestHeaders: HeadersInit = new Headers();
requestHeaders.set("xi-api-key", process.env.XI_API_KEY); // use your ElevenLabs API key
const response = await fetch(
"https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id={{agent id created through ElevenLabs UI}}",
{
method: "GET",
headers: requestHeaders,
}
);
if (!response.ok) {
return Response.error();
}
const body = await response.json();
const url = body.signed_url; // use this URL for startConversation method.
A method to manually end the conversation. The method will end the conversation and disconnect from websocket.
await conversation.endSession();
A method to set the output volume of the conversation. Accepts object with volume field between 0 and 1.
await conversation.setVolume({ volume: 0.5 });
A React state containing the current status of the conversation.
const { status } = useConversation();
console.log(status); // "connected" or "disconnected"
A React state containing the information of whether the agent is currently speaking. This is helpful for indicating the mode in your UI.
const { isSpeaking } = useConversation();
console.log(isSpeaking); // boolean
Please, refer to the README.md file in the root of this repository.
Please, create an issue first to discuss the proposed changes. Any contributions are welcome!
Remember, if merged, your code will be used as part of a MIT licensed project. By submitting a Pull Request, you are giving your consent for your code to be integrated into this library.
FAQs
ElevenLabs React Library
The npm package @11labs/react receives a total of 9,353 weekly downloads. As such, @11labs/react popularity was classified as popular.
We found that @11labs/react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.