By the end of this guide you will haveDocumentation 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 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.
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:Set your API key
Export your Gemini API key as an environment variable before running your script. The provider will pick it up automatically:
Generate your first response
Create a file called Run it from your terminal:You should see a concise explanation of PHP closures printed to stdout. The
generate.php and paste the following complete example: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.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:
$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:
$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.