C# – How to use Google Custom Search API?

Google search engine has a market share of over 60%. For some advanced features, such as searching metadata or relevant info of an object, we maybe want to integrate search result of Google search engine instead of inventing our own one. In this post, I would like to write down the steps how we can consume Google Custom Search API in .Net. The code itself is pretty short. However, because of lacking documentation it’s really time consuming to find out how the code should be, where to get the API Key or Search Engine ID for authentication. These all settings stuff drive me crazy because they locate on different control panel.
Please note that Custom Search Engine is a free API edition. For CSE users, the API provides 100 search queries per day for free. If you need more, you may sign up for billing in the Developers Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day.
The other paid version is Google Site Search which is out of scope of this post.

1. Prerequisites

We need an Google Account. Sign up when you don’t have any. That’s all.

2. Get API key and write code

2.1 Get API key

If you already read my previous post Google Cloud Messaging, ASP.NET Web Api and Android client, you must be familiar with generating a Google API key. For Google Custom Search it’s almost the same.
– Go to https://console.developers.google.com
– Create a project if you don’t have any

Create project

– Click on your project link to go to project control panel. On the link menu, click on APIs, be sure that Custom Search API is enabled. If not you can enable it from available list under

Custom Search API

Custom Search API enabled

– Also in project control panel, to Credentials, Create new key

Create new key

– Be sure that a Browser key is generated
Browser key

– Write down this browser key. We need it for code later.

2.2 Search Engine ID

The next thing we need for our code is a Search Engine ID. The API key above is used for authentication so that we’re allowed to use Google API function. For custom searching, we need to define our custom search engine to make a search.

– Go to https://www.google.com/cse/all and add new custom search engine

– For Sites to search enter any domain, for example google.com, then Create. In CSE control panel, select to edit the just created CSE. Be sure that you land on Setup menu for current editing CSE.

Edit custom search engine

– Be sure that Search the entire web but emphasize included sites is selected then click on Update

– Click on Search Engine ID button to get its ID

– Write down this Search Engine ID, we’ll need it later.

2.3 Code

For consuming Google Custom Search API, luckily we don’t have to write our own code for reading/writing REST object. Google has also provided a Nuget package Google.Apis.Customsearch.v1 for .NET client. In your project, just simply add a reference to it over Nuget.

And the code is pretty simple.

private static void Main(string[] args)
{
	const string apiKey = "AIzaSyAt8AkrmkiLVghrcKA3lFh37R79rSG0NsE";
	const string searchEngineId = "003470263288780838160:ty47piyybua";
	const string query = "hintdesk";
	var customSearchService = new CustomsearchService(new BaseClientService.Initializer {ApiKey = apiKey});
	var listRequest = customSearchService.Cse.List(query);
	listRequest.Cx = searchEngineId;

	Console.WriteLine("Start...");
	IList<Result> paging = new List<Result>();
	var count = 0;
	while (paging != null)
	{
		Console.WriteLine($"Page {count}");
		listRequest.Start = count * 10 + 1;
		paging = listRequest.Execute().Items;
		if (paging != null)
			foreach (var item in paging)
				Console.WriteLine("Title : " + item.Title + Environment.NewLine + "Link : " + item.Link +
								  Environment.NewLine + Environment.NewLine);
		count++;
	}
	Console.WriteLine("Done.");
	Console.ReadLine();
}

We have to initialize an instance of CustomsearchService with API key. Using this instance for generating a query of keyword hintdesk over Cse property. This query requires a Search Engine ID, so remember to set it’s Cx property by this value. At the end just simply call Execute() to get the search result

The image below shows a snapshot of keyword hintdesk search result.

Hintdesk seach result

The .NET client library supports also Async call. In combination with async, await keyword you can make an asynchronous call to Google web service.

3. Conclusions

Consuming Google API is pretty simple. The only annoyed thing is too little documentation to instruct when we can get die necessary settings such as API key or search engine id.
Source code: https://bitbucket.org/hintdesk/dotnet-how-to-use-google-custom-search-api

4. Updates

4.1 Paging

– Add paging to query to show more than top 10 result

10 thoughts on “C# – How to use Google Custom Search API?”

  1. Really cool tutorial. thanks for that.

    I need to access the right pane in the first result page when I search for location (eg. company name. G4S London, United Kingdom). I just need to pick the phone number and the website URL from there.

    Any idea are appreciated.

    Thank you,

  2. Thank You very much for your post…… It has reduced the complexity exponentially (n power n) to use the Google API …. Thank You ….

Leave a Reply

Your email address will not be published. Required fields are marked *