Is the Grok API Compatible with OpenAI-Style Clients?

Updated

Yes. The Grok API exposes an OpenAI-compatible endpoint at https://api.x.ai/v1, so you can point the OpenAI SDK at it and call chat.completions with a Grok model name. You still need an xAI API key, and the model string must be a Grok model (for example grok-4.7) rather than an OpenAI one.

What "OpenAI-compatible" means here

Compatibility is at the HTTP and SDK level, not the account level. The API accepts the same request shape OpenAI clients send — a model field plus a messages array — and returns a response with choices[0].message.content. That means existing code that already talks to OpenAI's chat completions can be redirected by changing two things: the base URL and the API key.

From the x.ai site, the documented base URL is:

https://api.x.ai/v1

Calling Grok with the OpenAI SDK

The site shows a JavaScript example using the official openai package, with baseURL overridden:

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://api.x.ai/v1",
});

const response = await client.chat.completions.create({
  model: "grok-4.7",
  messages: [{ role: "user", content: "Explain quantum computing" }],
});

console.log(response.choices[0].message.content);

The same pattern applies in Python: construct the OpenAI client with base_url="https://api.x.ai/v1" and your xAI key, then call client.chat.completions.create(...) with a Grok model.

Raw HTTP equivalent

If you prefer no SDK at all, the endpoint is a standard POST:

curl https://api.x.ai/v1/chat/completions \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4.7",
    "messages": [{ "role": "user", "content": "Explain quantum computing" }]
  }'

Input: your key in the Authorization header, a Grok model name, and a messages array. Expected result: a JSON body whose choices[0].message.content holds the reply.

Native SDKs as an alternative

OpenAI compatibility is one of several ways in. x.ai also documents first-party and ecosystem SDKs:

Approach Import / package Notes
OpenAI SDK openai Override baseURL to https://api.x.ai/v1
xAI Python SDK xai_sdk Uses Client(api_key=...) and client.chat.create(model=...)
Vercel AI SDK @ai-sdk/xai Uses xai.responses("grok-4.7") with generateText

The Python SDK example from the site:

import os
from xai_sdk import Client
from xai_sdk.chat import user

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(model="grok-4.7")
chat.append(user("Explain quantum computing"))
response = chat.sample()
print(response.content)

The Vercel AI SDK example:

import { xai } from "@ai-sdk/xai";
import { generateText } from "ai";

const { text } = await generateText({
  model: xai.responses("grok-4.7"),
  prompt: "Explain quantum computing",
});
console.log(text);

Choosing between the two paths

  • Already have OpenAI code? Use the OpenAI SDK with the overridden base URL — smallest change, and you keep your existing request/response handling.
  • Starting fresh or want xAI-specific features? The native xai_sdk or @ai-sdk/xai may fit better, since they're built around xAI's own client surface.
  • Migrating a mixed codebase? The OpenAI-compatible route lets you swap providers per call by changing the client config rather than rewriting call sites.

Practical points to check

  • Key: you need an xAI API key (XAI_API_KEY in the examples). An OpenAI key will not work against api.x.ai.
  • Model name: pass a Grok model such as grok-4.7. The site lists 5+ model families and describes a single unified API covering text, code, voice, images, and video.
  • Scale signals from the site: 1M+ API calls per day and a median latency under 200ms — useful context if you're estimating whether the endpoint fits a latency-sensitive workload.
  • Pricing and access terms: the site links to a Pricing page, but the provided material doesn't state specific rates or whether a free tier exists. Check https://x.ai/pricing before assuming cost or free access.

If your goal is to reuse OpenAI-style client code with minimal edits, the compatible base URL plus a Grok model name is the whole change. If you want xAI-native tooling, the dedicated SDKs are the documented alternative.