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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package xai-sdk-php

xAI Logo

xAI PHP SDK

An unofficial PHP SDK for xAI's APIs

Packagist Version License PHP Version


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

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

  1. Synchronous Only (v1.0) - Async support planned for v2.0
  2. REST API - Uses REST with JSON instead of gRPC
  3. Iterator Pattern - Uses PHP iterators for streaming
  4. 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


All versions of xai-sdk-php with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
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
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package displace/xai-sdk-php contains the following files

Loading the files please wait ...