Get started
Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard. Please note that this feature is available for paid accounts only. If you’re on the free plan, you’ll need to upgrade. The entire source code of this guide can be viewed here.Step-by-step instructions
-
1
To use the microphone stream you need to install pyaudio. Mac and Linux users also need to install
portaudiofirst. Additionally, install thewebsocket-clientpackage:In your code, first setup the microphone stream and then establish a WebSocket connection with the streaming endpoint by using a WebSocket client and connecting towss://api.assemblyai.com/v2/realtime/ws. Authenticate your request by including your API key in the authorization header of your WebSocket connection, and provide the sample rate of your audio data as a query parameter to the streaming endpoint. -
2
Update the WebSocket’s
messageevent to load the incoming data as JSON and extract the text -
3
Update the WebSocket’s
messageevent to print the transcript, conditionally prepended with a string that signifies if the transcript is partial or final. -
4
Update the WebSocket’s
openevent to stream data from the microphone. -
5
Optional: Add up to 2,500 characters of custom vocabulary to your streaming session by including the
word_boostparameter as an optional query parameter in the URL. See also Adding Custom Vocabulary -
6
Update the WebSocket’s
errorevent to handle WebSocket errors and application-level errors, including bad sample rate, authentication failure, insufficient funds, and more. See also Closing and Status Codes for a list of errors. Additionally, update the WebSocket’scloseevent.
Audio Requirements
The raw audio data must comply with a strict encoding format. This is because we don’t do any transcoding to your data, we send it directly to the model for transcription to reduce latency. The encoding of your audio must be in:- 16-bit signed integer PCM or mu-law encoding
- A sample rate that matches the value of the
sample_ratequery param you supply - Single-channel
- 100 to 2000 milliseconds of audio per message
Audio segments with a duration between 100 ms and 450 ms produce the best results in transcription accuracy.
Specifying the encoding
By default, transcriptions expect PCM16 encoding. If you want to use mu-law encoding, you must set theencoding parameter to pcm_mulaw:
Request Types
These are the types of requests that can be sent to the WebSocket API.Opening a Session
When opening a Session you can pass the following query attributes to the WebSocket URL:sample_rate
The sample rate of the streamed audio.
Example: wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000
word_boost
See also Adding Custom Vocabulary
encoding
See also Specifying the encoding
token
See also Creating Temporary Authentication Tokens
Sending Audio
When sending audio over the WebSocket connection, you can use the websocket’s binary mode to send raw audio data. This can be the raw data recorded directly from a microphone or read from an audio file.Sending
audio_data via JSON is also supported but will be deprecated in the future. Use the binary mode instead.Terminating a Session
When you’ve completed your session, clients should send a JSON message with the following field.
After requesting session termination, the server will send the remaining transcript messages, followed by a SessionTerminated message.
Response Types
These are the types of responses that can be received from the WebSocket API.Session Start
Once your request is authorized and connection established, your client receives aSessionBegins message with the following JSON data:
Transcripts
Our Streaming Speech-to-Text pipeline uses a two-phase transcription strategy, broken into partial and final results.Partial Transcripts
As you send audio data to the API, the API immediately starts responding with Partial Results. The following keys are returned from the WebSocket API.Final Transcripts
After you’ve received your partial results, our model continues to analyze incoming audio and, when it detects the end of an “utterance” (usually a pause in speech), it’ll finalize the results sent to you so far with higher accuracy, as well as add punctuation and casing to the transcription text. The following keys are returned from the WebSocket API when Final Results are sent:Session Terminated
After requesting session termination, the server will send the remaining transcript messages, followed by aSessionTerminated message. Your client receives a SessionTerminated message with the following JSON data:
Closing and Status Codes
The WebSocket specification provides standard errors. Our API provides application-level WebSocket errors for well-known scenarios:Quotas and Limits
The following limits are imposed to ensure performance and service quality.- Idle Sessions - Sessions that don’t receive audio within 1 minute will be terminated.
- Session Limit - 100 sessions at a time for paid users. Please contact us if you need to increase this limit. Free-tier users must upgrade their account to use real-time streaming.
- Session Uniqueness - Only one WebSocket per session.
- Audio Sampling Rate Limit - Customers must send data in near real-time. If a client sends data faster than 1 second of audio per second for longer than 1 minute, we’ll terminate the session.
Adding Custom Vocabulary
Developers can also add up to 2500 characters of custom vocabulary to their real-time session by adding the optional query parameterword_boost in the URL. The parameter should map to a JSON encoded list of strings as shown in this Python example:
Creating Temporary Authentication Tokens
If you need to authenticate on the client, you can avoid exposing your API key by using temporary authentication tokens. Temporary tokens have a one-time use restriction. To generate a temporary token, send aPOST request to https://api.assemblyai.com/v2/realtime/token. Use the expires_in parameter to specify how long the token should be valid for, in seconds.
The
expires_in parameter must have a value between 60 and 360000 seconds.wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000&token={New Temp Token}. For example:
Conclusion
Streaming Speech-to-Text is a powerful feature with even more powerful possibilities for integration. On the AssemblyAI blog, you can learn about using Streaming Speech-to-Text to:- Automatically Transcribe Zoom Calls in Real Time
- Transcribe Twilio Phone Calls
- Connect to the Streaming Speech-to-Text API using a PyAudio stream