Overview
The Google Gen AI SDK supports two authentication methods depending on which API you’re using:- Gemini Developer API: Uses API keys
- Vertex AI API: Uses Google Cloud credentials (service accounts, Application Default Credentials)
Gemini Developer API Authentication
Using API Keys
The Gemini Developer API requires an API key for authentication.Direct API Key
Pass the API key directly to the client:Environment Variables
Set the API key using environment variables:If both
GOOGLE_API_KEY and GEMINI_API_KEY are set, GOOGLE_API_KEY takes precedence.Getting an API Key
To obtain a Gemini API key:- Visit Google AI Studio
- Sign in with your Google account
- Create a new API key or use an existing one
- Copy the API key for use in your application
Vertex AI Authentication
Using Application Default Credentials (ADC)
Vertex AI uses Google Cloud’s Application Default Credentials for authentication. The SDK automatically discovers credentials in the following order:- Environment variable
GOOGLE_APPLICATION_CREDENTIALS - User credentials from
gcloud auth application-default login - Compute Engine/App Engine/Cloud Run service account
- Cloud SDK credentials
Basic Vertex AI Client
Setting Up ADC
For Local Development
Use the Google Cloud SDK to set up your credentials:Using a Service Account Key File
For production or automated environments, use a service account:- Create a service account in the Google Cloud Console
- Download the JSON key file
- Set the environment variable:
Using Environment Variables
You can configure Vertex AI authentication entirely through environment variables:Explicit Credentials
Pass credentials explicitly to the client:Service Account Credentials
Load service account credentials directly:Project and Location Configuration
For Vertex AI
Vertex AI requires a Google Cloud project ID and location.Your Google Cloud project ID. Find it in the Google Cloud Console
or by running
gcloud config get-value project.The Google Cloud location/region where requests will be sent (e.g.,
us-central1, europe-west1).
See available locations.Using Environment Variables
Direct Configuration
Custom Base URLs
For advanced use cases like API gateway proxies:When using a custom base URL, you may need to bypass standard authentication checks.
This is typically used for API gateway proxies or testing environments.
Quota Projects
For Vertex AI, you can specify a quota project for billing:Authentication Best Practices
For production deployments:
- Use service accounts with minimal required permissions
- Rotate API keys and service account keys regularly
- Use secret management services (Google Secret Manager, AWS Secrets Manager, etc.)
- Enable audit logging for credential usage
For local development:
- Use
gcloud auth application-default loginfor Vertex AI - Store API keys in
.envfiles (add to.gitignore) - Use separate credentials for development and production
IAM Permissions (Vertex AI)
For Vertex AI, ensure your credentials have the necessary IAM permissions:Required Roles
- Vertex AI User (
roles/aiplatform.user) - For making API calls - Storage Object Viewer (
roles/storage.objectViewer) - For accessing files in Cloud Storage
Grant Permissions
Troubleshooting
Common Issues
”API key not valid”
- Verify your API key is correct
- Check if the API key has been restricted to specific APIs
- Ensure the Gemini API is enabled for your API key
”Permission denied” (Vertex AI)
- Verify your credentials have the required IAM roles
- Check if the Vertex AI API is enabled in your project:
“Could not find default credentials”
- Set up Application Default Credentials:
- Or set
GOOGLE_APPLICATION_CREDENTIALSenvironment variable
”Invalid project ID”
- Verify your project ID is correct
- Ensure billing is enabled for your project
- Check that you have access to the project
Environment Variable Reference
Gemini Developer API
| Variable | Description | Example |
|---|---|---|
GOOGLE_API_KEY | Gemini API key (takes precedence) | AIza... |
GEMINI_API_KEY | Alternative Gemini API key | AIza... |
Vertex AI
| Variable | Description | Example |
|---|---|---|
GOOGLE_GENAI_USE_VERTEXAI | Enable Vertex AI mode | true |
GOOGLE_CLOUD_PROJECT | Google Cloud project ID | my-project-123 |
GOOGLE_CLOUD_LOCATION | Google Cloud location | us-central1 |
GOOGLE_APPLICATION_CREDENTIALS | Path to service account key | /path/to/key.json |