Skip to main content
Lesson 4 of 14 · Grok Code & Editors

xAI API and OpenAI compatibility

Lesson 4 of 14 Updated July 29, 2026 Developer
On this page
  1. A familiar API
  2. Minimal configuration
  3. Get an API key
  4. Available endpoints
  5. Compatibility limitations
  6. Key takeaways

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:

  1. Base URL: replace https://api.openai.com/v1 with https://api.x.ai/v1
  2. 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

  1. Go to console.x.ai
  2. Log in with your xAI account (or create one at accounts.x.ai)
  3. In the API Keys section, generate a new key
  4. 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/completions with stream: true: streaming with reasoning_content
  • /v1/chat/completions with tools: 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_content is specific to the xAI API and does not exist in OpenAI’s API

Key takeaways

  • The xAI API is at https://api.x.ai/v1 and 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