This guide walks you from a blank project to a working OpenAI integration in five minutes. You will install the package, configure authentication, generate your first text response, stream tokens as they arrive, and produce an image — all using the clean, fluent API provided byDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/core and aisdk/openai.
Install the package
Require
aisdk/openai via Composer. It will pull in aisdk/core ^0.2 and any other dependencies automatically:Set your API key
Export your OpenAI API key as an environment variable before running any scripts:The provider reads
OPENAI_API_KEY automatically. You can also pass it programmatically via OpenAI::create(['apiKey' => 'sk-...']) if you prefer explicit configuration.Generate text
Call If you want to set a default model once and omit the
Generate::text(), attach a model handle, add instructions and a prompt, then call run(). The response is synchronous and the generated content is available on $result->text:->model() call on every request, use the Generate::model() shorthand:Stream tokens
Call
->stream() instead of ->run() to receive a stream object. Iterate over $stream->chunks() to print each token as it arrives, then call ->run() on the same stream to obtain the fully assembled result:Next Steps
Tool Calling
Bind PHP callables to tools and let the model invoke them automatically.
Structured Output
Use
json_schema mode to receive typed, validated objects from the model.Configuration
Customise base URLs, organisation headers, and per-request provider options.
Streaming
Build real-time UIs by processing tokens as they stream from the API.