This page walks you through addingDocumentation 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 to a PHP project: from running the Composer command and understanding what gets installed, to setting your API key and verifying that the provider is ready to use.
Prerequisites
Before you install, make sure you have:- PHP 8.3 or higher — check with
php --version. - Composer — the standard PHP dependency manager. Download it from getcomposer.org if needed.
- An xAI API key — sign up and generate a key at console.x.ai.
Install
Run the following command in your project root:What Gets Installed
When you requireaisdk/xai, Composer also installs two packages that it depends on:
aisdk/core ^0.2— the shared generation pipeline, fluent builder API (Generate::text(),Generate::image()), and provider contracts. All provider-agnostic logic — request construction, streaming, tool-call loops, structured output handling — lives here.aisdk/openai-compatible ^0.2— a reusable HTTP adapter for OpenAI-compatible APIs. The xAI provider delegates all network communication to this adapter, targeting xAI’s endpoint (https://api.x.ai/v1) with Bearer-token authentication.
composer.json directly; they are transitive dependencies managed automatically.
Environment Setup
The recommended way to supply your API key is through theXAI_API_KEY environment variable. The provider reads it automatically when no key is passed to XAI::create():
XAI::model() or XAI::image() without any explicit configuration:
XAI::create():
Verify
Once the package is installed and your key is set, run this short snippet to confirm the provider initialises correctly:xai printed without errors, the package is installed and configured correctly. You are ready to make your first Grok API call.