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.

This page walks you through adding 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:
composer require aisdk/xai
Composer resolves and installs all dependencies automatically. No additional flags are required.

What Gets Installed

When you require aisdk/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.
You do not need to add either package to your composer.json directly; they are transitive dependencies managed automatically.

Environment Setup

The recommended way to supply your API key is through the XAI_API_KEY environment variable. The provider reads it automatically when no key is passed to XAI::create():
export XAI_API_KEY=xai-...
With the variable set, you can call XAI::model() or XAI::image() without any explicit configuration:
use AiSdk\XAI;

$model = XAI::model('grok-4.3'); // reads XAI_API_KEY from the environment
If you prefer to pass the key directly — for example, when the key comes from a secrets manager or a configuration object — use XAI::create():
use AiSdk\XAI;

XAI::create(['apiKey' => 'xai-...']);
You can also override the base URL if you are pointing at a proxy or a self-hosted endpoint:
use AiSdk\XAI;

XAI::create([
    'apiKey'  => 'xai-...',
    'baseUrl' => 'https://api.x.ai/v1',
]);

Verify

Once the package is installed and your key is set, run this short snippet to confirm the provider initialises correctly:
use AiSdk\XAI;

$provider = XAI::create(['apiKey' => 'xai-...']);

echo $provider->name(); // "xai"
If you see xai printed without errors, the package is installed and configured correctly. You are ready to make your first Grok API call.

Build docs developers (and LLMs) love