Cross-platform Voice Agents with Expo React Native
Introduction
In this tutorial you will learn how to build a voice agent that works across iOS and Android using Expo React Native and the ElevenLabs React Native SDK with WebRTC support.
Prefer to jump straight to the code?
Find the example project on GitHub.
Requirements
- An ElevenLabs account with an API key.
- Node.js v18 or higher installed on your machine.
Setup
Create a new Expo project
Using create-expo-app
, create a new blank Expo project:
Install dependencies
Install the ElevenLabs React Native SDK and its dependencies:
If you’re running into an issue with peer dependencies, please add a .npmrc
file in the root of
the project with the following content: legacy-peer-deps=true
.
Enable microphone permissions and add Expo plugins
In the app.json
file, add the following permissions:
This will allow the React Native to prompt for microphone permissions when the conversation is started.
Note
For Android emulator you will need to enable “Virtual microphone uses host audio input” in the emulator microphone settings.
Add ElevenLabs Conversational AI to your app
Add the ElevenLabs Conversational AI to your app by adding the following code to your ./App.tsx
file:
Native client tools
A big part of building conversational AI agents is allowing the agent access and execute functionality dynamically. This can be done via client tools.
Create a new file to hold your client tools: ./utils/tools.ts
and add the following code:
Dynamic variables
In addition to the client tools, we’re also injecting the platform (web, iOS, Android) as a dynamic variable both into the first message, and the prompt:
Agent configuration
Create a new agent
Navigate to Conversational AI > Agents and create a new agent from the blank template.
Set up the client tools
Set up the following client tools:
- Name:
getBatteryLevel
- Description: Gets the device battery level as decimal point percentage.
- Wait for response:
true
- Response timeout (seconds): 3
- Name:
changeBrightness
- Description: Changes the brightness of the device screen.
- Wait for response:
true
- Response timeout (seconds): 3
- Parameters:
- Data Type:
number
- Identifier:
brightness
- Required:
true
- Value Type:
LLM Prompt
- Description: A number between 0 and 1, inclusive, representing the desired screen brightness.
- Data Type:
- Name:
flashScreen
- Description: Quickly flashes the screen on and off.
- Wait for response:
true
- Response timeout (seconds): 3
Run the app
This app requires some native dependencies that aren’t supported in Expo Go, therefore you will need to prebuild the app and then run it on a native device.
- Terminal 1:
- Run
npx expo prebuild --clean
- Run
- Run
npx expo start --tunnel
to start the Expo development server over https.
- Terminal 2:
- Run
npx expo run:ios --device
to run the app on your iOS device.
- Run