# xAI API

CreateWithAI Hub page: https://createwithaihub.com/apis-sdks/xai-api
Latest version of this file: https://createwithaihub.com/apis-sdks/xai-api.md

- Type: API / SDK · Category: APIs & SDKs
- Publisher: SpaceXSI (xAI)
- Pricing: Paid
- Runs on: API (cloud)

xAI's (now SpaceXSI) OpenAI-compatible API for Grok text, image, video and voice models, with Python and TypeScript SDKs.

The xAI API (called the SpaceXSI API in newer docs) serves Grok models at api.x.ai through Responses, Chat Completions, Images, Videos, Voice, Files, Batches and Models endpoints, and states compatibility with the OpenAI REST API. Server-side tools add web search, X search, code execution and collections search. Official SDKs exist for Python (gRPC-based) and TypeScript, and the docs also show the OpenAI SDKs and the Vercel AI SDK provider.

Useful for: Switching an existing OpenAI SDK integration to Grok by changing the base URL, or generating images and videos from a backend.

## Key features

- Responses and Chat Completions endpoints, compatible with the OpenAI REST API
- Image and video generation and editing endpoints (Grok Imagine)
- Voice API for speech-to-speech, speech-to-text and text-to-speech
- Server-side Web Search, X Search, code execution and remote MCP tools
- Batch API at 20% off standard rates for Grok 4.x models
- Official xai-sdk (Python) and TypeScript SDKs, Apache-2.0

## Connection

- Modality: Text, Image, Audio, Video, Multimodal
- SDK language: Python, TypeScript / JS

## Before you start

- console.x.ai account with credits
- API key (XAI_API_KEY)

## Install steps from the official documentation

Python SDK:

```
pip install xai-sdk
```

Vercel AI SDK provider:

```
npm install ai @ai-sdk/xai zod
```

OpenAI SDK (Python):

```
pip install openai
```

## Limitations

- Usage is prepaid: the account must be loaded with credits before making requests
- Server-side tools are billed per call on top of token costs
- Not every model accepts Batch API requests

## Official links

- Website: https://x.ai
- Source repository: https://github.com/xai-org/xai-sdk-python
- Documentation: https://docs.x.ai/developers/quickstart
- Source: https://docs.x.ai/developers/quickstart (Prepaid credits, XAI_API_KEY, install commands, base URL https://api.x.ai/v1, image generation example)
- Source: https://docs.x.ai/developers/rest-api-reference (Endpoint families, base URLs, compatibility with the OpenAI REST API)
- Source: https://docs.x.ai/developers/pricing (Tool call prices; Batch API 20% off for grok-4.7, 4.6, 4.5, 4.3 and 4.20 models; image and video per-unit prices)
- Source: https://github.com/xai-org/xai-sdk-python (Official gRPC-based Python SDK, pip install xai-sdk, Python 3.10+, Apache-2.0)
- Source: https://github.com/xai-org (Official org lists xai-sdk-python and xai-sdk-ts (both Apache-2.0))
- Source: https://docs.x.ai/developers/advanced-api-usage/batch-api (Batch API: only batch-enabled models accepted)
- Source: https://docs.x.ai/console/billing (Prepaid credits; requests rejected when credits run out)
