Adding Google AJAX Search APIs to Your Web Application: Learn the Easy Basics to Adding Robust Content to Your Site

Page content

The most commonly used Google API seems to be Search, making it the perfect candidate for first in a series on including Google Code in web applications. I introduced this topic in “Should I Try the Google APIs?” found on the Brighthub Google Channel.

You can get to the Google AJAX APIs here. Once there you will see the more popular APIs. We are going to address the Google AJAX Search API (referred to as Search API here) first. Quoting Google, “The Google AJAX Search API lets you put Google Search in your web pages with JavaScript.” ( 10/15/2009). The Google APIs provide “Web Search”, “Local Search” and “Multimedia Search.” The Search API even allows integration with Google Maps. The Web Search API can locally and globally search the Web, News Services and Blogs.

The easiest way to incorporate search is to use the wizards and API key feature at the Google Code Key Sign-up Page. Please read the sign-up pages found in the link.

Wizards at Work

The Wizards are point & click, copy & paste and practically self explanatory. There are wizards for NewsShow_, Map Search, Video Bar, Video Search, Blog Bar, Book Bar,_ and two versions of a News Bar. Let’s look at including the APIs using HTML and scripting. (Go to Google’s AJAX Search Documentation for more information.) There is an interface available for non-JavaScript instances. That raw interface returns JSON results that can be manipulated in most environments.

The Search API is a JavaScript library that is used to place Google Search in your web applications. One of the keys to implementing any of the Google APIs is to include Google’s API loader. This URL needs to be included right after your tag within the your section using the following code. Please note that JavaScript is case sensitive.

<script src=




The parameters in the google.load function indicate the API and version that you are calling. In this case, we are calling the search API version 1. Using the Google Loader allows you to place more than one API on a page or in an application.

One thing to remember is that the G prefix is removed from the API name and replaced with the appropriate namespace (e.g. GLocalSearch becomes Once you have loaded the search API, you can call the search option you want with the SearchControl object. This object has seven child objects available: WebSearch, VideoSearch, BlogSearch, NewsSearch, ImageSearch, BookSearch and PatentSearch. Each of these can be called from the LocalSearch child as well. The addSearcher method will add the specific object. The following snippet (adapted from 10/20/2009) shows how to implement a local search with WebSearch and NewsSearch.

The Code


Original Copyright (c) 2008 Google Inc.

You are free to copy and use this sample.

License can be found here:


<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”">

Bruce Chapin’s adaptation of Google HelloWorld sample script

body {

background-color: lightblue;

color: black;

font-family: Arial, sans-serif;


#searchcontrol .gsc-control { width : 400px; }

<script src= type=“text/javascript”>


google.load(‘search’, ‘1.0’);

function OnLoad() {

// Create a search control

var searchControl = new;

// Add in a full set of searches

var localSearch = new;


searchControl.addSearcher(new google,search.WebSearch());


// Set the Local Search center point

localSearch.setCenterPoint(“Omaha, NE”);

// tell the searcher to draw itself and tell it where to attach


// execute an initial search



google.setOnLoadCallback(OnLoad, true);



This is a very basic implementation of the Google Search API. See the Developer’s Guide for a more detailed explanation, including setting options.

This post is part of the series: Using Google Code

Do you want to bring your web pages to life? Add robust, interactive features to your web pages. This series of how-to articles walks you through adding Google APIs, simply, to your applications. Code samples in HTML & JavaScript included.

  1. Using Google Code: Part 1 - Search
  2. Using Google Code: Pt. 2 - Maps
  3. Using Google Code: Pt. 3 - AJAX Feeds API
  4. Using Google Code: Pt. 4 - Book Search