How to Get Started with the Grok API on x.ai
Updated
You can make your first Grok API call in a few minutes: create an API key in the x.ai API Console, then send a chat completion request to https://api.x.ai/v1 using the xAI SDK, the Vercel AI SDK, any OpenAI-compatible client, or plain curl. All modalities — text, code, voice, images, and video — are exposed through a single unified API, and the current model named in the site's examples is grok-4.7. The steps below cover the prerequisites, the exact request shapes shown on x.ai, how to verify the call worked, and where people typically get stuck.
Prerequisites
- An x.ai account with access to the API Console (the site lists "API Console" and "Documentation" under Developers).
- An API key stored as an environment variable. The examples use
XAI_API_KEY, so export it before running anything:export XAI_API_KEY="your-key-here" - A working Python or Node.js environment if you use an SDK;
curlneeds nothing beyond a shell.
Step 1: Get an API key
From the x.ai navigation, open Developers → API Console and create a key there. The site's call-to-action for new users is "Get API Key," and the same entry point is used for "Get API Access." Treat the key as a secret — every example below reads it from the environment rather than hardcoding it.
Step 2: Choose a client
x.ai documents four interchangeable ways to call the same endpoint. Pick based on your stack:
| Client | Import / entry | Notes |
|---|---|---|
| xAI Python SDK | from xai_sdk import Client |
Native SDK; create a chat with client.chat.create(model="grok-4.7") |
| Vercel AI SDK | import { xai } from "@ai-sdk/xai" |
Use with generateText; model via xai.responses("grok-4.7") |
| OpenAI-compatible client | new OpenAI({ baseURL: "https://api.x.ai/v1" }) |
Reuse existing OpenAI code by changing the base URL and key |
curl |
https://api.x.ai/v1/chat/completions |
No dependencies; good for a first smoke test |
Step 3: Make a first call
Each snippet sends the same prompt — "Explain quantum computing" — to grok-4.7.
Python (xAI SDK)
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)
TypeScript (Vercel AI SDK)
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);
TypeScript (OpenAI-compatible)
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);
curl
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: the model name and a messages array (or a prompt, in the SDK form). Action: a POST to the chat completions endpoint with your bearer token. Expected result: a response object whose content is the model's answer, printed to your console.
Step 4: Verify it worked
- The script prints a text answer rather than an error. If you used
curl, you'll see a JSON body containing the completion. - A
401means the key is missing or wrong — confirmXAI_API_KEYis exported in the same shell that runs the command. - A
404or model error means the model name doesn't match what your account can access; the site's examples usegrok-4.7.
Common sticking points
- Hardcoding the key. All four examples read from the environment; follow that pattern so keys don't leak into source control.
- Forgetting the base URL. The OpenAI-compatible path only works because
baseURLis set tohttps://api.x.ai/v1. Without it, requests go to OpenAI. - Mixing SDK and raw HTTP shapes. The xAI Python SDK uses
chat.create/chat.sample, while the raw endpoint usesmessagesandchoices. Don't blend the two. - Assuming one model fits all. The site advertises 5+ model families and multiple modalities;
grok-4.7is just the model used in the current examples.
Where to go next
The x.ai site points to Documentation for deeper configuration and the API Console for key and account management. The published scale figures — 1M+ API calls per day, <200ms median latency, 5+ model families — give a sense of what the platform is built for. For pricing, the site links to a dedicated Pricing page; check it directly rather than assuming a tier, since the input doesn't state rates or free allowances.