75

Does Google provide API access to autocomplete for search like on the actual site? I have not been able to find anything.

I would like to use Google's autocomplete logic for web search on my own site which relies on Google's search API.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Justin Meltzer
  • 13,318
  • 32
  • 117
  • 182
  • 2
    Sometime between yesterday and today, http://google.com/complete/search?q=&hl=en stopped working. That means the autocomplete functions on xlune and blinkdash stopped working as well. Google changed something, and until someone figures out what that is, trying to access their autocomplete won't work. –  Nov 29 '11 at 15:08

7 Answers7

79

The new url is:

http://suggestqueries.google.com/complete/search?client=firefox&q=YOURQUERY

the client part is required; I did't test other clients.

[EDIT]

If you want the callback use this:

http://suggestqueries.google.com/complete/search?client=chrome&q=YOURQUERY&callback=callback

As @Quandary found out; the callback does not work with client "firefox".

[EDIT2]

As indicated by @ user2067021 this api will stop working as of 10-08-2015: Update on the Autocomplete API

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
RickyA
  • 15,465
  • 5
  • 71
  • 95
  • 1
    Use this instead: http://suggestqueries.google.com/complete/search?client=chrome&q=YOURQUERY&callback=abc because callback doesn't work if you use client=firefox – Stefan Steiger Mar 01 '13 at 05:01
  • those both direct to a blank file for me – Ky - Oct 11 '13 at 19:32
  • @RickyA Yes, but even when viewing the source code, it was completely empty. Is it supposed to download or something? – Ky - Oct 15 '13 at 02:16
  • @Supuhstar: They download a json/js string like: `callback && callback(["YOURQUERY",[],[],[],{"google:clientdata":{"bpc":false,"tlw":false},"google:suggesttype":[],"google:verbatimrelevance":1300}])`. The content type is however "Source code". Maybe your browser blocks that type. Did you try another browser? – RickyA Oct 15 '13 at 07:38
  • Musta been a bad nightly release; I got it now. Though... this doesn't look like an autocomplete list. How does this help with autocomplete? – Ky - Oct 15 '13 at 12:49
  • 4
    Try a real query like: http://suggestqueries.google.com/complete/search?client=firefox&q=superstar – RickyA Oct 15 '13 at 13:03
  • 2
    It looks like 'unauthorised' access to the autocomplete APIs may be restricted from August 10 2015, based on this announcement from Google [http://googlewebmastercentral.blogspot.ca/2015/07/update-on-autocomplete-api.html](http://googlewebmastercentral.blogspot.ca/2015/07/update-on-autocomplete-api.html). – user2067021 Jul 28 '15 at 08:29
  • 2
    It is now 01-19-2016 and the API is still available. Has anyone seen an update on this since the original announcement? – bridiver Jan 19 '16 at 18:25
  • 2
    Yeh. I am also still able to use the API. Seems working fine lol. – GeneCode Feb 22 '16 at 10:19
  • 1
    CORS Restrictions apply for the API when querying with AJAX through the browser. If, however, the request ist routed through your own backend with a synchronous GET request on server side, the API still works as intended. Since it's not possible to do autocomplete requests through the CSE JSON API, this is a mandatory feature that has to be kept alive IMO. – Liquinaut Jan 18 '17 at 10:42
  • 2022 and the api is still working lol – Sundios May 27 '22 at 06:03
13

First, go to google, click Settings (bottom right corner), change Search Settings to "never show instant results. That way, you'll get regular autocomplete instead of a full page of instant results.

After your settings are saved, go back to the Google main home page. Open your browser's developer tools and go to the Network tab. If you're in Firefox, you might have to reload the page.

Type a letter in the search box. A new line should appear in the Network window you just opened. That line is showing where the autocomplete data came from. Copy that url. It should look something like this:

https://www.google.com/complete/search?client=hp&hl=en&sugexp=msedr&gs_rn=62&gs_ri=hp&cp=1&gs_id=9c&q=a&xhr=t&callback=hello

You'll notice your search term right after the part that says q=.

Add &callback=myAmazingFunction to the end of the url. You may replace myAmazingFunction with whatever you want to name your function that will handle the data.

Here's an example of the code required to show the autocomplete data for the search term "a".

<div id="output"></div>

<script>
/* this function shows the raw data */
function myAmazingFunction(data){
    document.getElementById('output').innerHTML = data;
}
</script>

<script src="https://www.google.com/complete/search?client=hp&hl=en&sugexp=msedr&gs_rn=62&gs_ri=hp&cp=1&gs_id=9c&q=a&xhr=t&callback=hello&callback=myAmazingFunction"></script>

Now that you know how to get the data, the next step is to automatically change that last script (the one with the autocomplete url). The basic procedure is: each time the user types something in the search box (onkeyup) replace the search term (q=whatever) in the url, and then append to the body a script with that url. Remove the previous script so that the body doesn't get cluttered.

For more info, see http://simplestepscode.com/autocomplete-data-tutorial/

Matt
  • 74,352
  • 26
  • 153
  • 180
yaphi1
  • 157
  • 1
  • 4
12

Most of the above mentioned methods works for me, specifically the following serves my purpose.

http://suggestqueries.google.com/complete/search?client=firefox&q=YOURQUERY

Being a newbie in web programming, I'm not much aware of the "Callback" functionality and the format of the file returned by query. I'm little aware of AJAX and JSON. Could someone provide more details about the format of file returned by the query. Thanks.

Vijay Rajanna
  • 645
  • 1
  • 8
  • 15
  • Doesn't seem to work anymore... I get a document with this contents: ["YOURQUERY",[]] - nothing else. – Stefan Reich Feb 16 '14 at 10:25
  • 1
    Aha! OK, so it DOES work. :) Just "YOURQUERY" is something Google cannot complete. Haha. http://suggestqueries.google.com/complete/search?client=firefox&q=mary+had gives some proper results. – Stefan Reich Feb 16 '14 at 10:26
  • Does anybody know how to make this work for the `Android Studio` for an android apk ? – TheCoderGuy Mar 10 '19 at 11:27
1

You should use AutocompleteService and pass that text box value into the service.getPlacePredictions function. It send the data in callback function.

let service = new google.maps.places.AutocompleteService();

let displaySuggestions = function(predictions, status) {
}

service.getPlacePredictions({
    input: value
}, displaySuggestions);

Base: https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteService.getPlacePredictions

example: https://dzone.com/articles/implement-and-optimize-autocomplete-with-google-pl

quinz
  • 1,282
  • 4
  • 21
  • 33
1

Hi I don't know if this answer is relevant for you anymore or not but google returns JSON data through following get request (although this isn't an official API but many toolbars are using this API so there's no reason why google might discontinue it):

http://google.com/complete/search?q=<Your keywords here>&hl=en 
Sid
  • 61
  • 2
  • 3
    Could you provide more information on what this is? I tried it out and it has me download a file. Check it out yourself. – Justin Meltzer Jul 12 '11 at 14:35
  • Yes justin, that file is a JSON response. You can utilize it using javascript.. you can see the raw result by opening that file using notepad or vim if you are on linux. – Sid Jul 13 '11 at 05:18
  • Works in a web browser but doesn't seem to work in a native application. Can't get the request working in iOS or Android (Swift/Java). – Supertecnoboff Oct 27 '17 at 10:15
0

I'm using (( Edrra.com )) API that have google search and suggestions that works with both GET & POST:

Google suggestions: https://edrra.com/v1/api.php?c=google&f=suggest&k=YOUR_API_KEY&v=YOUR_SEARCH

Google search: https://edrra.com/v1/api.php?c=google&f=search&k=YOUR_API_KEY&v=YOUR_SEARCH

and more...

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 22 '22 at 15:40
-2

What are you trying to use an auto-complete for? More information would help narrow it down.

As far as I know, google does not provide one, but they do exist like jQuery UI's auto-complete.

EDIT:

If you are using their custom search API view here for autocomplete.

Collecter
  • 218
  • 1
  • 3
  • 11