Prerequisites
Before you start, make sure you have the following:- A Google account to access the Google Cloud Console and the Programmable Search Engine control panel
- An API key with the Custom Search JSON API enabled
- A Search Engine ID (
cx) from the Programmable Search Engine control panel - Basic familiarity with HTML/JavaScript or Python
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 aGET to the base URL with your credentials and query:
searchInformation object with metadata and an items array where each element represents one search result.
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. ReplaceYOUR_API_KEY and YOUR_SEARCH_ENGINE_ID with your actual values before opening the file in a browser.
index.html
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:
Step 5: Handle errors gracefully
The Custom Search API returns standard HTTP status codes. Handle these cases in all applications:Exponential back-off example (Python)
Exponential back-off example (Python)
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.