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.

By the end of this guide you will have aisdk/google installed, your Gemini API key wired up, and a working PHP script that calls Gemini, prints the generated text, and reports token usage. The whole process takes about five minutes and requires nothing beyond PHP 8.3+ and Composer.
1

Install the package

Add aisdk/google to your project using Composer. This also installs aisdk/core, which provides the Generate builder and all shared interfaces:
composer require aisdk/google
2

Set your API key

Export your Gemini API key as an environment variable before running your script. The provider will pick it up automatically:
export GOOGLE_GENERATIVE_AI_API_KEY=your-api-key-here
No API key yet? Visit Google AI Studio to create one for free. The provider also accepts GEMINI_API_KEY as a fallback if you already have that variable set.
3

Generate your first response

Create a file called generate.php and paste the following complete example:
<?php

require __DIR__ . '/vendor/autoload.php';

use AiSdk\Generate;
use AiSdk\Google;

$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
Run it from your terminal:
php generate.php
You should see a concise explanation of PHP closures printed to stdout. The Google::model() call lazily initialises the provider from your environment variable and returns a TextModelInterface that Generate::text() uses to route the request to Gemini.
Google::model() delegates to Google::default(), which calls Google::create() on first use. If you need to configure the provider explicitly — for example, to point at a different base URL — call Google::create(['apiKey' => '...', 'baseUrl' => '...']) before your first Google::model() call.
4

Inspect token usage

The response object exposes token counts for every request, which is useful for cost tracking and rate-limit management. Extend your script to print usage information:
<?php

require __DIR__ . '/vendor/autoload.php';

use AiSdk\Generate;
use AiSdk\Google;

$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text . PHP_EOL;

echo 'Input tokens:  ' . $result->usage->inputTokens . PHP_EOL;
echo 'Output tokens: ' . $result->usage->outputTokens . PHP_EOL;
$result->usage->inputTokens reflects the tokens consumed by your prompt and instructions; $result->usage->outputTokens reflects the tokens in the model’s reply.

Streaming

For long responses — or anywhere you want to display output progressively — swap ->run() for ->stream(). The SDK handles the server-sent-event connection internally and exposes an iterable chunks() generator. You can still call ->run() on the stream object at the end to get the complete TextModelResponse with usage data:
<?php

require __DIR__ . '/vendor/autoload.php';

use AiSdk\Generate;
use AiSdk\Google;

$stream = Generate::text('Tell me a story.')
    ->model(Google::model('gemini-3.5-flash'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;
}

$result = $stream->run();
Each $chunk is a plain string containing the next piece of generated text as it arrives from the API, so you can echo or buffer it immediately without waiting for the full response.

Next Steps

Streaming

Learn how streaming works in depth, including flushing output buffers and handling errors mid-stream.

Image Generation

Generate images from text prompts using Google::image() and save them directly to disk.

Tool Calling

Register PHP callables as tools and let Gemini decide when and how to invoke them.

Authentication

Configure API keys, custom base URLs, and additional HTTP headers programmatically.

Build docs developers (and LLMs) love