Skip to content

Instantly share code, notes, and snippets.

Created February 3, 2012 13:20
Show Gist options
  • Save alotaiba/1730160 to your computer and use it in GitHub Desktop.
Save alotaiba/1730160 to your computer and use it in GitHub Desktop.
Google Speech To Text API

Google Speech To Text API

Base URL:
It accepts POST requests with voice file encoded in FLAC format, and query parameters for control.

Query Parameters

The client's name you're connecting from. For spoofing purposes, let's use chromium

Speech language, for example, ar-QA for Qatari Arabic, or en-US for U.S. English

Maximum results to return for utterance


Should contain FLAC formatted voice binary

HTTP Header

Should be audio/x-flac; rate=16000;, where MIME and sample rate of the FLAC file is included

Can be the client's user agent string, for spoofing purposes, we'll use Chrome's


These examples assume you have a voice file encoded in FLAC called alsalam-alikum.flac.


This will save JSON response in a file called recognized.json

wget --post-file='alsalam-alikum.flac' \
--user-agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header='Content-Type: audio/x-flac; rate=16000;' \
-O 'recognized.json' \


curl -X POST \
--data-binary @alsalam-alikum.flac \
--user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header 'Content-Type: audio/x-flac; rate=16000;' \
Copy link

Kaspler commented May 22, 2014


I'm in same situation as you - need to finish my BS project till the end of May.
I also was working fine with V1 till now... Anyway, now, thanks to this page and all the comments here I managed to make it work with V2.

Here is my C# code (it's just a prototype where I tested, so it's simple):

using System;
using System.IO;
using System.IO.Compression;
using System.Net;

class Program
    static void Main(string[] args)
        using (var fileStream = new FileStream(@"C:\LetsArrangeMeeting.flac", FileMode.Open))
            const string requestUrl = "";
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);
            var requestStream = request.GetRequestStream();

            CopyStream(fileStream, requestStream);

            using (var response = request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                    using (var zippedStream = new GZipStream(responseStream, CompressionMode.Decompress))
                        using (var sr = new StreamReader(zippedStream))
                            var res = sr.ReadToEnd();


    private static void CopyStream(FileStream fileStream, Stream requestStream)
        var buffer = new byte[32768];
        int read;
        while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            requestStream.Write(buffer, 0, read);

    private static void ConfigureRequest(HttpWebRequest request)
        request.KeepAlive = true;
        request.SendChunked = true;
        request.ContentType = "audio/x-flac; rate=44100";
        request.UserAgent =
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
        request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-GB,en-US;q=0.8,en;q=0.6");
        request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        request.Method = "POST";

And this is the result:

{"result":[{"alternative":[{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock"},{"transcript":"hello lets arrange a meeting in Tel Aviv at 5 o'clock"},{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock Buy"},{"transcript":"hello lips arrange a meeting in Tel Aviv at 5 o'clock"},{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock by"},{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock buy"}],"final":true}],"result_index":0}

Hope this helps!

Copy link

ecmnet commented May 22, 2014

V2 seems to be down. None of my API keys work. Same experience?

Copy link

Everything seems normal for me.

Copy link

ghost commented May 24, 2014

I also only got an empty response. Does anyone know how to fix this?

Copy link

mudler commented May 24, 2014

empty response too here, i'm digging on the chromium code, but no luck for now.
Edit: seems pfilter is doing the trick, gillesdemey/google-speech-v2#6 (comment)

Copy link

@NSThread how did you get the key? Is there any constraints of limited number of use of the key? I have used the following link to get a key "" but it is not working.

Copy link

hauhhvn commented May 27, 2014

I want to use more than 50 requests / day. How to do it?

Copy link

Same as hauhhvn! Why doesn't Google have commercial offerings for this API (at least, I haven't seen them)? When I try to extend quota in my developer's cabinet, some private Google doc is about to be shown, two times I requested permission to read it, but no further reaction!

Copy link

pannous commented Jun 2, 2014

<title>Error 403 (Forbidden)!!1</title> original google response "!!1" lol

Copy link

wildroo commented Jun 12, 2014

Working java code:

//libs to import
import java.nio.file.Files;
import java.nio.file.Paths;

 * Send post to google
private void sendPost() throws Exception {
    String USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2",
    url = "";

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    // add reuqest header
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Content-Type", "audio/l16; rate=16000");
    con.setRequestProperty("AcceptEncoding", "gzip,deflate,sdch");

    // Send post request
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {

    // print result


Copy link

yochze commented Jun 13, 2014

Here's my working Ruby code.

        url = ''
        uri = URI.parse(url)
        http =, uri.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE    
        request =
        request.body ="your_flac_file.flac}")
        request.content_type = 'audio/x-flac; rate=16000;'
        res = http.request(request)

BTW - Does anybody know what's the time limit for audio file?

Copy link

bezerko commented Jun 28, 2014

I thought I would share this for those who just get back:

I was setting the "rate" in the Content-Type HTTP header to 16000 that had always worked in V1. Changing this to 44100 fixed the issue:

"Content-type": "audio/x-flac; rate=44100"

Copy link

LRNAB commented Jul 2, 2014

Does anyone know how to get the key?

Copy link

voicer commented Jul 3, 2014

The key for google api you can get in the google APIs console for free.
But the question is how to increase the max limit of 50 requests 'a day?
I found no way to get more, and the link to order quote is dead.

The Google corp. forces me and other developers to find any other way to solve this problem.
I see the solution in java script for Chrome browser. But if anybody need this API i.e. for C# then he must to switch the source of sound from microphone to the file stream in flac format.

My question is now - how to change the source of sound from microphone into binary file on disk?

Copy link

Could you tell if there is a way to use java script for Chrome browser from terminal? I realize it is a little bit strange question, but I can't find a way to use unlimited access to google speech API from linux using annyang.

Copy link

smithas commented Aug 8, 2014

I went through the docs for creating key but it gives me forbidden error 403. I had tried a key few weeks ago shared by someone here which had worked but now that also fails. What step am I missing that I get 403 error? Is there any other way. Please help.

Copy link

Anyone got it working in Java with V2?? I am getting 403 error.
Also looking at the comments looks like it only converts 10 to 15 secs of audio ... Anyway we can extend that time limit to say one minute or even longer??

Copy link

amd5200 commented Oct 11, 2014

I changed to use pocketsphinx, it can be works offline and supports Chinese language.

Copy link

marfnk commented Oct 16, 2014

I also have issues with the authentication for Google Speech2Text API on Android.
It works with my API key in WiFi if I tell Google my current IP. But the Android Key isn't working. I provided the correct debug-keystore SHA1 but am receiving: The client does not have permission to get url ... from this server. I use the Apache HttpClient within my Android application. Any ideas?

@ijazsarwar This Google API is not intended to continuously recognize speech. Its for short commands. Thats why there is this few seconds limit. Take a look at my master thesis project where I try to send 15sec chunks to Google to get something continuous-like.

@amd5200 I'd love to talk to you about PocketSphinx on Andoird for generic vocabulary searches since I already tried unsuccessfully to get it to work as described here - you'll find my email in my github profile.

Copy link

amd5200 commented Oct 27, 2014

@marfnk I just used it on my single board, like raspberry pi, and for Chinese. ( )
I never tried PocketSphinx on Andoird, so i got no idea for this.

Copy link

IS this possible to use ajax to post in to Google Speech API ?

Copy link

Hello everybody,

Some time ago I created this shell script which packages everything you need to use the API v2 (record data for a given duration or use a file, specify language, filter out results, etc.):

For more details about the reverse engineering being used, check out this article:


Copy link

rogo21 commented Feb 4, 2016

how to divide audio into frames of 15 sec using java code or command line??

Copy link

ghost commented Jul 21, 2016

Hi, -> 400. That’s an error.
I am in Canada

Copy link

HI, Sending 'POST' request to URL :
Response Code : 200

I am getting above code.. But no output of recorded file to be convert in to text. How do I get that ?

Copy link

IvanZhao commented Sep 5, 2016

Hi everybody,
When I was "POST".
I receive the error message like this:
"code": 403,
"errors": [
"domain": "global",
"message": "Requests from this Android client application are blocked.",
"reason": "forbidden"
"message": "Requests from this Android client application are blocked.",
Dose any buddy knows how to resolve this problem?

Copy link

indrabayu commented Nov 24, 2016

    public static async Task<string> RequestGoogleSpeechAPIAsync(byte[] byteArray) 
        var httpClient = new HttpClient();
        var mediaType = new MediaTypeWithQualityHeaderValue("audio/x-flac");
        var parameter = new NameValueHeaderValue("rate", "16000");

        var url = "";
        var appSettings = ConfigurationManager.AppSettings;
        var apiKey = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw";
        var uri = new Uri(url + apiKey);

        using (MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length))
            var param = new StreamContent(ms);
            param.Headers.ContentType = mediaType;

            var result = await httpClient.PostAsync(uri, param);

            var responseFromServer = await result.Content.ReadAsStringAsync();
            var responseArray = responseFromServer.Split('\n');
            var responseJson = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<SpajamHonsen.Models.GoogleSpeechAPIResponseModel.Resuls>(responceArray[1]));

            return responseJson.result[0].alternative[0].transcript;

Copy link

fj4870 commented Dec 1, 2016

Request you to please share speech to text c# code.

Copy link

Nalinh commented Apr 4, 2017

i can't run :(
result is blank, tell me why, plz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment