Using Google Custom Search API in Web API


This article explains how to add a Google Custom Search in the Web API.

Google Custom Search API

The Google Custom Search API allows developers to create a search engine created by using the Google core search technology. Developers can search a website or collection of websites according to your needs and interests in the website. Google generates the source code for the search box. You can copy this code and paste the code into your web application.

Procedure for adding the Google Custom Search API:

  1. First we create a Google account.
  2. For integrating the Google Custom Search you need to login with your Gmail id on this link:

  3. After login in the page of the preceding link a window will open that gets the information about the search engine.

    In the image above there is some information that you can see:
    • Site for search: In this bock you write your site for searching.
    • Language: Select the language for the website.
    • Name: It is based on the website that you fill in the site for search box.
    • Terms of services: This link describes the terms of services of the Google.
    • Create: At last you click on the create button for generating the script.
  4. When you click on the create button code is generated. You copy and paste this code into your web application.


  5. Click on the link "Edit your search settings" to change the setting.

  6. Create Web API application:
    • Start Visual Studio 2012.
    • From the start window select "Installed" -> "Visual C#" -> "Web".
    • Select "ASP.NET MVC4 Web Application" and click the "Ok" button.
    • From the"MVC4 Project" window select "Web API".

    • Click the "OK" button.
  7. Now copy the code and paste it into the "Index.cshtml" file. This file exists in:
    • In the "Solution Explorer".
    • Select the "Home folder".
    • Select "Index.cshtml file".


      The code looks like this:
      1. <!DOCTYPE>  
      2. <html xmlns="">  
      3. <head>  
      4.     <title>c-sharpcorner</title>  
      5. </head>  
      6.     <body>  
      7. <script>  
      8.     (function () {  
      9.         var cx = '001111315940323651598:b5c5-62jb84';  
      10.         var gcse = document.createElement('script'); gcse.type = 'text/javascript';  
      11.         gcse.async = true;  
      12.         gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +  
      13.             '//' + cx;  
      14.         var s = document.getElementsByTagName('script')[0];  
      15.         s.parentNode.insertBefore(gcse, s);  
      16.     })();  
      17. </script>  
      18. <div style="width:350px;"><gcse:search></gcse:search>  
      19. </body>  
      20. </html>  
  8. Now execute the application:


    Enter any topic to search for and click on "Search". It looks like this: