· reAPI Team
Grok API Key: How to Get One and What the Grok API Costs
Get a Grok API key from console.x.ai, call api.x.ai with the OpenAI SDK, and budget with current Grok 4.7 token prices, batch and priority rates, and tool fees.

A Grok API key comes from xAI's developer console. Sign up at console.x.ai, add credits to the account, create a key on the API Keys page and store it as XAI_API_KEY. The API runs at https://api.x.ai/v1, and the OpenAI SDK works with that base URL.[1] xAI's documentation now names the company SpaceXSI, but the console, docs and API domains are unchanged.[1]
The model xAI recommends for code, chat and everything outside image, video and voice is grok-4.7, priced at $2 per million input tokens and $6 per million output tokens for prompts under 200,000 tokens.[3] The rest of this guide covers the setup steps, the full Grok API price list as of November 4, 2026, and the charges people tend to miss.
How to get a Grok API key
xAI's quickstart has four steps:[1]
- Create an account at
console.x.aiand load it with credits. - Open the API Keys page in the console and create a key.
- Export it as
XAI_API_KEY, or put it in a.envfile. - Install an SDK. xAI documents its own
xai-sdkfor Python, the Vercel AI SDK provider@ai-sdk/xai, and the standard OpenAI SDKs.
With the OpenAI Python SDK, the only changes from an OpenAI setup are the key and the base URL:
from openai import OpenAI
client = OpenAI(api_key="<XAI_API_KEY>", base_url="https://api.x.ai/v1")
resp = client.chat.completions.create(
model="grok-4.7",
messages=[{"role": "user", "content": "Explain the bug: function median(a){a.sort();return a[a.length/2]}"}],
)
print(resp.choices[0].message.content)xAI's quickstart notes that the same grok-4.7 model powers agentic coding in Grok Build, its coding product, so if you already use Grok Build, the API gives you that same model inside your own code. Keep the key server-side. Anyone holding it can spend the credits on the account.
Grok API pricing
Text model prices per million tokens, from xAI's pricing page:[3]
| Model | Context | Input | Cached input | Output |
|---|---|---|---|---|
grok-4.7 | 500K | $2.00 | $0.50 | $6.00 |
grok-4.6 | 500K | $2.00 | $0.50 | $6.00 |
grok-4.5 | 500K | $2.00 | $0.30 | $6.00 |
grok-4.3 | 1M | $1.25 | $0.20 | $2.50 |
grok-4.20-0309-reasoning and -non-reasoning | 1M | $1.25 | $0.20 | $2.50 |
grok-build-0.1 | 256K | $1.00 | $0.20 | $2.00 |
Every model in that table doubles its rates once a prompt reaches 200,000 tokens, and the higher rate applies to all tokens in that request, not only the part above the threshold. For grok-4.7, a long prompt costs $4 input and $12 output per million.[3]
Three modifiers change the bill:[3]
- Batch API: 20% off every token type for the Grok 4.x text models, with most jobs finishing within 24 hours. Batch requests do not count toward rate limits. For
grok-4.7that works out to $1.60 input and $4.80 output per million. Image and video jobs sent through Batch are billed at standard rates. - Priority Processing: 2x standard rates, so $4 and $12 for
grok-4.7, billed only when the response confirms"service_tier": "priority". Cache discounts apply before the multiplier. - Server-side tools: web search costs $5 per 1,000 calls, X Search $5 per 1,000 posts and $10 per 1,000 profiles returned, and collections search $2.50 per 1,000 calls, on top of tokens. X Search counts every post returned, including parent and quoted posts.
Voice is priced separately: speech-to-speech with grok-voice-3.0 costs $0.08 per minute plus $0.004 per text input, speech-to-text $0.10 per hour over REST or $0.20 streaming, and text-to-speech $15 per million characters.[2]
Grok API price for a typical request
Take a request with 20,000 input tokens and 2,000 output tokens. On grok-4.7 that is 0.02 × $2 + 0.002 × $6 = $0.052. On grok-4.3 it is 0.02 × $1.25 + 0.002 × $2.50 = $0.03. Add one web search and the grok-4.7 request rises by $0.005. These are arithmetic from list prices, not measured invoices; reasoning tokens and cache hits change the real figure.
Which Grok model to call
xAI's own guidance is short: use Grok 4.7 for code and chat, and the dedicated models for images, video and voice.[2] The older models still have uses. grok-4.3 has a 1M-token context and costs less than half as much per output token, and grok-build-0.1 is the cheapest text model on the list at $1 and $2.[3]
Two details catch people moving from other providers. Grok has no knowledge of events after its training data unless you enable the web or X search tools. And logprobs and top_logprobs are silently ignored by grok-4.20 and newer models. On the Responses API, grok-4.7 always returns reasoning.encrypted_content, even if you did not ask for it. Image inputs can be JPEG or PNG up to 20 MiB each, with no limit on how many you send.[2] A faster "Grok 4.7 Fast" exists, but xAI says it is available only in Cursor and Grok Build, not on the public API.[3]
What grok-4.7 supports on the API
The model page lists text and image input with text output, a 500,000-token context, function calling, structured outputs and Batch support.[4] Reasoning effort accepts low, medium, high and xhigh, and the default is high. That default matters for cost, because reasoning tokens are billed as output: set low or medium for simple tasks if you do not need deep reasoning.
The documented rate limits are 150 requests per second and 50 million tokens per minute. The model runs in three regions, us-east-1, us-west-2 and us-central-1, which is worth checking if your data has to stay in a particular jurisdiction.[4]
FAQ
Is there a free Grok API key?
xAI's quickstart asks you to load credits before using the API, and its pricing page lists no free API tier for the text models.[1][3]
Is the Grok API OpenAI-compatible?
Yes for the common case: xAI's own examples use the OpenAI SDK with base_url="https://api.x.ai/v1".[1]
How much does the Grok API cost per image or video?
xAI lists Grok Imagine Image 2.0 at $0.04 per image and the original Grok Imagine Image at $0.02. Video is billed per second: $0.020 for Grok Imagine Video 1.5 Lite, $0.050 for Grok Imagine Video and $0.080 for Grok Imagine Video 1.5.[2]
Grok Imagine without a Grok API key
reAPI does not offer Grok's text models. It does serve the Grok Imagine family, including Grok Imagine Image 2.0 and Grok Imagine Video 1.5, on the same key as its other image, video and text models; current rates are on each model page. For Grok 4.7 and the other text models, a Grok API key from console.x.ai is the direct route.
References
- xAI (SpaceXSI). Quickstart. Retrieved November 4, 2026 from docs.x.ai/developers/quickstart
- xAI (SpaceXSI). Models. Retrieved November 4, 2026 from docs.x.ai/developers/models
- xAI (SpaceXSI). Pricing. Retrieved November 4, 2026 from docs.x.ai/developers/pricing
- xAI (SpaceXSI). Grok 4.7 model page. Retrieved November 4, 2026 from docs.x.ai/developers/models/grok-4.7