Gemini API Key Invalid or Restricted (403 PERMISSION_DENIED)
Your API key was rejected. Common causes: key restrictions (referrer/IP/API), expired key, Generative Language API not enabled in project, or trying to use an AI Studio key for Vertex AI (they're different auth systems).
Verify: (1) key is correct and not truncated, (2) Generative Language API is enabled in your Google Cloud project, (3) API restrictions include Generative Language API, (4) IP/referrer restrictions match your caller, (5) you're using an AI Studio key on generativelanguage.googleapis.com (not Vertex AI, which needs service accounts). Regenerate the key if unsure.
The Error Messages You're Seeing
HTTP/1.1 403 Forbidden
{
"error": {
"code": 403,
"message": "Method doesn't allow unregistered callers
(callers without established identity). Please use API
Key or other form of API consumer identity to call this API.",
"status": "PERMISSION_DENIED"
}
}
Variants:
"API key not valid. Please pass a valid API key."
"Requests from referer <URL> are blocked."
"Requests from this IP address are blocked."
"Generative Language API has not been used in project
<PROJECT> before or it is disabled."
"API key expired. Please renew the API key."Key Types Reference
| Deployment | Auth method | Where to create |
|---|---|---|
| AI Studio (default) | API key | aistudio.google.com/apikey |
| Vertex AI (recommended) | Service account + ADC | Google Cloud IAM console |
| Vertex AI (also works) | API key (limited) | Google Cloud APIs & Services |
| Local dev | gcloud auth application-default login | gcloud CLI |
What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1Diagnose Which Failure Path
2AI Studio Key Setup Correctly
3Vertex AI Service Account Setup
Application Default Credentials automatically picks the right auth for each environment (local, Cloud Run, GKE, GCE, etc.). Service account JSON files in code repos are a top security incident source. Use workload identity in cloud, gcloud in local dev.
Preventing This Error Going Forward
- Enable Generative Language API in Cloud Console before creating key.
- Restrict keys to specific APIs. Prevents accidental misuse.
- Use IP restrictions for backend keys, referrer for browser keys.
- Never commit keys to git. Google scans and auto-revokes.
- Rotate keys quarterly. Set expiration when possible.
- Use ADC for Vertex AI, keys for AI Studio. Don't mix.
- Add diagnostic health check at app startup. Catch auth breakage fast.
Researcher · AI Error Hub
Frequently Asked Questions
Officially no — they're different auth systems. Vertex AI supports API keys in some contexts now but the canonical auth is service accounts + ADC. Simpler mental model: keys for AI Studio, ADC for Vertex.
Most common: key committed to public GitHub → Google auto-revoked. Also: billing account disabled, quota project deleted, org policy blocking public API access, expiration reached. Check Cloud Console audit logs.
Store in secret manager (GCP Secret Manager, AWS Secrets Manager, Vault). Load at app startup, never log. Apply IP restrictions matching your server's egress IP. Never expose to client.
No — backend calls don't send a Referer header. Referrer restrictions only make sense for browser JavaScript calls. For server, use IP restrictions or leave unrestricted (with tight API restrictions).
Free tier is only available on AI Studio API keys. Vertex AI always bills through your GCP account (with initial $300 credit for new accounts). Use AI Studio for prototyping, migrate to Vertex for enterprise features.
Related Errors
Secure Gemini API Key Management
Weekly deep dives on auth, key rotation, and cloud security. 12,000+ developers subscribe.
Subscribe Free →