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.
- curl
- Python
- JavaScript (fetch)
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.
- curl
- Python
- JavaScript (fetch)
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:- Select HTTP referrers (websites).
- Add your allowed origins, for example:
https://www.example.com/*— all pages on your sitehttps://app.example.com/search*— a specific path
- Click Save.
Set IP address restrictions
- Select IP addresses (web servers, cron jobs, etc.).
- Enter your server’s IPv4 or IPv6 address (e.g.,
203.0.113.42or203.0.113.0/24for a range). - Click Save.
Best practices for key security
Use environment variables, never source code
Use environment variables, never source code
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.
Add .env files to .gitignore
Add .env files to .gitignore
If you use a Use
.env file for local development, make sure it’s listed in .gitignore before your first commit..env.example (with placeholder values) to document required variables for teammates.Rotate keys immediately if exposed
Rotate keys immediately if exposed
If you accidentally commit a key or expose it in logs:
- Go to APIs & Services → Credentials.
- Delete the compromised key.
- Create a new key and apply restrictions immediately.
- Update all services using the old key.
Create separate keys per environment
Create separate keys per environment
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.
Audit usage regularly
Audit usage regularly
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. Parseerror.code and error.message to determine the cause.
401 — Invalid credentials
The API key is malformed, deleted, or belongs to a different project.403 — Quota exceeded or key restriction mismatch
A403 can mean two different things:
- Quota exceeded
- Key restriction mismatch
You’ve exhausted the 100 free queries/day on the free tier. The How to fix: Enable billing on your project and purchase additional quota (up to 10,000 queries/day) in the Cloud Console.
reason field will be RATE_LIMIT_EXCEEDED or QUOTA_EXCEEDED.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.