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.
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.Option 1 — key Query Parameter (recommended for server-side calls)
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).
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
- API Restrictions
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.
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.
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 anerror 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:
Other 403 messages you may see
Other 403 messages you may see
API not enabled on project:Daily quota exceeded:Referrer blocked:
Handling Authentication Errors in Code
Check the HTTP status code first, then inspecterror.status for granular handling:
Related Pages
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.