Using Google custom search API, you can programmatically generate Google search results.
Two types of Google custom search APIs-
- Google custom search JSON API
- Google Custom Search Site Restricted JSON API
Google custom search JSON API-
Google custom search JSON API is a RESTFUL API lets you develop applications to get and show search results (Websearch, images) from Google custom search programmatically. With this API, you can use RESTful requests to get either web search or image search results in JSON format.
The existing documentation on creating a google custom search using the API method is confusing and we will try to simplify the process here.
In this article, we will see how we can create a custom search engine using the Google custom search API. We will also see what are the limitations of google’s custom search API and how we can overcome them.
Create your custom search engine using expertrec
Create a custom search engine at https://www.google.com/cse
The usual API call is
https://www.googleapis.com/customsearch/v1?parameters
Here parameters could be –
- q– query string
- cx– Custom search engine ID.
- fileType– bmp, gif, jpg, png, pdf etc
- num-number of search results to display
- searchType- search type.
- sort-Sort by functionality to use.
For a list of all query parameters, go to this link.
A sample API call would be like this-
https://www.googleapis.com/customsearch/v1?key={YOUR_API_KEY}&cx={CUSTOM_SEARCH_ENGINE_ID}&q={KEYWORD}
https://www.googleapis.com/customsearch/v1?key=AIzaSyDOvbipdULbUpVm3HLjoX7bEEfDUKHELh4&cx=012278024424817052234:cimrvj4h4uk&q=jeff
The response of this API will be in JSON format for you to consume to build your search application.
{ "kind": "customsearch#result", "title": "Jeff Pedowitz - President and CEO - The Pedowitz Group | LinkedIn", "htmlTitle": "\u003cb\u003eJeff\u003c/b\u003e Pedowitz - President and CEO - The Pedowitz Group | LinkedIn", "link": "https://www.linkedin.com/in/jeffpedowitz", "displayLink": "www.linkedin.com", "snippet": "View Jeff Pedowitz's full profile. It's free! Your colleagues, classmates, and 500 \nmillion other professionals are on LinkedIn. View Jeff's Full Profile ...", "htmlSnippet": "View \u003cb\u003eJeff\u003c/b\u003e Pedowitz's full profile. It's free! Your colleagues, classmates, and 500 \u003cbr\u003e\nmillion other professionals are on LinkedIn. View \u003cb\u003eJeff's\u003c/b\u003e Full Profile ...", "formattedUrl": "https://www.linkedin.com/in/jeffpedowitz", "htmlFormattedUrl": "https://www.linkedin.com/in/\u003cb\u003ejeff\u003c/b\u003epedowitz", "pagemap": { "cse_thumbnail": [ { "width": "276", "height": "183", "src": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcT00CLj3qk3NDZp35KPvo4A2kaXF-FrhY7DwtGru9ikLSV2LDmaKneWJT2Z" } ],
Getting your Google custom search API Key
- Go to https://developers.google.com/custom-search/json-api/v1/overview
- Click on get a key.
- Click to create a new project.
- This will generate your new API key.
- You can restrict the usage of this API key in your google API console https://console.developers.google.com/apis
Getting your CX / Search engine ID:
Go to your custom search / site search control panel-> edit search engine->Basics->Details->Search engine ID.
This is your search engine ID.
Testing your google custom search JSON API-
You can test your google custom search API using the Google API explorer
Google Custom search site restricted API
Points to remember- (if you do not adhere to the following points your API might not return the search results)
- Google Custom Search Site Restricted JSON API is similar to the JSON custom search API.
- You can search within 10 or lesser websites using this API.
- No daily Query limit.
- Make sure that the “search the entire web” is set to OFF.
Create your google custom search from https://cse.google.com
Google Custom Search Site Restricted JSON API
Google Custom Search Site Restricted JSON API is of the following format-
https://www.googleapis.com/customsearch/v1/siterestrict?[parameters]
Here parameters could be –
- q– query string
- cx– Custom search engine ID.
- fileType– bmp, gif, jpg, png, pdf etc
- num-number of search results to display
- searchType- search type.
- sort-Sort by functionality to use.
Here is the list of all parameters-
Here is a sample API call to get search results-
https://www.googleapis.com/customsearch/v1/siterestrict?key={your_api_key}&cx={custom_search_engine_id}&q={search_keyword}
https://www.googleapis.com/customsearch/v1/siterestrict?key=AIzaSyDOvbipdULbUpVm3HLjoX7bEEfDUKHELh4&cx=012278024424817052234:cimrvj4h4uk&q=linkedin
Here are the search results in JSON format-
{ "kind": "customsearch#result", "title": "LinkedIn: Log In or Sign Up", "htmlTitle": "\u003cb\u003eLinkedIn\u003c/b\u003e: Log In or Sign Up", "link": "https://www.linkedin.com/", "displayLink": "www.linkedin.com", "snippet": "500 million+ members | Manage your professional identity. Build and engage \nwith your professional network. Access knowledge, insights and opportunities.", "htmlSnippet": "500 million+ members | Manage your professional identity. Build and engage \u003cbr\u003e\nwith your professional network. Access knowledge, insights and opportunities.", "cacheId": "AszVZRcWxZEJ", "formattedUrl": "https://www.linkedin.com/", "htmlFormattedUrl": "https://www.\u003cb\u003elinkedin\u003c/b\u003e.com/", "pagemap": { "metatags": [ { "referrer": "origin", "pageimpressionid": "b502334b-1c2a-442a-b05b-6883abbfb21b", "appname": "chrome", "pagekey": "uno-reg-guest-home", "treeid": "bjU+kvwgSRWw0apPLysAAA==", "globaltrackingurl": "//www.linkedin.com/mob/tracking", "globaltrackingappname": "chrome", "globaltrackingappid": "webTracking", "msapplication-tileimage": "https://static.licdn.com/scds/common/u/images/logos/linkedin/logo-in-win8-tile-144_v1.png", "msapplication-tilecolor": "#0077B5", "application-name": "LinkedIn", "remotenavjscontentbaseurl": "https://static.licdn.com/scds/concat/common/js?v=0.1.561", "lnkd-track-json-lib": "https://static.licdn.com/scds/concat/common/js?h=2jds9coeh4w78ed9wblscv68v-ebbt2vixcc5qz0otts5io08xv", "apphost": "seo-directory-frontend", "appversion": "0.1.343", "appinstance": "i002", "locale": "en_US", "baidu-site-verification": "bqQqxRHTnv", "detectadblock": "//platform.linkedin.com/js/px.js" }
How to get your custom search API Key-
- Go to https://developers.google.com/custom-search/json-api/v1/overview
- Click on get a key.
- Click to create a new project.
- This will generate your new API key.
Getting your CX / Search engine ID:
Go to your custom search / site search control panel-> edit search engine->Basics->Details->Search engine ID.
You can test your google custom search API using the Google API explorer
Get Custom Search for your Website at $9