How to Get a Grok (xAI) API Key & a Cheaper Way to Use It
Getting a Grok API key takes about two minutes: create one on the xAI console, point the standard OpenAI SDK at https://api.x.ai/v1, and you are calling Grok with your existing code. This guide walks the exact steps, lists the current Grok model ids and their published pricing (verified against xAI's July 2026 docs), and shows the OpenAI-compatible request in Python and JavaScript. Then it covers the honest trade-off: one xAI key locks you to one vendor, so we also show the gateway alternative — a single key that reaches Grok and 300+ other models with automatic failover, no per-provider signup.
Get a Grok API key in 4 steps
Create the key on the xAI console, then point the standard OpenAI SDK at https://api.x.ai/v1 — that is the whole flow. The short version:
- Sign in to the xAI console API Keys page.
- Create a new key, name it, and copy the value (shown once).
- Export it as
XAI_API_KEYin your environment. - Use the OpenAI client with
base_url="https://api.x.ai/v1"and that key.
Every number and endpoint below is verified against xAI's official docs (July 2026); the rest of this guide expands each step, lists the current model ids and pricing, and covers the one thing xAI won't tell you — how to avoid locking your app to a single provider.
What a Grok API key actually is
It is a secret bearer token that authenticates your requests to xAI's Grok models over HTTPS. Grok is the family of large language models built by xAI; the API lets your own code send prompts to models like grok-4.3 and get completions back, the same way you'd call OpenAI or Claude. The key travels in the Authorization: Bearer <key> header on every request to the base URL https://api.x.ai/v1. Treat it like a password: anyone with the key can spend on your account, so keep it out of client-side code and source control. If you want the fastest possible path to trying Grok without any of the vendor-specific setup, skip to the gateway option, which uses one key for Grok and hundreds of other models.
Create the key on the xAI console
Keys are created on the xAI console's API Keys page — not in the Grok chat app. The exact steps, matching xAI's quickstart:
| # | Step | Where |
|---|---|---|
| 1 | Sign in / create an xAI account | console.x.ai |
| 2 | Open the API Keys page | console.x.ai/team/default/api-keys |
| 3 | Click Create API Key, give it a name | API Keys page |
| 4 | Copy the key value immediately (shown once) | Modal dialog |
| 5 | Store it as an env var, e.g. XAI_API_KEY | Your shell / secrets manager |
Set it in your shell so it never touches your code:
export XAI_API_KEY="xai-...your-key..."
Call Grok with the OpenAI SDK
The xAI API is OpenAI-SDK-compatible: change two lines — base_url and the key — and existing OpenAI code calls Grok. xAI documents this directly, with both Python and JavaScript OpenAI-SDK examples pointing baseURL at https://api.x.ai/v1. Python:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["XAI_API_KEY"],
base_url="https://api.x.ai/v1",
)
resp = client.chat.completions.create(
model="grok-4.3",
messages=[{"role": "user", "content": "Explain the token bucket algorithm in one line."}],
)
print(resp.choices[0].message.content)
JavaScript is the same shape — only baseURL and the key change:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: "https://api.x.ai/v1",
});
const resp = await client.chat.completions.create({
model: "grok-4.3",
messages: [{ role: "user", content: "Hello, Grok" }],
});
console.log(resp.choices[0].message.content);
Because the surface is OpenAI-compatible, anything already written against the OpenAI client — retries, streaming, tool calls — carries over. If you're curious about what "OpenAI-compatible" guarantees and where the edges are, our OpenAI-compatible API guide maps exactly which fields port cleanly.
Grok models, context & pricing
As of xAI's July 2026 docs, the Chat API exposes grok-4.3 and the grok-4.20 family at $1.25 / $2.50 per 1M tokens; the coding model is cheaper. The full verified table:
| Model id | API | Context | Input / 1M | Output / 1M |
|---|---|---|---|---|
grok-4.3 | Chat | 1M | $1.25 | $2.50 |
grok-4.20-0309-reasoning | Chat | 1M | $1.25 | $2.50 |
grok-4.20-0309-non-reasoning | Chat | 1M | $1.25 | $2.50 |
grok-4.20-multi-agent-0309 | Chat | 1M | $1.25 | $2.50 |
grok-build-0.1 | Code | 256k | $1.00 | $2.00 |
A few things to note: the four Chat models share one price, so model choice within the family is about behaviour (reasoning vs non-reasoning vs multi-agent), not cost. The 1M-token context window is generous — long documents and large codebases fit without chunking. And grok-build-0.1 is a separate coding-focused model on a distinct Code API, priced below the chat family. All figures are from xAI's official models page; see our fuller Grok 4 model page for the gateway view. If you're weighing Grok against the similarly-named Groq inference platform, that is a different company entirely — our Grok vs Groq piece untangles the two.
A worked cost example
At $1.25 in / $2.50 out, a typical RAG-style call costs a fraction of a cent — here is the arithmetic on grok-4.3. Say each request sends 8,000 input tokens (a system prompt plus retrieved context) and generates 800 output tokens:
| Component | Tokens | Rate / 1M | Cost |
|---|---|---|---|
| Input | 8,000 | $1.25 | $0.0100 |
| Output | 800 | $2.50 | $0.0020 |
| Per request | 8,800 | — | $0.0120 |
| 100,000 requests | — | — | $1,200 |
So one request is about 1.2 cents, and 100k of them is roughly $1,200. Output tokens cost 2× input, so verbose responses dominate the bill faster than large contexts do — cap max_tokens and prompt for concision when you can. For a side-by-side of where Grok lands against other providers on price, see our cheapest LLM API comparison.
One key for Grok + 300 other models
A direct xAI key only reaches Grok — through an OpenAI-compatible gateway, one key reaches Grok and 300+ other models, with failover when xAI is rate-limited or down. The setup is identical to the direct call; you just swap the base URL and use your gateway key:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_DATALLMLAB_KEY",
base_url="https://www.datallmlab.com/v1", # one key, 300+ models
)
resp = client.chat.completions.create(
model="x-ai/grok-4", # or any other model id
messages=[{"role": "user", "content": "Hello"}],
)
Why route this way instead of holding a separate key per vendor:
- No per-provider signup. Grok, Claude, GPT and open models sit behind one account and one bill — no separate xAI, OpenAI and Anthropic keys to manage.
- Automatic failover. If xAI returns a rate limit or an outage, the request reroutes to an equivalent model so your users never see the error — the mechanics are in our routing & failover guide.
- Same OpenAI SDK. It's still the OpenAI-compatible surface, so you change one line versus your direct-Grok code. See what an LLM gateway is for the full picture.
You keep the exact ergonomics of the xAI call — you just stop tying your uptime to a single provider. When rate limits do bite, our rate-limit playbook covers the direct-provider fixes too.
Call Grok on one key — with 300+ models behind it
DataLLM Lab is OpenAI-compatible: swap your base URL to https://www.datallmlab.com/v1 and reach Grok plus 300+ models on a single key, with automatic failover when a provider is down.
When a single xAI key is the right call
If Grok is the only model you'll ever call and vendor uptime isn't business-critical, a direct xAI key is the simplest thing that works. A gateway earns its keep when you use more than one model, want failover, or need one bill and one dashboard across providers. If none of that applies — a hobby project, a single-model prototype, or a workload contractually pinned to xAI — the direct key skips a hop and keeps your dependency list short. The honest rule: direct key for single-model simplicity; gateway the moment you have two models or an uptime SLA. For choosing which model to standardise on in the first place, our best LLM API guide and best coding LLM roundup compare the current field.
FAQ
How do I get a Grok API key?
Sign in to the xAI console, open the API Keys page, create a key, name it, and copy the value immediately — it's shown once. Store it as XAI_API_KEY and send it as a bearer token to https://api.x.ai/v1.
What is the Grok API base URL?
It's https://api.x.ai/v1. The API is OpenAI-SDK-compatible, so you use the standard OpenAI client and set base_url (or baseURL) to that URL with your xAI key.
Is the Grok API OpenAI-compatible?
Yes. xAI's docs show Python and JavaScript OpenAI-SDK examples that only change base_url to https://api.x.ai/v1 and the key to your xAI key. Existing OpenAI-SDK code runs against Grok with a two-line change.
What are the current Grok model ids?
Per xAI's July 2026 docs: grok-4.3, grok-4.20-0309-reasoning, grok-4.20-0309-non-reasoning and grok-4.20-multi-agent-0309 on the Chat API (1M context each), plus the coding model grok-build-0.1 on the Code API (256k context).
How much does the Grok API cost?
grok-4.3 and all grok-4.20 variants are $1.25 per 1M input tokens and $2.50 per 1M output tokens. The coding model grok-build-0.1 is $1.00 input / $2.00 output per 1M tokens.
Can one key reach Grok and other models?
Not an xAI key alone — it only reaches Grok. Through an OpenAI-compatible gateway like DataLLM Lab, one key reaches Grok plus 300+ models, and a rate-limited or down xAI request fails over to an equivalent model automatically.
DataLLM Lab