TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/xai/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/xai package is configured through XAIOptions, a value object that controls how the provider authenticates with the xAI API, where it sends requests, and which HTTP client it uses. You pass configuration as a plain array to XAI::create(), which calls XAIOptions::fromArray() internally to resolve and validate every option — including falling back to environment variables for secrets you should not hard-code.
Configuration options
Bearer token used to authenticate every request to the xAI API. When not
supplied explicitly,
XAIOptions::fromArray() reads the XAI_API_KEY
environment variable. A missing or empty key throws at construction time so
misconfiguration surfaces immediately.Base URL prepended to all API endpoint paths. Trailing slashes are stripped
automatically. You can also override this value via the
XAI_BASE_URL
environment variable without changing application code — useful for pointing
at a staging gateway or a local proxy.Additional HTTP headers merged into every outgoing request alongside the
Authorization header. Use this for custom tracing headers, organisation
identifiers, or any other header required by an API gateway sitting in front
of xAI.Optional
AiSdk\Support\Sdk value object that wraps a PSR-18
ClientInterface, a PSR-17 RequestFactoryInterface, and a PSR-17
StreamFactoryInterface. Inject one when you need custom timeouts, an HTTP
proxy, retry middleware, or a test double. When null, the provider uses the
HTTP client registered with the global Generate facade.Using XAI::create()
XAI::create() accepts the same configuration array, delegates parsing to
XAIOptions::fromArray(), stores the resulting provider as the package
singleton, and returns it. You can call it once during application bootstrap:
apiKey when the environment
variable is not set:
Environment variables
| Variable | Required | Default | Description |
|---|---|---|---|
XAI_API_KEY | Yes | — | Bearer token for xAI API authentication. |
XAI_BASE_URL | No | https://api.x.ai/v1 | Base URL for all API requests. |
XAI_API_KEY in the environment is the recommended approach for
production deployments — it keeps secrets out of source code and makes
rotating keys a matter of updating a single environment variable.
Default provider
XAI::default() returns the current singleton XAIProvider. If no provider
has been created yet, it lazily calls XAI::create() with no arguments,
relying entirely on environment variables:
XAI::reset() clears the singleton, setting it back to null. This is
primarily useful in tests that need a clean slate between cases: