Skip to main content
Building a search application with the Google Custom Search JSON API lets you embed Google-quality search directly into your product — scoped to specific domains, tuned to your audience, and fully under your control. This guide walks you through creating a Programmable Search Engine, wiring up a search input, calling the API when a user submits a query, and rendering results that include a title, snippet, and URL. You’ll finish with two complete working implementations: a standalone HTML/JavaScript single-page app and a Python Flask web application.

Prerequisites

Before you start, make sure you have the following:
If you haven’t created an API key yet, go to Google Cloud Console → APIs & Services → Credentials → Create Credentials → API key. Enable the Custom Search JSON API in the API Library before proceeding.

Step 1: Create and configure your Programmable Search Engine

1

Open the Programmable Search Engine control panel

Navigate to https://programmablesearchengine.google.com/ and sign in with your Google account. Click Add to create a new engine.
2

Scope the engine to a domain

In the Sites to search field, enter the domain you want to search. For example, to restrict results to a single documentation site, enter docs.example.com. You can add multiple domains or use a wildcard like *.example.com to include all subdomains.Give your engine a memorable name (for example, My Docs Search) and click Create.
3

Copy your Search Engine ID

After creation, click Control Panel on the confirmation screen. Under Basic, find the Search engine ID field and copy the value — this is your cx parameter. Keep it alongside your API key.
4

(Optional) Enable the entire web

If you want results from the whole web rather than specific sites, go to Control Panel → Basics and toggle Search the entire web to on. You can still pin or boost specific sites using the Sites panel.

Step 2: Understand the API request structure

Every search request is a GET to the base URL with your credentials and query:
The response contains a searchInformation object with metadata and an items array where each element represents one search result.
The items array can contain up to 10 results per request. Use the num parameter (1–10) to reduce that count, and the start parameter to paginate through additional pages of results.

Step 3: Build the HTML/JavaScript application

The example below is a self-contained single HTML file. It renders a search input, calls the Custom Search API on form submission using the Fetch API, and displays results without any build tooling or dependencies. Replace YOUR_API_KEY and YOUR_SEARCH_ENGINE_ID with your actual values before opening the file in a browser.
index.html
Embedding your API key in client-side HTML exposes it to anyone who views the page source. For production applications, proxy API calls through your own backend and keep the key server-side only.

Step 4: Build the Python Flask application

The Flask application below exposes a /search route that accepts a q query parameter, calls the Custom Search API server-side, and returns rendered HTML. This approach keeps your API key out of the browser. Install the dependencies first:
Run the Flask app with your credentials injected as environment variables:
Then open http://localhost:5000 in your browser.
In production, use a secrets manager (e.g., Google Secret Manager, AWS Secrets Manager, or environment variables injected at deploy time) rather than hardcoding credentials anywhere in your source code.

Step 5: Handle errors gracefully

The Custom Search API returns standard HTTP status codes. Handle these cases in all applications:

Next steps

Pagination

Learn how to use the start parameter to page through all available results.

Safe Search

Configure the safe parameter to filter adult content from your results.

Localization

Use lr, gl, cr, and hl to deliver region- and language-specific results.

API Reference

Browse the full list of query parameters and response fields.