Using google custom search api, you can programatically generate google search results.

Two types of google custom search APIs-

  1. Google custom search JSON API
  2. Google Custom Search Site Restricted JSON API

Google custom search JSON API-

Google custom search JSON API is a RESTFUL API let 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 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 –

  1. q– query string
  2. cx– Custom search engine ID.
  3. fileType– bmp, gif, jpg, png, pdf etc
  4. num-number of search results to display
  5. searchType- search type.
  6. sort-Sort by functionality to use.

For a list of all query parameters, go to this link.

google search api

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

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

  1. Go to https://developers.google.com/custom-search/json-api/v1/overview
  2. Click on get  a key.Google custom search JSON API simplified
  3. Click create new project.Google custom search JSON API simplified
  4. This will generate your new API key.Google custom search JSON API simplified
  5. You can restrict the usage of this API key in your google api console https://console.developers.google.com/apis
Google custom search JSON API simplified

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.

Google custom search JSON API simplified

Testing your google custom search JSON API-

You can test you google custom search api using the google api explorer 

google custom search JSON simplifiedGoogle Custom search site restricted API

google custom search site restricted json api

Points to remember- (if you do not adhere to the following points your API might not return the search results)

  1. Google Custom Search Site Restricted JSON API is similar to the JSON custom search API.
  2. You can search within 10 or lesser websites using this API.
  3. No daily Query limit.
  4. 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 is of the following format-

https://www.googleapis.com/customsearch/v1/siterestrict?[parameters]

Here parameters could be –

  1. q– query string
  2. cx– Custom search engine ID.
  3. fileType– bmp, gif, jpg, png, pdf etc
  4. num-number of search results to display
  5. searchType- search type.
  6. sort-Sort by functionality to use.

Here are the list of all parameters-

google search api

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-

  1. Go to https://developers.google.com/custom-search/json-api/v1/overview
  2. Click on get  a key.
  3. Click create new project.
  4. This will generate your new API key.google search api

Getting your CX / Search engine ID:

Go to your custom search / site search control panel-> edit search engine->Basics->Details->Search engine ID.

google search api

You can test you google custom search api using the google api explorer 

google search api

 

 

 

muthali ganesh

Muthali loves writing about emerging technologies and easy solutions for complex tech issues. You can reach out to him through chat or by raising a support ticket on the left hand side of the page.

You may also like