The Grok API does not require an X Premium subscription. That's a common assumption because Grok started life inside X, but the developer platform is now a standalone product at console.x.ai. You sign up with an email, you get $25 in promotional credits, you pay per token after that.
This guide walks through the actual flow as it exists in May 2026.
Step 1: Create an xAI Account
Navigate to console.x.ai and sign up. You can register with:
- Email and password
- Google SSO
- X account (this links your X subscription status to xAI but doesn't change anything about API access)
Sign-in is self-serve. There's no waiting period, no manual approval queue, and no Premium subscription requirement. Within a few minutes of signup, you'll have access to the developer console.

Step 2: Complete the Initial Setup
On first login, the console will prompt you for:
- Your name and (optionally) organization
- A short description of your intended use case
- Acceptance of xAI's API terms and usage policies
You may also be offered the data sharing program during onboarding. Opting in lets xAI use your API inputs for model training, in exchange for $150/month in ongoing API credits. New accounts get $25 in promotional credits regardless of whether you opt in. If you handle sensitive data, leave this off. If you're prototyping, take the credits.
Step 3: Where to Find Your xAI API Key
Once in the console, navigate to API Keys in the left sidebar (or go directly to console.x.ai/team/default/api-keys).
The API Keys page shows:
- All existing keys with creation dates and last-used timestamps
- The team and permission scope each key belongs to
- Buttons to create, rotate, or revoke keys

Step 4: Create Your Grok API Key

Click "Create API Key" and configure:
- Key name: descriptive, like "Production Backend", "Local Dev", or "CI/CD Pipeline". You'll thank yourself later when you're auditing usage.
- Team: if you're in multiple teams, pick the right one. Keys are scoped to a single team.
- Endpoint access: select which endpoints the key can hit (chat completions, image generation, voice, etc.).
- Model access: select which Grok models the key can use. Most users select all.
Step 5: Copy and Save Your Grok API Key
The key is shown once. xAI does not store the secret value, so if you close the dialog without copying, you'll need to revoke and create a new one.
Grok API keys start with xai- followed by a long alphanumeric string. The fastest way to use it is to set it as an environment variable:
export XAI_API_KEY="xai-..."
The official xAI Python SDK and the AI SDK both default to reading the XAI_API_KEY environment variable, so once it's set, you don't pass the key in code.
Storage recommendations:
- Use
.envfiles in development (and add.envto.gitignore) - Use a secrets manager in production (AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager)
- Never commit keys to Git, even in private repos
- Never paste keys into Slack, email, or shared docs
Step 6: Add a Payment Method (Optional)
Your $25 in promotional credits will keep you running for a while at Grok 4.1 Fast pricing, but credits expire 30 days after account creation and don't carry over. Add a payment method before they expire if you want uninterrupted service.
In the console, go to Billing → Payment Methods. Add a credit card. Configure spend controls:
- Hard monthly cap: the number above which xAI stops accepting requests for the rest of the month. Set this. A runaway loop without a cap is expensive.
- Usage alerts: set notifications at 50%, 75%, and 90% of your monthly cap.
- Auto-recharge: optional. Convenient for production, dangerous for experimentation.
xAI uses pay-as-you-go pricing with no minimum commitment and no monthly subscription fee. The API and the consumer Grok subscriptions (SuperGrok, X Premium) are completely separate billing tracks. Subscribing to one doesn't affect the other.
Step 7: Test Your Grok API Key
With your key set as an environment variable, run a single curl request:
curl https://api.x.ai/v1/chat/completions \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.3",
"messages": [
{"role": "user", "content": "Hello, Grok!"}
]
}'

A successful response returns Grok's reply in JSON. Two things to know:
- xAI's API is OpenAI-compatible. The endpoint, request shape, and
Authorization: Bearerheader all match OpenAI's. You can point the OpenAI Python SDK athttps://api.x.ai/v1and it will work. - The base endpoint is
https://api.x.ai/v1. Don't confuse this withconsole.x.ai(the dashboard) orgrok.com(the consumer chatbot).
Is the Grok API Free?
The first $25 is. After that, you pay per token.
xAI offers $25 in promotional credits to new accounts, plus an additional $150/month if you opt into the data sharing program. Promotional credits expire 30 days after signup. Data sharing credits refresh monthly.
There is no permanent free tier in the OpenAI-style sense. Once your credits run out, you need a payment method on file.
If you want to use Grok without paying for API access, the consumer products are:
- grok.com — the standalone chatbot (free tier with limits)
- SuperGrok — $30/month for higher rate limits and access to Grok Heavy
- X Premium / Premium+ — bundles Grok access with X platform features
These are completely separate from API access and don't grant API credits.
Which Grok Model Should You Use?
The current production lineup as of Q2 2026:
- Grok 4.3 (current default): xAI's most intelligent and fastest model. Their official recommendation for new API integrations. Strong at reasoning, coding, and agentic tool use.
- Grok 4.20: 2 million token context window, $1.25/M input and $2.50/M output. Industry-leading speed and a low hallucination rate. Solid balance of cost and quality.
- Grok 4.1 Fast: optimized for cost. $0.20/M input, $0.50/M output. Cheaper per token than GPT-5 mini and Gemini Flash, with a 2 million token context window. Default choice for most cost-sensitive production workloads.
- Grok 4: the previous flagship. $3.00/M input, $15.00/M output. Better suited for complex reasoning where accuracy matters more than cost.
In the API, you reference models by their model string (e.g. grok-4.3, grok-4.20, grok-4.1-fast, grok-4). xAI also supports aliases:
<modelname>is aliased to the latest stable version<modelname>-latestis aliased to the latest version including previews<modelname>-<date>pins to a specific release for workflows that need version stability
Models you might see referenced in older tutorials (grok-1, grok-1.5, grok-2) are deprecated. Calls to them either fail or get auto-routed to a current model depending on your account.
xAI API Key Format
Grok API keys all start with the prefix xai- followed by a long alphanumeric string. If your key doesn't start with xai-, it's not an xAI key. Project-scoped keys may have additional sub-prefixes.
Real-Time Data: How It Actually Works
Grok's reputation is built on real-time access to X and the web. In the API, you don't get this through a magic flag. You get it through server-side search tools.
When you call the API with search tools enabled, Grok decides whether to query the web or X to answer your prompt, returns the cited results inline, and bills you per tool invocation on top of the standard token cost.
Tool pricing as of Q2 2026 is up to $5 per 1,000 successful calls (xAI cut these in half recently). Available tools include:
- Web Search — pulls current pages from the live web
- X Search — pulls posts from X
- Code Interpreter — sandbox for code execution
- File Search — search through xAI vector stores (collections)
You enable these by passing them in the tools array of your request, in the same shape OpenAI uses for function calling. There is no enable_x_context: true parameter. There is no include_trending: true parameter. Anything you read suggesting otherwise is fabricated or hallucinated by an older AI-generated guide.
Security Best Practices
A leaked Grok API key on a public repo can drain a budget overnight. The basics:
- Rotate keys quarterly, not just when you suspect a leak
- Use a different key per environment. Dev, staging, and prod should never share a key
- Restrict scope. Limit each key to the endpoints and models it actually needs
- Monitor usage daily through the console
- Set a hard monthly cap so a runaway loop can't bankrupt you
- Enable 2FA on your xAI account. It's the recovery vector if your key is compromised
Migrating from OpenAI, Anthropic, or Gemini to Grok
The xAI API is OpenAI-compatible, so migrating from OpenAI is the smallest possible code change.
| OpenAI | Anthropic | Gemini | Grok | Perplexity | |
|---|---|---|---|---|---|
| Auth header | Authorization: Bearer | x-api-key | x-goog-api-key | Authorization: Bearer | Authorization: Bearer |
| Endpoint | api.openai.com/v1/responses | api.anthropic.com/v1/messages | generativelanguage.googleapis.com/v1beta/... | api.x.ai/v1/chat/completions | api.perplexity.ai/chat/completions |
| Request shape | messages array | messages + system | contents with parts | messages (OpenAI-compatible) | messages (OpenAI-compatible) |
| Env var | OPENAI_API_KEY | ANTHROPIC_API_KEY | GEMINI_API_KEY | XAI_API_KEY | PERPLEXITY_API_KEY |
| Key format | sk-... | sk-ant-... | AIza... | xai-... | pplx-... |
| Built-in web search? | no | no | no | optional via tools | yes (Sonar) |
| Citations included? | no | no | no | optional | yes (Sonar) |
| OpenAI SDK works? | ✓ | ✗ | ✗ | ✓ | ✓ |
To migrate from OpenAI, the actual code change is two lines:
from openai import OpenAI
client = OpenAI(
api_key=os.environ["XAI_API_KEY"],
base_url="https://api.x.ai/v1",
)
Then call client.chat.completions.create(model="grok-4.3", ...) and the rest of your OpenAI code works.
Troubleshooting Common Errors
401 Unauthorized: invalid API key — The key is wrong, expired, or revoked. Check the header format (Authorization: Bearer xai-...), check for trailing whitespace, and regenerate at console.x.ai if needed.
404 model not found — You're calling a deprecated model (grok-1, grok-2, etc.) or a model your account doesn't have access to. Migrate to grok-4.3 or grok-4.1-fast.
429 rate_limit_exceeded — You're sending requests faster than your tier allows. Implement exponential backoff. Tier upgrades happen automatically as you build spend history.
402 payment_required — Your promotional credits ran out and there's no payment method on file. Add one in the console.
$0.05 usage guideline violation fee — xAI charges this when a request is caught violating usage guidelines before generation in the Responses API. Review your prompt and the xAI usage policy.
FAQ
Where do I find my xAI API key? At console.x.ai/team/default/api-keys. Existing keys are listed there, but the secret value is only shown at creation. If you've lost a key, revoke it and create a new one.
How do I get a Grok API key for the first time?
Sign up at console.x.ai, complete the short onboarding (use case, terms acceptance), then go to API Keys → Create API Key. Copy the xai-... string immediately. The flow is self-serve and takes about three minutes.
Do I need X Premium to use the Grok API? No. The Grok API and X Premium are completely separate. You can have one without the other. Older guides claiming X Premium is required for API access are out of date.
Is the Grok API free? There is no permanent free tier. New accounts get $25 in promotional credits (expires after 30 days), plus $150/month if you opt into the data sharing program. After that, it's pay-per-token.
What's the difference between SuperGrok and the Grok API? SuperGrok is a consumer chatbot subscription on grok.com. The Grok API is for developers building applications. They share the same underlying models but separate billing, separate rate limits, and separate accounts.
What does an xAI API key look like?
A string starting with xai- followed by alphanumeric characters. Don't share it.
How do I use Grok with the OpenAI SDK?
xAI's API is OpenAI-compatible. Point the OpenAI client at https://api.x.ai/v1, pass your XAI_API_KEY, and call models with their xAI names (grok-4.3, etc.). Most OpenAI code works unchanged.
How do I get real-time X data through the API?
Use server-side search tools (Web Search and X Search) in the tools array of your request. There is no boolean flag for "enable X context" — that's a fabrication from older guides.
How do I set the XAI_API_KEY environment variable in Python?
export XAI_API_KEY="xai-..."
Then in Python:
from xai_sdk import Client
client = Client() # picks up XAI_API_KEY from env
chat = client.chat.create(model="grok-4.3")
chat.append({"role": "user", "content": "Hello, Grok!"})
print(chat.sample())
Next Steps
With your key working, the official docs are the source of truth:
If you're integrating Grok across multiple SaaS tools (CRM, accounting, HRIS, file storage), look at how unified APIs reduce the per-vendor integration work.
Ready to get started?
Scale your integration strategy and deliver the integrations your customers need in record time.








