API Connector Documentation
Import Google SERP Data to Google Sheets
In this guide, we’ll walk through how to use Google's Programmable Search Engine API (previously known as the Custom Search API) to import search engine result page (SERP) data to Google Sheets, using the API Connector add-on for Sheets.
This free API is primarily designed for creating applications that retrieve and display search results programmatically, like a search box widget that you place on your own website. However, it also lets you query Google's search engine check the search result rankings for any keyword, using Google's own search engine results page data.
Contents
- Before You Begin
- Part 1: Create a Programmable Search Engine
- Part 2: Get your Programmable Search API Key
- Part 3: Pull Google SERP Data into Sheets
- Part 4: Handle Pagination
- Part 5: API Documentation
Before You Begin
Click here to install the API Connector add-on from the Google Marketplace.
Part 1: Create a Programmable Search Engine
- While logged into your Google account, navigate to https://programmablesearchengine.google.com/controlpanel/all, and click Add.
- You'll be prompted to configure your custom search engine. Give your Search Engine a name, and choose to search the entire web (unless you specifically want to limit your results to a subset of pages). Click Create.
- Your search engine has been created. Click Customize.
- Scroll down the page and optionally adjust any settings. Note the search engine ID as we'll need that soon.
- That's it, your Google Programmable Search engine is ready.
Part 2: Get your Custom Search API Key
Accessing Google's Custom Search JSON API requires the use of an API key.
- Navigate to the Custom Search JSON API page and click Get a Key.
- Choose an existing project, or create a new one, and click Next.
- Note and copy your API key, you'll need this along with your custom search engine ID.
Part 3: Pull Google SERP Data into Sheets
For our first API request, we'll pull in the top 10 page titles, URLs, and snippets for a keyword.
- Open up Google Sheets and click Extensions > API Connector > Open > Create request.
- In the request form enter the following. Substitute in your own key, search engine ID, and keyword in the
key
,cx
, andq
parameters, respectively.- Application:
Custom
- Method:
GET
- Request URL:
https://www.googleapis.com/customsearch/v1?key=AIzaSy11111111&cx=e74099405af6745a5&q=island vacations&fields=queries,items(title,link,snippet)
- Application:
- Create a new tab and click Set current to use that tab as your data destination.
- Under Output options, set the Report style to 'compact'.
- Name your request and click Run. A moment later you’ll see a list of 10 search results populate your sheet.
- Fetch more records, or further customize your request by including any of the parameters available in the documentation. You can filter the response data by site, region, language, and so on.
Part 4: Handle Pagination
By default, the Custom Search API will return only 10 results. To get more, use the start
and num
parameters as described in their documentation.
You can run through these request URLs manually, or use API Connector's pagination handling function to loop through these pages automatically, like this:
- Pagination type:
offset-limit
- Offset parameter:
start
- Limit parameter:
num
- Limit value:
10
- Run until:
choose when the request should stop running
This API will not return more than 100 total records, regardless of the actual number of search results.
Part 5: API Documentation
Official API documentation: https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list
I've been looking for a way to go around the "Sites to Search" Limitation. Thanks for the tip.
Awesome, glad to have helped.
Hi, how can I display the results in a webpage ?
Hi Arnaud, this is a web development question so the answer completely depends on what you're trying to accomplish and the technology behind your website. Generally you'd write some code to parse the JSON response so it can be displayed on your site. Or, if you're looking for a no-code solution, you can look for a CMS plugin (here's one for WordPress: https://wordpress.org/plugins/json-content-importer/), or use an app like http://sheet2site.com/ to turn your spreadsheet into a website.
Hi Ana. Thanks for your answer ! 😉
I looked into the WP Jason importer but unfortunately I'm not techie enough to implement it till the end... I am actually trying to display Google Custom Search Results only (Say article is speaking about "Paris restaurants close to Eiffel Tower", I want to display results from predefined search terms such as "Paris+restaurants+Eiffel+Tower" as a side information based on the blog article) on a WP site to give readers further details on a subject.
Hi Ana,
Great tutorial. Can this fetch method be used with a search box like the one the comes with the google basic CSE?
Hey Hassan, do you mean can you get search results for the whole web via a search box? Sure, in that case you'd configure your search engine as described, then click the 'Add it to your site' Get Code button to get the code that creates a search box on your site.
How can I add Google Analytics to Custom Search JSON API
Hey Artur, sorry, I'm not really sure what you're trying to do. Is this what you're looking for? https://support.google.com/customsearch/answer/2715510?hl=en
Hello Ana,
Such an important article especially for developers. But I want to pay attention that the result of Google CSE is not the same actual Google web search. At this point, we as Aves API, offer the most powerful and affordable SERP API in the market.
Best Regards,
Murat Akboğa / Founder, AvesAPI
very cool, thanks much!
Thanks for the guided tutorial. Surprisingly I was able to get it to work.
Awesome, I'm glad that worked for you! Is it so surprising? 😀
Hi! This is great! How can we add more than one "search term"?
Unfortunately the Custom Search JSON API only lets you specify one search term at a time. So you'll either need to run separate requests for each search query you're interested in (you can use Append mode to print them into the same sheet), or use API Connector's (paid) multi-query feature which lets you stack URLs and run them as a block.
Thank you very much. This really works well!
All the best
Glad you like it, thanks for the comment 🙂
Hi Ana,
Would you mind answering a couple of questions about the Api connector:
1. Can use this Api connector on a mobile, as I do not have a computer at the moment,
2. If yes to 1, how would I would I be able to make a search request to add the the following headers: Date, Title, Description, Url, Size 1000, Sort Asc, and import data under these headings. I would like to get data on shared google or excel stock spreadsheets.
3. Regarding free Api option, if I do one search request for 1000 search results, is that considered as one request for the months, in regards to billing.
1. Can use this Api connector on a mobile, as I do not have a computer at the moment,
The mobile version of Google Sheets supports extensions for Android, but not iOS (info)
2. If yes to 1, how would I would I be able to make a search request to add the the following headers: Date, Title, Description, Url, Size 1000, Sort Asc, and import data under these headings.
I'm not sure what should go under Size 1000 and Sort Asc, but for Date you can add a timestamp, and for Title, Description, and URL you can use a URL like this:
https://www.googleapis.com/customsearch/v1?key=1234&cx=123:123
(use the field editor to label the fields exactly as you want them)&q=pizza slice&fields=queries,items(title,link,snippet)
3. Regarding free Api option, if I do one search request for 1000 search results, is that considered as one request for the months, in regards to billing.
That would be considered one request. Requests are counted by the number of times the API is called, so it doesn't matter how many records are returned. Hope that helps, let me know if I can clarify anything else.
Hi Ana,
I hope you and your family had an enjoyable start to 2023.
Thank you for the quick reply. I am unable to see the add-on when I select add-ons from the sheet. However when I go to the Play Store it indicates that is already installed. Maybe it installed the pc version ? Can you please send me the link to download the android verskon
Thank you, Ron, you too 🙂 There's no device-specific link for API Connector, so you should already have access if you installed it previously. Can you please check this link for how to open add-ons in the Android version of Google Sheets?
Hi Ana!
What if I want to fetch Top100 search results for certain keyword and import into GSheets only search results from certain domains (like of me and my direct competitors) with corresponding ranking place?
Hey Evgeniy! You can filter via the API request URL by using the
siteSearch
parameter, e.g.&siteSearch=yoursite.com
. That's convenient for making dynamic changes but thesiteSearch
parameter only lets you specify a single domain at a time. So the other way to include more is to specify the domains you want to search in your custom search engine configuration. Under "What to search" you can enter the sites you want to include.Really helpful guide! Enabled us to make a big leap!
Nice, glad you like it!
Hi,
Can i somehow get the ?q= parameter value from cell in my sheet?
Sure, just include a cell reference in the URL like
&q=+++Sheet1!A1+++
For more information, please see this article: Use Cell Values in Requests
Hi Ana, Thanks for the excellent tutorial. I have a quick question: Is there a way to get data from the Google "People Also Ask" section for a given keyword?
Good question, but as far as I can see, this data is not available from Google's Search API, and this Q&A from their community seems to confirm it. If you're looking for this data, I think you'll need to use one of the paid search data services; they have access to a wider range of data since they scrape data themselves from Google's search results rather than relying on the official API.