How to Get a Grok API Key (xAI API Key) and Make Your First Request (October 2026)
APIsQuick answer: Sign up at console.x.ai, load credits, then create a key on the API Keys page. Export it as XAI_API_KEY and call https://api.x.ai/v1 with model grok-4.7. It works with the OpenAI SDK. A Grok or X subscription does not cover API usage: xAI bills it separately at the rates on its models page (accessed October 25, 2026).
How do you get a Grok API key?
These are the steps in xAI’s quickstart. xAI’s developer docs now carry the SpaceXSI name, but the console and API domains are still x.ai.
- Create an account at console.x.ai and load it with credits.
- Open the API Keys page in the console and create a key.
- Copy the key once and store it in a password manager or secrets store.
- Set it as an environment variable:
export XAI_API_KEY="your_api_key"
Or put XAI_API_KEY=your_api_key in a .env file, and add .env to .gitignore.
How do you make your first Grok API request?
With curl:
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "grok-4.7", "input": "Explain recursion in one sentence."}'
With the OpenAI Python SDK (pip install openai):
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv("XAI_API_KEY"), base_url="https://api.x.ai/v1")
response = client.responses.create(model="grok-4.7", input="Explain recursion in one sentence.")
print(response.output_text)
xAI also has its own Python SDK (pip install xai-sdk) and a Vercel AI SDK provider (npm install ai @ai-sdk/xai zod).
Which Grok models can you call?
| Use case | Model (per xAI’s models page) | Notes |
|---|---|---|
| Code and chat | grok-4.7 |
Flagship; 500k-token context; configurable reasoning; knowledge cutoff May 2026 |
| Images | grok-imagine-image-2.0 |
/v1/images/generations; 1K and 2K output |
| Video | Grok Imagine Video 1.5 | 480p, 720p and 1080p |
| Voice | Grok Voice API | Speech-to-speech, text-to-speech and speech-to-text |
Model names follow aliases: <model> points to the latest stable version, <model>-latest to the newest, and <model>-<date> pins a fixed release. Grok has no live knowledge unless you enable the Web Search or X Search tools.
How is the Grok API billed?
- Pay as you go: text models bill per million input and output tokens; Imagine bills per image or per second of video; Voice bills per minute, character or hour. Rates are on the models page.
- Subscriptions are separate: per the x.ai pricing FAQ, paid plans “include Grok API access”, but API usage “is billed separately at standard model rates and doesn’t draw from your plan’s usage pool”.
- Enterprise: xAI’s sales team offers custom rate limits, dedicated infrastructure and volume pricing.
How do you keep a Grok API key safe?
Never put the key in front-end code or a public repo. Use one key per app so you can revoke it without breaking others, and rotate it if it leaks. If you get 401 or 429 errors, see how to fix AI API errors. Setting up other providers too? See how to get an OpenAI API key and how to get a Claude API key.
FAQ
Is the Grok API free?
No. The quickstart says to load your console account with credits before you start, and requests are billed per token at the rates on xAI's models page.
Does SuperGrok or The X Subscription include the Grok API?
xAI says paid plans include Grok API access, but API usage is billed separately at standard model rates and does not draw from the plan's usage pool.
Is the Grok API compatible with the OpenAI SDK?
Yes. Point the OpenAI SDK at https://api.x.ai/v1 with your xAI key. xAI's examples use the Responses API (client.responses.create).
Which Grok model should I call?
xAI recommends grok-4.7 for code, chat and everything except media. Use grok-imagine-image-2.0 for images, Grok Imagine Video 1.5 for video and the Voice API for speech.
How do I authenticate requests?
Send your key as a Bearer token in the Authorization header: Authorization: Bearer $XAI_API_KEY.
Sources
- xAI (SpaceXSI) Docs: Quickstart (accessed 2026-10-25)
- xAI (SpaceXSI) Docs: Models and pricing (accessed 2026-10-25)
- x.ai: Pricing (The X Subscription and API FAQ) (accessed 2026-10-25)