Grok is xAI’s large language model, available through the xAI API. As of 2026, Grok-2 and Grok-3 are competitive with the top frontier models on coding and reasoning benchmarks. If you are already familiar with the OpenAI API, you will find xAI’s API immediately familiar — it is intentionally API-compatible, so migrating existing code takes minutes rather than hours.
Getting API access
Go to console.x.ai and sign in with your X (Twitter) account. From the dashboard, navigate to API Keys and generate a new key. xAI offers a free tier with a monthly credit allocation — enough to experiment and build a proof of concept without entering payment details. The base URL for the xAI API is https://api.x.ai/v1. All requests use Bearer token authentication with your API key.
Available models
| Model | Context window | Best for |
|---|---|---|
| grok-2 | 131,072 tokens | General use, fast responses |
| grok-2-vision | 32,768 tokens | Image understanding + text |
| grok-3 | 131,072 tokens | Complex reasoning, code generation |
| grok-3-mini | 131,072 tokens | Cost-efficient, faster latency |
Basic API call
Because the xAI API is OpenAI-compatible, you can use the OpenAI SDK and just point it at a different base URL:
from openai import OpenAI
client = OpenAI(
api_key="your-xai-api-key",
base_url="https://api.x.ai/v1"
)
response = client.chat.completions.create(
model="grok-3",
messages=[
{"role": "system", "content": "You are a helpful technical assistant."},
{"role": "user", "content": "Explain how WebSockets differ from HTTP polling"}
],
max_tokens=1000
)
print(response.choices[0].message.content)
// Node.js / TypeScript
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-3',
messages: [{ role: 'user', content: 'Write a TypeScript function to debounce a function call' }],
});
console.log(response.choices[0].message.content);
Using the vision model
Grok-2-vision accepts images alongside text. Pass image data as a base64-encoded string or as a URL:
import base64
from openai import OpenAI
client = OpenAI(api_key="your-xai-api-key", base_url="https://api.x.ai/v1")
with open("screenshot.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
response = client.chat.completions.create(
model="grok-2-vision",
messages=[{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_data}"}},
{"type": "text", "text": "Describe what you see and identify any UI issues"}
]
}]
)
print(response.choices[0].message.content)
Streaming responses
stream = client.chat.completions.create(
model="grok-3",
messages=[{"role": "user", "content": "Write a comprehensive README for a Python CLI tool"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
Environment variables and security best practices
Keep your xAI API key in an environment variable, never hardcoded in source files. A .env file with python-dotenv or Node’s dotenv package is standard for local development. In production, use a secrets manager — AWS Secrets Manager, HashiCorp Vault, or your cloud provider’s equivalent — and inject the key at runtime rather than baking it into your container image.
# .env file (add to .gitignore — never commit this)
XAI_API_KEY=xai-your-key-here
# Python
from dotenv import load_dotenv
import os
load_dotenv()
client = OpenAI(api_key=os.getenv("XAI_API_KEY"), base_url="https://api.x.ai/v1")
# Node.js
import 'dotenv/config';
const client = new OpenAI({ apiKey: process.env.XAI_API_KEY, baseURL: 'https://api.x.ai/v1' });
If you accidentally commit a key, rotate it immediately in the xAI console — treat a committed key as compromised regardless of whether the repository is private.
How Grok compares to other frontier models
Grok’s main differentiators in 2026 are real-time access to X/Twitter data (useful for social media context tasks), competitive coding performance on HumanEval and similar benchmarks, and an OpenAI-compatible API that makes switching easy. For teams already in the OpenAI ecosystem, adding Grok as a fallback or comparison model is minimal effort. For the full trade-offs between frontier models, see the post on how to pick the right AI model for your use case. Rate limits and pricing are on the xAI console and update frequently — check before building cost estimates into a business case.
For more developer tools and utilities to streamline your workflow, check out the free browser-based DevTools at atebyteapps.com — including tools for encoding, hashing, JWT debugging, and more. Working with AI APIs pairs well with tools that let you quickly inspect and validate the data flowing through your system.