How to Add Voice to Your React App with ElevenLabs
Описание и применение
В этом руководстве показано, как добавить голосовой вывод в приложение React с помощью ElevenLabs, используя REST‑API, axios и пользовательский хук.
Задача
Integrate realistic text‑to‑speech into a React application.
Результат
The workflow yields a functional React component that converts user‑typed text into spoken audio using ElevenLabs, with clear loading and error handling.
Рабочий процесс
- Obtain an ElevenLabs API key via the sign‑up page and dashboard.
- Store the key in a .env file as REACT_APP_ELEVENLABS_API_KEY.
- Create a fresh React project (or use an existing one) and install axios.
- Add a service wrapper in src/api/elevenlabs.js that POSTs to /text-to-speech/{voiceId} and returns an object URL for the MP3 blob.
- Implement a custom hook src/hooks/useElevenLabs.js that manages loading, error, and audioUrl state and exposes a speak(text) function.
- Build a UI component that lets users type text and plays the returned audio URL via an
<audio>element. - Use the hook in the component to trigger synthesis and display loading/error feedback.
Доказательства
ElevenLabs – a modern TTS platform that offers ultra‑realistic speech, custom voice cloning, and a straightforward REST API.
We POST the text to /text-to-speech/{voiceId}. The response is an MP3 blob; we turn it into an object URL so the browser can play it instantly.
The hook returns: speak(text): triggers synthesis; audioUrl: a temporary URL you can feed to an <audio> element.
- Дата записи: 2026-11-01T02:43:55.321Z