React SDK
useScribe: real-time speech-to-text transcription in React
For an overview of Scribe and its capabilities, see the Speech to Text overview. For step-by-step usage guides, see Client-side streaming.
Installation
Use the ElevenLabs speech-to-text skill to transcribe audio from your AI coding assistant:
@elevenlabs/react re-exports everything from @elevenlabs/client, so you don’t need to install
both packages.
Usage
Here is a minimal working example that connects to Scribe and displays real-time transcription:
Getting a token
Scribe requires a single-use token for authentication. Create an API endpoint on your server:
Your ElevenLabs API key is sensitive. Never expose it to the client. Always generate the token on the server.
Hook options
Configure the hook with default options and callbacks:
Connection options
VAD options
These options control when transcripts are automatically committed when using the VAD commit strategy.
Audio options
The microphone object accepts:
Behavior options
Callbacks
All event callbacks are optional and can be provided as hook options:
- onConnect - handler called when the WebSocket connection is established.
- onDisconnect - handler called when the WebSocket connection is closed.
- onSessionStarted - handler called when the Scribe session starts.
- onPartialTranscript - handler called with interim transcription results. Receives
{ text: string }. - onCommittedTranscript - handler called with finalized transcription results. Receives
{ text: string }. - onCommittedTranscriptWithTimestamps - handler called with finalized transcription results including word-level timing. Receives
{ text: string; words?: { start: number; end: number }[] }. - onError - generic error handler for all errors. Receives
Error | Event. - onAuthError - handler called on authentication errors. Receives
{ error: string }.
Error callbacks
The generic onError callback fires for all errors. Specific error callbacks are also available for granular handling. All specific error callbacks receive { error: string }.
Microphone mode
Stream audio directly from the user’s microphone:
Manual audio mode (file transcription)
Transcribe pre-recorded audio files:
Return values
State
- status - current connection status:
"disconnected","connecting","connected","transcribing", or"error". - isConnected - boolean indicating if connected.
- isTranscribing - boolean indicating if actively transcribing.
- partialTranscript - current partial (interim) transcript string.
- committedTranscripts - array of
TranscriptSegmentobjects (see below). - error - current error message, or
null.
Each committed transcript segment has the following structure:
Methods
connect(options?)
Connect to Scribe. Options provided here override hook defaults:
disconnect()
Disconnect and clean up resources:
sendAudio(audioBase64, options?)
Send audio data (manual mode only):
The previousText field can only be sent in the first audio chunk of a session. Sending it in
subsequent chunks results in an error.
commit()
Manually commit the current transcription:
clearTranscripts()
Clear all transcripts from state:
getConnection()
Get the underlying connection instance:
Commit strategies
Control when transcriptions are committed:
For more details, see Transcripts and commit strategies.
Complete example
Here is a complete example of a React component using the useScribe hook with VAD-based commit strategy: