Skip to main content

Documentation 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.

GoogleOptions is an immutable value object that holds all provider-level configuration for the aisdk/google package — the resolved API key, the API base URL, any custom HTTP headers, and an optional PSR-18 HTTP client override. Once constructed, none of its properties can be changed; to use different settings you create a new instance. In practice, GoogleOptions is created for you inside Google::create() via its fromArray() static factory, but you can also construct it directly when you need precise control.

Namespace

use AiSdk\Google\GoogleOptions;

Constants

DEFAULT_BASE_URL
string
The default Gemini REST API base URL used when no override is supplied through config or environment variables.
https://generativelanguage.googleapis.com/v1beta
PROVIDER_NAME
string
The canonical provider name string returned by GoogleProvider::name().
google

Constructor

public function __construct(
    public readonly string $apiKey,
    public readonly string $baseUrl = self::DEFAULT_BASE_URL,
    public readonly array $headers = [],
    public readonly ?Sdk $sdk = null,
)
All four parameters are exposed as public read-only properties. The constructor performs no validation or key resolution — use fromArray() when you want the full environment-variable fallback chain.
apiKey
string
required
The resolved Gemini API key that will be sent with every request via the x-goog-api-key header. Must be a non-empty string; pass a value returned by Env::loadApiKey() or resolved through fromArray() to guarantee this.
baseUrl
string
The API base URL. Trailing slashes are stripped by fromArray(); if you construct GoogleOptions directly, supply a clean URL without a trailing slash.
headers
array<string, string>
default:"[]"
Extra HTTP headers that are merged into every outgoing request alongside the authentication header. Both keys and values must be strings.
sdk
Sdk|null
default:"null"
An optional AiSdk\Support\Sdk instance wrapping a custom PSR-18 HTTP client and PSR-17 request/response factories. When null, the package constructs a default HTTP client. Primarily useful in tests for injecting a mock HTTP client.

Static Factory: fromArray()

public static function fromArray(array $config = []): self
The recommended way to create a GoogleOptions instance. Accepts a plain associative array of configuration values and resolves each setting through a prioritised fallback chain before calling the constructor.
config
array
default:"[]"
Associative configuration array. All keys are optional.
Returns self — a new GoogleOptions instance with all settings resolved.

API key resolution order

The apiKey is resolved in the following priority order, stopping at the first non-empty value found:
  1. $config['apiKey'] — explicit value passed to fromArray()
  2. GEMINI_API_KEY — checked in $_ENV, $_SERVER, then getenv()
  3. GOOGLE_GENERATIVE_AI_API_KEY — resolved via Env::loadApiKey()
If none of these sources yields a non-empty string, an exception is thrown.

authHeaders()

public function authHeaders(): array
Returns the complete set of HTTP headers that should be attached to every request made by this provider. The authentication header (x-goog-api-key) is always present as the first entry; any additional headers supplied via the headers constructor property are merged in afterwards using array_merge(), so custom headers can override the auth header if needed. Returns array<string, string> — merged authentication and custom headers.
$options = GoogleOptions::fromArray([
    'apiKey'  => 'my-api-key',
    'headers' => ['X-Environment' => 'production'],
]);

$headers = $options->authHeaders();
// [
//   'x-goog-api-key'  => 'my-api-key',
//   'X-Environment'   => 'production',
// ]

Complete Example

The following example shows GoogleOptions::fromArray() being called with every supported config key, followed by inspecting the resolved authHeaders().
use AiSdk\Google\GoogleOptions;

$options = GoogleOptions::fromArray([
    'apiKey'  => 'AIzaSy_your_key_here',
    'baseUrl' => 'https://generativelanguage.googleapis.com/v1beta',
    'headers' => [
        'X-Custom-Header' => 'my-value',
        'X-Request-ID'    => 'req-abc-123',
    ],
]);

// Inspect the resolved base URL.
echo $options->baseUrl;
// https://generativelanguage.googleapis.com/v1beta

// Inspect headers that will be sent with every HTTP request.
var_dump($options->authHeaders());
// array(3) {
//   ["x-goog-api-key"]    => string "AIzaSy_your_key_here"
//   ["X-Custom-Header"]   => string "my-value"
//   ["X-Request-ID"]      => string "req-abc-123"
// }

// Provider name constant.
echo GoogleOptions::PROVIDER_NAME; // google
When relying entirely on environment variables (the most common production setup), you can omit the config array entirely:
// Assumes GEMINI_API_KEY is set in the environment.
$options = GoogleOptions::fromArray();

echo $options->baseUrl; // https://generativelanguage.googleapis.com/v1beta

Build docs developers (and LLMs) love