Skip to main content

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

The 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

apiKey
string
required
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.
baseUrl
string
default:"https://api.x.ai/v1"
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.
headers
array<string, string>
default:"[]"
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.
sdk
Sdk|null
default:"null"
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:
use AiSdk\XAI;

XAI::create([
    'apiKey'  => 'xai-...',
    'baseUrl' => 'https://api.x.ai/v1',
    'headers' => ['X-Custom-Header' => 'value'],
]);
Any key absent from the array falls back to its environment variable or default value, so a minimal call only needs apiKey when the environment variable is not set:
XAI::create(['apiKey' => 'xai-...']);

Environment variables

VariableRequiredDefaultDescription
XAI_API_KEYYesBearer token for xAI API authentication.
XAI_BASE_URLNohttps://api.x.ai/v1Base URL for all API requests.
Setting 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:
use AiSdk\XAI;

// Lazily bootstraps from XAI_API_KEY / XAI_BASE_URL env vars.
$provider = XAI::default();
XAI::reset() clears the singleton, setting it back to null. This is primarily useful in tests that need a clean slate between cases:
afterEach(function () {
    XAI::reset();
});

Build docs developers (and LLMs) love