Question · Speech recognition
How do you use the Google Speech-to-Text API?
Enable the Speech-to-Text API in a Google Cloud project with billing turned on, sign in with the gcloud CLI so your code finds credentials, install the google-cloud-speech package, and call the v2 client with a short audio file. Synchronous calls take 1 minute of audio or less, so use the asynchronous method for longer recordings.
Updated October 17, 20263 min read9 cited facts, each traced to its source
To use the Google Speech-to-Text API, you set up a Google Cloud project, enable the API, sign in locally, install one Python package and make a call. The documented route uses local credentials rather than an API key, and the simplest call takes audio of 1 minute or less 1. The steps below follow Google's own documentation. We read the sample code but did not run it against a live project, so test it on a short file first. The speech-to-text API guide explains where Google fits among the other providers, and the overview of speech recognition covers the subject more broadly.
Set up the project, the API and local credentials first
- Check billing. Google's quickstart asks you to verify that billing is enabled for your Google Cloud project 2.
- Enable the API. The same quickstart says to enable the Speech-to-Text APIs in your project if they are not already enabled 3.
- Install the gcloud CLI and initialize it. Google's setup says to run
gcloud init4. - Create local credentials. On a local computer, the quickstart says to create local authentication credentials for your user account with
gcloud auth application-default login5. You do not need this step in Cloud Shell. - Install the Python package. On Mac and Linux, Google's Python reference gives
pip install google-cloud-speech6. We suggest a virtual environment, so the package stays with this project.
A Python call transcribes a local WAV file
The code below uses the v2 client and the chirp_3 model with the language set to en-US. That follows Google's sample for a local audio file, which sets the language to en-US and the model to chirp_3 7. Replace the project ID with your own.
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
PROJECT_ID = "your-project-id"
client = SpeechClient()
with open("resources/audio.wav", "rb") as f:
audio_content = f.read()
config = cloud_speech.RecognitionConfig(
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
language_codes= ["en-US"],
model="chirp_3",
)
request = cloud_speech.RecognizeRequest(
recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
config=config,
content=audio_content,
)
response = client.recognize(request=request)
for result in response.results:
print(f"Transcript: {result.alternatives [0].transcript}")
Check the current rate for the model you pick, because the sample's model and the price table can change. The pricing question sets out the rates.
Synchronous calls take 1 minute of audio or less
The code above is a synchronous call. Google says that synchronous recognition requests process audio of 1 minute or less 1, so keep each file under that length or split it first. For longer recordings, Google's overview describes an asynchronous method, which takes longer audio and returns results later.
You do not need an API key for this route
People who search for a Google speech-to-text API key often expect a key to paste into the code. On a local computer, the gcloud login from step 4 creates credentials for your user account 5, and the sample creates its client with no key in the file.
A standard Google API key is a different credential. Google says a standard API key can be used with any API that accepts API keys, unless the key has API restrictions 8. We did not find a Speech-to-Text page that shows a key being used, so if you want one, check the service's page before you create it. We did not cover service accounts here.
What to do next
- Run the sample on a short WAV file you recorded, and confirm that the transcript prints.
- Measure accuracy on a few minutes of your own audio with word error rate, and compare the result with a reference you wrote yourself.
- Opting into data logging lowers Google's V1 standard price from $0.024 to $0.016 per minute 9, so decide on data logging before you go live. The pricing question explains the trade-off and lists the free minutes.
- If you are still choosing a provider, the free options for speech-to-text APIs and the OpenAI price question list the other allowances and rates.
Evidence
Every number on this page, with where it comes from. Open a fact to see the exact words in its source.
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
Go deeper
Related questions
- Does Gemini have a speech-to-text API?
- How do you get a Google Speech-to-Text API key?
- How do you use the Azure Speech-to-Text API?
- How much does the ElevenLabs speech-to-text API cost, and how do you use it?
- How much does the OpenAI speech-to-text API cost?
- Is ElevenLabs speech to text better than Whisper?
- Is Google Speech-to-Text free?
- Is there a free speech-to-text API?
Referenced by
- Speech Recognition: What It Is and How Well It WorksTopic
- Speech-to-Text APIs Compared: Price, Accuracy, SpeedGuide
- Does Gemini have a speech-to-text API?Question
- How do you get a Google Speech-to-Text API key?Question
- How do you use the Azure Speech-to-Text API?Question
- Is Google Speech-to-Text free?Question
- Is there a free speech-to-text API?Question
- What is IBM Watson Speech to Text?Question