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.

XAIOptions lives in the AiSdk\XAI namespace and is a readonly value object that carries every piece of configuration that XAIProvider, XAITextModel, and XAIImageModel need to authenticate and communicate with the xAI API. Build one either through its constructor or, more conveniently, via the fromArray() static factory which resolves missing values from environment variables automatically.

Namespace and import

use AiSdk\XAI\XAIOptions;

Constants

ConstantValue
XAIOptions::DEFAULT_BASE_URL'https://api.x.ai/v1'
XAIOptions::PROVIDER_NAME'xai'

Constructor parameters

public function __construct(
    public readonly string $apiKey,
    public readonly string $baseUrl = self::DEFAULT_BASE_URL,
    public readonly array  $headers = [],
    public readonly ?Sdk   $sdk = null,
)
apiKey
string
required
Bearer token used to authenticate every request to the xAI API. When using fromArray() without supplying this key, the value is loaded from the XAI_API_KEY environment variable. An exception is thrown if neither source provides a non-empty string.
baseUrl
string
default:"https://api.x.ai/v1"
Root URL of the xAI API. Trailing slashes are stripped automatically. Override via the XAI_BASE_URL environment variable or by passing baseUrl to fromArray(). You rarely need to change this unless you are pointing at a self-hosted or proxy endpoint.
headers
array<string, string>
default:"[]"
Extra HTTP headers merged into every outgoing request on top of the standard Authorization header. Useful for request tracing, custom routing, or other middleware requirements.
sdk
?Sdk
default:"null"
Optional PSR-18 HTTP client wrapper (AiSdk\Support\Sdk). When null, the provider uses the default HTTP client bundled with the SDK. Pass a custom instance to control timeouts, proxies, or to inject a mock client in tests.

fromArray()

public static function fromArray(array $config = []): XAIOptions
Static factory that builds an XAIOptions instance from a plain associative array. Any key that is absent or null falls back to the corresponding environment variable, and then to the built-in default. Accepted keys: apiKey, baseUrl, headers, sdk.
$options = XAIOptions::fromArray([
    'apiKey'  => 'xai-...',
    'baseUrl' => 'https://api.x.ai/v1',
    'headers' => ['X-Request-ID' => 'abc123'],
]);
config
array<string, mixed>
default:"[]"
Associative array with any combination of the keys apiKey, baseUrl, headers, and sdk. Missing keys are resolved from environment variables (XAI_API_KEY, XAI_BASE_URL) or built-in defaults.
return
XAIOptions
A fully resolved, immutable XAIOptions instance.

authHeaders()

public function authHeaders(): array<string, string>
Returns the complete set of authentication headers to attach to every request. The result is the Authorization: Bearer … header merged with any additional headers supplied in the headers constructor parameter. Keys from headers take precedence in case of collision.
// Returns:
// ['Authorization' => 'Bearer xai-...', ...extra headers]
$headers = $options->authHeaders();
return
array<string, string>
Merged array of Authorization and any extra headers. Safe to pass directly to an HTTP request builder.

Build docs developers (and LLMs) love