Skip to main content
Every request to the Custom Search JSON API must be authenticated with a Google API key. The key identifies your project for quota tracking, billing, and access control. This page covers how to create a key, the two ways to pass it on each request, how to restrict it to reduce exposure, and what error responses look like when a key is missing, invalid, or revoked.

Creating an API Key

1

Open the Google Cloud Console

Go to console.cloud.google.com and select or create the project you want to use for search.
2

Enable the Custom Search API

Navigate to APIs & Services → Library, search for Custom Search API, and click Enable. The key will not work for search requests until the API is enabled on your project.
3

Create a credential

Navigate to APIs & Services → Credentials, click Create Credentials, and choose API key. Google generates a key immediately.
4

Copy and store the key securely

Copy the key value now. Treat it like a password — store it in an environment variable or secret manager, never in source code or client-side JavaScript.
5

Apply restrictions (recommended)

Click Edit API key and apply both an application restriction and an API restriction before your key goes to production. See Key Restrictions below.
API keys are not access tokens. They do not expire on their own and are not scoped to a user identity. Anyone who obtains your key can consume your quota. Restrict every key before using it in production.

Passing the Key on Each Request

You have two options for supplying your API key: a query parameter or an HTTP authorization header. Both are accepted on every endpoint; use whichever fits your architecture. Append key=YOUR_API_KEY to the query string. This is the most common approach and works with any HTTP client.

Option 2 — Authorization: Bearer Header

Pass the key as an OAuth2 Bearer token in the Authorization header. Use this approach when your HTTP client or proxy framework enforces standard authorization headers and you want to keep credentials out of the URL (and therefore out of server access logs).
Prefer the Authorization header over the query parameter when you control the HTTP client. URLs (including query strings) can appear in proxy logs, browser history, and CDN access logs. Headers are not logged by most intermediaries.
Do not supply both the key query parameter and an Authorization header in the same request. If both are present, the key query parameter takes precedence.

Key Restrictions

Google Cloud Console lets you attach two independent restriction layers to each API key. Apply both before you deploy.
Application restrictions limit which callers can use the key. Choose the type that matches your deployment:To set a restriction: Credentials → Edit API key → Application restrictions.
HTTP referrer restrictions do not protect server-side calls — Referer headers are trivially spoofed by command-line tools. For server-to-server requests, use IP address restrictions or keep the key in a backend proxy so clients never see it.

Rotating Keys

Rotate your API key whenever:
  • You suspect it has been exposed (committed to a repo, logged in plaintext, etc.)
  • A team member who had access to the key leaves your organization
  • You perform a scheduled credential rotation as part of your security policy
1

Create the replacement key

In APIs & Services → Credentials, click Create Credentials → API key. Apply the same restrictions as the existing key before proceeding.
2

Deploy the new key

Update your secret manager, environment variables, and any configuration files with the new key value. Deploy the change to all services that call the API.
3

Verify the new key works

Make a test request with the new key and confirm you receive 200 OK. Check your Cloud Console quota dashboard to confirm queries are attributed to the new key.
4

Delete the old key

In Credentials, click the delete icon next to the old key. Deletion is immediate and irreversible — the old key stops working instantly.
If you are unsure whether the old key is still in use anywhere, disable it instead of deleting it first. A disabled key returns 401 errors, making it easy to identify any missed references in your logs before you delete permanently.

Error Responses for Authentication Failures

When a request fails due to a missing, invalid, restricted, or revoked API key, the API returns a structured JSON error body. All authentication errors carry an error object at the top level.

Missing Key — 400 Bad Request

Omitting the key parameter entirely returns a 400:

Invalid Key — 401 Unauthorized

A key that does not exist or has been deleted returns a 401:

Key Restricted — 403 Forbidden

A key that exists but does not have permission to call this API (API restriction not met) or that is being called from a disallowed referrer or IP (application restriction not met) returns a 403:
API not enabled on project:
Daily quota exceeded:
Referrer blocked:

Handling Authentication Errors in Code

Check the HTTP status code first, then inspect error.status for granular handling:

API Overview

Base URL, required parameters, response shape, and a minimal working example.

Error Codes

Full reference for every HTTP status code the API returns, with resolution steps.