xAI API and OpenAI compatibility
On this page
A familiar API
The xAI API uses the same format as the OpenAI API. If you’ve worked with the OpenAI API or compatible tools before, there’s nothing new to learn. The only thing that changes is the base URL.
https://api.x.ai/v1
This compatibility is a strategic choice by xAI. Rather than imposing a new proprietary format, they adopted the de facto industry standard. As a result, any tool, SDK, or editor that supports the OpenAI API can use grok-build-0.1 immediately.
Minimal configuration
To switch from OpenAI to xAI in any compatible client, you need to change only two parameters:
- Base URL: replace
https://api.openai.com/v1withhttps://api.x.ai/v1 - API key: use your xAI API key (obtained from
console.x.ai)
Everything else—message format, temperature parameters, response structure—is identical.
Example with the OpenAI Python SDK
from openai import OpenAI
client = OpenAI(
api_key="your-xai-key",
base_url="https://api.x.ai/v1"
)
response = client.chat.completions.create(
model="grok-build-0.1",
messages=[
{"role": "system", "content": "You are a programming assistant."},
{"role": "user", "content": "Write a quicksort function in Python."}
]
)
You use the standard OpenAI SDK without any modifications. Only base_url and the model name change.
Example with curl
curl https://api.x.ai/v1/chat/completions \
-H "Authorization: Bearer your-xai-key" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-build-0.1",
"messages": [
{"role": "user", "content": "Explain the difference between let and const in JavaScript"}
]
}'
Get an API key
- Go to console.x.ai
- Log in with your xAI account (or create one at accounts.x.ai)
- In the API Keys section, generate a new key
- Copy it immediately—it won’t be shown again
Keep this key secure. Never commit it to a Git repository or share it in team chat. Use an environment variable or a secrets manager.
Available endpoints
grok-build-0.1 supports the following endpoints, all in OpenAI format:
/v1/chat/completions: text and code generation (the most commonly used)/v1/chat/completionswithstream: true: streaming withreasoning_content/v1/chat/completionswithtools: native tool calling
Compatibility limitations
A few minor differences exist:
- The available models are xAI’s (grok-build-0.1, grok-4.5, etc.), not OpenAI’s
- Some OpenAI-specific parameters (such as
logprobs) may not be supported reasoning_contentis specific to the xAI API and does not exist in OpenAI’s API
Key takeaways
- The xAI API is at
https://api.x.ai/v1and is compatible with the OpenAI format - You only need to change the base URL and API key to migrate
- The OpenAI Python SDK works as-is
- Get the API key at console.x.ai
- All OpenAI-compatible editors work with xAI