TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/google/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/google provider authenticates every request to the Gemini API by attaching your API key as the x-goog-api-key HTTP header. This header is constructed inside GoogleOptions::authHeaders(), which merges the resolved key with any extra headers you supply, and is sent automatically on every outbound request — you never need to set it manually.
Environment Variables
The recommended way to supply credentials in any environment is through environment variables. The provider reads the following variables at runtime:| Variable | Description | Required |
|---|---|---|
GOOGLE_GENERATIVE_AI_API_KEY | Primary Gemini API key | Yes (or GEMINI_API_KEY) |
GEMINI_API_KEY | Fallback API key variable | Yes (or GOOGLE_GENERATIVE_AI_API_KEY) |
GOOGLE_GENERATIVE_AI_BASE_URL | Override the API base URL | No |
Key Resolution Order
WhenGoogleOptions::fromArray() is called, the provider resolves the API key using the following precedence — the first non-empty value wins:
- Explicit
apiKeyin the config array — value passed directly toGoogle::create(['apiKey' => '...']). GEMINI_API_KEYenvironment variable — checked via$_ENV,$_SERVER, andgetenv().GOOGLE_GENERATIVE_AI_API_KEYenvironment variable — the canonical variable name, resolved last via the SDK’sEnv::loadApiKey()helper.
Programmatic Configuration
You can also configure the provider directly in code usingGoogle::create(). This is useful in tests, multi-tenant applications, or situations where credentials come from a secret manager rather than the process environment:
Google::create() call constructs a GoogleProvider by passing your config array to GoogleOptions::fromArray() and stores it as the default provider instance for the current process. Subsequent calls to Google::model() and Google::image() use this instance automatically.
All supported keys in the config array:
| Key | Type | Description |
|---|---|---|
apiKey | string | Explicit API key — takes highest precedence over env vars |
baseUrl | string | Override the base URL for all requests |
headers | array<string, string> | Extra HTTP headers merged into every request |
sdk | Sdk|null | Optional SDK instance for advanced HTTP configuration |
Custom Headers
If you need to attach additional HTTP headers to every request — for example, a proxy token, a request-tracing ID, or an internal audit header — pass aheaders array to Google::create():
GoogleOptions::authHeaders() calls array_merge(['x-goog-api-key' => $this->apiKey], $this->headers), meaning the API key header is always present and your custom headers are appended after it. If you supply a key that conflicts with x-goog-api-key, your value will override it — so take care not to accidentally clobber authentication.
Base URL Override
The provider defaults tohttps://generativelanguage.googleapis.com/v1beta for all API calls. You can replace this with any compatible endpoint — a local proxy, a corporate gateway, or a test server — using either the environment variable or the config array key.
Both the
GOOGLE_GENERATIVE_AI_BASE_URL environment variable and the baseUrl config key are supported. The config key takes precedence over the environment variable. Trailing slashes are stripped automatically from whichever value is used.