Skip to main content
Every request to the Google Custom Search JSON API must include a valid API key. The key identifies your project to Google, enforces quota limits, and lets you monitor usage in the Cloud Console. This page walks you through creating a key, attaching it to requests, locking it down so only your code can use it, and recovering gracefully when authentication fails.

Create an API key

API keys live inside Google Cloud projects. You need a project before you can create a key.

Pass the key in a request

The Custom Search API accepts your key in two ways. Both are equally valid; use whichever fits your stack.

Option 1 — key query parameter (most common)

Append key=YOUR_API_KEY to the query string. This is the approach shown in Google’s own documentation and in most client libraries.

Option 2 — Authorization header

If you prefer to keep credentials out of the URL (useful when URLs are logged by proxies or servers), pass the key as a Bearer token in the Authorization header.
The Authorization: Bearer approach is identical in security to the key query parameter for API keys. Neither method encrypts the key beyond TLS — both are safe only over HTTPS, which the Custom Search API enforces.

Restrict your API key

Key restrictions are your primary defense against unauthorized use. Apply at least one restriction to every production key.

HTTP referrer restrictions

Limit the key to requests that originate from specific URLs. Ideal for browser-based integrations where you control the domain.

IP address restrictions

Limit the key to one or more server IP addresses. Ideal for server-to-server API calls where your server has a static IP.

API restrictions

Limit the key to specific Google APIs (e.g., Custom Search API only). Always combine this with an application restriction.

Per-key quota limits

Set a maximum daily or per-minute quota in Cloud Console to cap spend even if the key is misused.

Set HTTP referrer restrictions

In APIs & Services → Credentials, open your key and scroll to Application restrictions:
  1. Select HTTP referrers (websites).
  2. Add your allowed origins, for example:
    • https://www.example.com/* — all pages on your site
    • https://app.example.com/search* — a specific path
  3. Click Save.
HTTP referrer restrictions rely on the Referer header, which can be spoofed in server-side contexts. Use IP address restrictions for backend services instead.

Set IP address restrictions

  1. Select IP addresses (web servers, cron jobs, etc.).
  2. Enter your server’s IPv4 or IPv6 address (e.g., 203.0.113.42 or 203.0.113.0/24 for a range).
  3. Click Save.

Best practices for key security

Hard-coded keys in source files are the most common cause of credential leaks. Store keys in environment variables or a secrets manager (Google Secret Manager, AWS Secrets Manager, HashiCorp Vault) and read them at runtime.
If you use a .env file for local development, make sure it’s listed in .gitignore before your first commit.
Use .env.example (with placeholder values) to document required variables for teammates.
If you accidentally commit a key or expose it in logs:
  1. Go to APIs & Services → Credentials.
  2. Delete the compromised key.
  3. Create a new key and apply restrictions immediately.
  4. Update all services using the old key.
Check your Cloud Console quota dashboard for unusual usage spikes that might indicate the key was already used.
Use distinct keys for development, staging, and production. This lets you rotate or delete a key in one environment without affecting others, and gives you per-environment visibility in usage reports.
In APIs & Services → Metrics, review request volume, error rates, and latency per API key. Set up budget alerts in Cloud Billing to get notified before unexpected spend accumulates.

Authentication error responses

When authentication fails, the API returns a JSON error body alongside an HTTP error status code. Parse error.code and error.message to determine the cause.

401 — Invalid credentials

The API key is malformed, deleted, or belongs to a different project.
How to fix: Verify the key exists in your Cloud Console credentials page, that it’s for the correct project, and that the Custom Search API is enabled on that project.

403 — Quota exceeded or key restriction mismatch

A 403 can mean two different things:
You’ve exhausted the 100 free queries/day on the free tier. The reason field will be RATE_LIMIT_EXCEEDED or QUOTA_EXCEEDED.
How to fix: Enable billing on your project and purchase additional quota (up to 10,000 queries/day) in the Cloud Console.

Handle errors in code

Always check the status code before accessing response data:

Next steps

Quick Start

Make your first end-to-end search request using curl and Python.

Query Parameters

Filter results by language, country, date range, file type, and more.