Convert Audio to Text with Google Cloud Speech API

The Online Dictation app uses the HTML5 Speech Recognition API to transcribe your voice into digital text. If you have a pre-recorded audio file, you can turn on speech recognition inside Dictation, play the audio file and get the speech as text (see demo).

Google offers a Cloud Speech API for developers to convert audio to text. You can upload the audio file in FLAC format to Google Cloud storage and the speech API will transcribe the audio to text. If you have audio in MP3 format, use the FFMpeg tool for converting the audio to the desired format.

Also see: Cloud Speech API with Google Service Account

In this example, we upload the .flac audio file to Google Drive (for those who don’t have Google Cloud Storage) and call the Cloud Speech API via the UrlFetchApp service. You need to enable billing in your Google Cloud console, enable the Speech API and also setup an API Key or a service account.


Written by Amit Agarwal
twitter: @labnol


function convertAudioToText(flacFile, languageCode) {
  var file = DriveApp.getFilesByName(flacFile).next();
  var bytes = file.getBlob().getBytes();
  var payload = {
      encoding: "LINEAR16",
      sampleRate: 16000,
      languageCode: languageCode || "en-US"
    audio: {
      // You may also upload the audio file to Google 
      // Cloud Storage and pass the object URL here
  // Replace XYZ with your Cloud Speech API key
  var response = UrlFetchApp.fetch(
    "", {
      method: "POST",
      contentType: "application/json",
      payload: JSON.stringify(payload),
      muteHttpExceptions: true

Here’s another example that uses the CURL library to send speech recognition requests from the command line.

curl --silent --insecure --header "Content-Type: application/json"
--data @payload.json 

// Content of payload.json
    "config": {
        "sampleRate": 16000,
        "languageCode": "en-US"

    "audio": {