Google Search autocomplete API? [closed]
Asked Answered
E

6

78

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.

Existentialism answered 21/6, 2011 at 16:7 Comment(0)
J
81

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

Juristic answered 7/12, 2011 at 13:20 Comment(11)
Use this instead: suggestqueries.google.com/complete/… because callback doesn't work if you use client=firefoxAbatis
those both direct to a blank file for meBioluminescence
@Juristic Yes, but even when viewing the source code, it was completely empty. Is it supposed to download or something?Bioluminescence
@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?Juristic
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?Bioluminescence
Try a real query like: suggestqueries.google.com/complete/…Juristic
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.Golfer
It is now 01-19-2016 and the API is still available. Has anyone seen an update on this since the original announcement?Mohammedmohammedan
Yeh. I am also still able to use the API. Seems working fine lol.Lefthanded
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.Hereto
2022 and the api is still working lolFlexuous
F
13

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.

Flycatcher answered 22/10, 2013 at 16:51 Comment(3)
Doesn't seem to work anymore... I get a document with this contents: ["YOURQUERY",[]] - nothing else.Aweinspiring
Aha! OK, so it DOES work. :) Just "YOURQUERY" is something Google cannot complete. Haha. suggestqueries.google.com/complete/… gives some proper results.Aweinspiring
Does anybody know how to make this work for the Android Studio for an android apk ?Addle
R
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/

Robinrobina answered 28/2, 2015 at 19:57 Comment(0)
B
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 
Bucktooth answered 12/7, 2011 at 14:3 Comment(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.Existentialism
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.Bucktooth
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).Oldcastle
S
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

Scanty answered 1/3, 2021 at 13:47 Comment(2)
This is specific to maps.Silverside
@Silverside yeah, this is a google maps service. you can use it to search the places.Scanty
N
0

You can use Keyword Quill, which offers a Google autocomplete API. Get an API key for free and make a request to the endpoint.

A JS example for finding autocompleted keywords for the phrase "air fryers" would be:

const data = JSON.stringify({
  "keyword": "air fryers",
  "gl": "US",
  "hl": "en"
});

fetch('https://api.keywordquill.com/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-KEY': 'API KEY'
  },
  body: data
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Newton answered 1/4 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.