The xAI API: Building with Grok
- Describe what makes the xAI API different from OpenAI and other AI APIs
- Authenticate to the xAI API and make a chat completion call using the OpenAI SDK
- Identify advanced API features including function calling, streaming, and batch mode
Why Developers Choose xAI
The xAI API gives you programmatic access to the Grok model family through a REST interface that is compatible with the OpenAI Python and JavaScript SDKs. If you have already built with OpenAI's API, switching to xAI requires changing one line: the base URL. The same authentication pattern, the same message format, and the same function calling interface all work with xAI without changes.
What you gain by switching — or by adding xAI as a second provider — is real-time data. Every xAI API call has live X and web search built in. This means you can build AI applications that answer questions about what is happening right now without building a separate search integration. The context window is also large: Grok 4.3, the primary model, supports one million tokens.
Getting Your API Key
Visit console.x.ai — xAI's developer console. Create an account or sign in with your X credentials. Navigate to API Keys and create a new key. Copy it immediately; xAI does not show the full key again after creation.
All API requests authenticate with a Bearer token in the Authorization header:
Authorization: Bearer xai-your-api-key-here Base URL: https://api.x.ai/v1
The Model Lineup
As of mid-2026, the primary models available through the xAI API are:
Older model slugs (grok-4.1, grok-4-fast, grok-code-fast-1) were retired in May 2026 and now route to grok-4.3. Always check docs.x.ai for current slugs before publishing a production integration.
Your First API Call
A minimal chat completion call in Python using the OpenAI SDK:
from openai import OpenAI client = OpenAI( api_key="xai-your-key-here", base_url="https://api.x.ai/v1" ) response = client.chat.completions.create( model="grok-4.3", messages=[{"role": "user", "content": "What is happening in AI this week?"}] ) print(response.choices[0].message.content)
This is identical to an OpenAI call except for the base_url and api_key. Because Grok has live web search built in, the question "this week" will draw from current information — no additional configuration or search integration needed.
Advanced Features
- Function calling — Define tools and have Grok call them when it needs data. Works identically to OpenAI function calling, using the same tools array format.
- Streaming — Add stream=True for token-by-token output as it is generated. Identical to OpenAI streaming.
- System messages — Pass a system message in the messages array to set the model's persona, instructions, and constraints.
- Batch mode — Submit large volumes of requests at a reduced per-token cost. Useful for offline processing: document classification, bulk summarisation, large-scale data extraction.
- Server-side tools — Enable built-in tools like web search and code execution within the API call, without a client-side function calling loop.
- Long context — Send up to one million tokens in a single Grok 4.3 request. Useful for full codebase analysis, large document sets, or long conversation histories.
The Grok Imagine API
The xAI API also exposes image and video generation through the grok-imagine model endpoint. This lets you add Aurora's image generation and Grok Imagine's video capabilities to any application programmatically. Pricing is per image or video generated rather than per token. Check docs.x.ai for the current Imagine endpoint documentation and rate limits.
Try This
Set up a free account at console.x.ai, generate an API key, and run the minimal Python example above. Then modify the message to a time-sensitive question — something happening this week — and observe how live web access appears in the response. Next, add a system message: "You are a research assistant. Always cite your sources and note how recent each one is." Notice how the output structure changes. This three-step sequence confirms that the live search is working and that the API is responding to system-level instructions.
- The xAI API is OpenAI-compatible — change the base_url and api_key and your existing OpenAI code works with Grok
- Grok 4.3 is the primary model at $1.25/$2.50 per million tokens with a one-million-token context window; older slugs were retired May 2026
- Live X and web search is built into every xAI API call — no separate search integration needed for real-time data