npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@11labs/react

v0.0.1

Published

ElevenLabs React Library

Downloads

5,027

Readme

ElevenLabs React Library

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.

LOGO Discord Twitter

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.

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();

Options

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

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

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.
endConversation

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

await conversation.endSession();
setVolume

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 });
status

A React state containing the current status of the conversation.

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

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

Development

Please, refer to the README.md file in the root of this repository.

Contributing

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.