Download the PHP package displace/xai-sdk-php without Composer
On this page you can find all versions of the php package displace/xai-sdk-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package xai-sdk-php
The xAI PHP SDK is a modern PHP library for interacting with xAI's APIs. Built for PHP 8.4 and above, it provides full parity with the official Python SDK, offering an intuitive and developer-friendly interface.
Whether you're generating text, images, or structured outputs, the xAI PHP SDK is designed to be robust, type-safe, and production-ready.
Documentation
Comprehensive API documentation is available at docs.x.ai. Explore detailed guides, API references, and tutorials to get the most out of the xAI SDK.
Installation
Install via Composer:
Requirements
- PHP 8.4 or higher
ext-jsonextensionext-mbstringextension
Quick Start
Client Instantiation
The SDK looks for the XAI_API_KEY environment variable by default:
Multi-Turn Chat
The SDK supports multi-turn conversations with a simple append method to manage conversation history:
Streaming
Stream responses in real-time for interactive applications:
Image Understanding
Analyze images with vision models:
Features
The xAI PHP SDK provides comprehensive feature coverage:
| Feature | Description | Example |
|---|---|---|
| Chat Completions | Multi-turn conversations with stateful management | chat.php |
| Streaming | Real-time token streaming for responsive UIs | streaming.php |
| Image Understanding | Vision model support with URLs and base64 | image_understanding.php |
| Image Generation | Generate images from text prompts | image_generation.php |
| Function Calling | Define tools the model can invoke | function_calling.php |
| Structured Outputs | Type-safe JSON responses with schemas | structured_outputs.php |
| Reasoning Models | Extended thinking with configurable effort | reasoning.php |
| Server-Side Tools | Web search, X search, code execution | server_side_tools.php |
| Search (X & Web) | Real-time X and web search via /v1/responses | search.php |
| Collections (RAG) | Document management and semantic search | collections.php |
| Telemetry | OpenTelemetry tracing integration | telemetry.php |
Configuration
Client Options
Chat Configuration
Environment Variables
| Variable | Description |
|---|---|
XAI_API_KEY |
Your xAI API key |
XAI_SDK_DISABLE_TRACING |
Set to 1 to disable telemetry tracing |
XAI_SDK_DISABLE_SENSITIVE_TELEMETRY_ATTRIBUTES |
Set to 1 to exclude prompts/responses from traces |
OTEL_EXPORTER_OTLP_ENDPOINT |
OTLP endpoint URL for telemetry export |
OTEL_EXPORTER_OTLP_HEADERS |
Authentication headers for OTLP |
OTEL_EXPORTER_OTLP_PROTOCOL |
Protocol: grpc or http/protobuf |
Telemetry & Observability
The SDK includes optional OpenTelemetry integration for monitoring and debugging:
Console Export (Development)
OTLP Export (Production)
Installation Requirements
Error Handling
The SDK provides typed exceptions for different error scenarios:
| Exception | HTTP Status | Description |
|---|---|---|
AuthenticationException |
401 | Invalid or missing API key |
RateLimitException |
429 | Rate limit exceeded |
BadRequestException |
400 | Invalid request parameters |
ApiException |
Various | General API errors |
XaiException |
- | Base exception class |
Available Models
| Model | Description | Use Case |
|---|---|---|
grok-3 |
Latest Grok model | General chat, complex tasks |
grok-3-mini |
Reasoning model | Math, logic, step-by-step problems |
grok-2-vision |
Vision model | Image understanding and analysis |
grok-2-image |
Image generation | Creating images from prompts |
Use the Models API to retrieve available models:
Development
Running Tests
Code Quality
Comparison with Python SDK
The PHP SDK maintains API parity with the official Python SDK:
| Feature | Python SDK | PHP SDK |
|---|---|---|
| Client | Client() / AsyncClient() |
XaiClient() |
| Chat | client.chat.create() |
$client->chat->create() |
| Streaming | chat.stream() |
$chat->stream() |
| Messages | user(), system(), assistant() |
user(), system(), assistant() |
| Images | image(url) |
image($url) |
| Tools | tool() |
new Tool() |
| Telemetry | Telemetry() |
new Telemetry() |
PHP-Specific Adaptations
- Synchronous Only (v1.0) - Async support planned for v2.0
- REST API - Uses REST with JSON instead of gRPC
- Iterator Pattern - Uses PHP iterators for streaming
- Strict Types - Leverages PHP 8.4+ readonly properties
License
The xAI PHP SDK is distributed under the Apache License 2.0.
Attribution
This SDK was inspired by and maintains API compatibility with X.AI LLC's official Python SDK for the xAI API. We thank X.AI LLC for their excellent work on the original SDK design.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Support
- Issues: GitHub Issues
- Documentation: docs.x.ai
- Examples: /examples directory
All versions of xai-sdk-php with dependencies
ext-json Version *
ext-mbstring Version *
guzzlehttp/guzzle Version ^7.8
guzzlehttp/psr7 Version ^2.6
psr/http-client Version ^1.0
psr/http-factory Version ^1.0
psr/http-message Version ^2.0
psr/log Version ^3.0