Skip to content
, ,

How to Get a Grok API Key (xAI): Step-by-Step Guide for November 2026

10–15 minutes
Surprised woman in a green hoodie on a neon city street at night holding a Grok card, hook Grok API key in 5 minutes

Getting a Grok API key takes about five minutes, but there is one catch: xAI no longer gives every new account free credits. You need to load credits on console.x.ai before your first call works.

This guide walks you through every step for November 2026. You will set up a team, add credits, create a safe key, and make your first call to Grok 4.7 with curl and Python.

TLDR

  • Where: sign up at console.x.ai, then open the API Keys page of your team.
  • Cost: the API is prepaid. Requests fail until you buy credits. There is no general free tier.
  • Best model: grok-4.7 costs $2 input and $6 output per 1M tokens, with a 500K context window.
  • Base URL: https://api.x.ai/v1, which works with the OpenAI SDK and the official xai-sdk.
  • Safety: limit each key with ACLs, keep it in XAI_API_KEY, and never commit it to GitHub.

What You Need Before You Start

You do not need a SuperGrok plan or an X Premium plan to use the API. The API has its own billing in the xAI Console.

  • An account: an email address or an X account to sign in at console.x.ai.
  • A payment method: a card for prepaid credits. Bank transfers also work but take 2-3 business days.
  • A terminal: any shell with curl, or Python 3.10 or newer for the official SDK.
  • About $5: enough to test a lot of small prompts on Grok 4.7.

Note: xAI’s docs now carry the SpaceXSI company name after the merger with SpaceX. The console, API domain (api.x.ai) and model names have not changed.

Step 1: Create Your xAI Account and Team

Your xAI account is shared with grok.com, so if you already use the Grok chatbot you can sign in with the same login. Billing for the two stays separate.

  1. Open the console. Go to console.x.ai and click sign up or sign in.
  2. Pick a sign-in method. Use email or one of the other options shown. Signing up with X links your X account automatically.
  3. Check your team. A default team is created for you. API keys, credits and rate limits all belong to a team, not to your personal login.

Tip: If you sign up with a new method using an email that already has an account, xAI asks whether to link or create a new account. Link it. Data cannot be merged across accounts later.

Step 2: Add Credits (Is There Anything Free?)

The xAI quickstart is clear: sign up, then load the account with credits to start using the API. With the default settings, any request is rejected once your prepaid balance hits zero.

How to buy credits

  1. Open Billing. In the console sidebar, open the Billing section for your team.
  2. Add billing details first. Under Billing, then Payment, enter your billing address and tax details. xAI cannot regenerate invoices later.
  3. Buy prepaid credits. Choose an amount and pay. Credits go to the team you pick at checkout.
  4. Turn on auto top-up (optional). Set a threshold, a top-up amount (minimum $5) and a monthly maximum. You get a warning at 80% of that cap.

Monthly invoiced billing also exists, but it is off by default. Its spending limit starts at $0, and you request it through the Billing page or sales.

Free credits and the data sharing program

Older guides talk about $25 of monthly beta credits and a $150 per month data sharing program. The current docs.x.ai billing, pricing and FAQ pages do not mention any free credits as of November 2026.

  • Beta credits: the $25 public beta credit ended with the beta at the end of 2024.
  • Data sharing credits: not listed in the current docs. If your Billing page shows an offer, it applies to your team only.
  • Startups: xAI runs a separate Grok for Startups program at x.ai/startups for approved companies.

Warning: Prepaid credit purchases are generally not refundable, except where the law requires it. Start with a small top-up until you know your usage.

Step 3: Create Your Grok API Key

Once you have credits, the key itself takes a minute. The API Keys page lives at console.x.ai/team/default/api-keys for your default team.

  1. Open API Keys. In the console, choose the right team, then click API Keys in the sidebar.
  2. Click Create API Key. Give it a clear name, such as “blog-bot-prod” or “laptop-testing”.
  3. Set permissions. Choose which endpoints and models the key may use. Leave everything on only for quick local tests.
  4. Set limits (optional). Add per-key caps on requests per second, requests per minute or tokens per minute.
  5. Copy the key once. Save it in a password manager right away. Treat it like a password.
  6. Store it as an env var. Put it in XAI_API_KEY, which the official SDK reads by default.
# macOS / Linux
export XAI_API_KEY="xai-your-key-here"

# Windows PowerShell
$env:XAI_API_KEY = "xai-your-key-here"

What the key permissions (ACLs) mean

Under the hood, each key has a list of ACL strings. The console fills these in for you, and the Management API lets you set them in code.

ACL What it allows
api-key:endpoint:* All API endpoints
api-key:model:* All models your team can use
api-key:endpoint:chat Chat and vision models only
api-key:endpoint:image Image generation only
api-key:model:grok-4.7 Only the Grok 4.7 model

A key for a chatbot should get chat plus one model. A key for an image tool should get image plus the Imagine model only. That way a leaked key cannot run up costs on everything.

Note: A new key can take a short moment to work across all xAI clusters. If your very first call fails with 401, wait a few seconds and try again.

Grok API Models and Prices (November 2026)

The flagship is Grok 4.7, released on September 21, 2026. It accepts text and images, supports function calling and structured outputs, and has reasoning effort levels of low, medium, high (default) and xhigh.

Model ID Context Input / 1M Cached / 1M Output / 1M
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 1M $1.25 $0.20 $2.50
grok-build-0.1 256K $1.00 $0.20 $2.00

Extra charges to know

  • Long prompts: at 200K tokens or more, the whole request bills at double rates. For Grok 4.7 that is $4 input and $12 output.
  • US endpoint: us.api.x.ai costs 10% more and is only for Grok 4.7 and 4.6.
  • Batch API: 20% off for the Grok 4.x models above, and batch calls skip rate limits.
  • Priority processing: 2x the standard rate when the response confirms the priority tier.
  • Tools: web search and code execution cost $5 per 1,000 calls on top of tokens.
  • Images: grok-imagine-image starts at $0.02 per image. Video starts at $0.02 per second on the lite model.

Grok 4.7 Fast is not on the public API. It only runs inside Cursor and Grok Build. For a full cost breakdown, see my Grok pricing guide, and for images and video see the Grok Imagine API guide.

Step 4: Make Your First API Call

xAI recommends its Responses API at /v1/responses. The older Chat Completions endpoint still works too, so most OpenAI-style tools connect without changes.

Test 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 what an API key is in one sentence."
  }'

A JSON reply with an output field means your key, credits and permissions all work. An error code means something is off, and the troubleshooting section below covers each one.

Python with the official xai-sdk

The official SDK is xai-sdk (version 1.21.0 at the time of writing). It needs Python 3.10 or newer.

pip install xai-sdk
import os
from xai_sdk import Client
from xai_sdk.chat import user

client = Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(model="grok-4.7")
chat.append(user("Give me three tips for writing clean Python."))

print(chat.sample().content)

Python with the OpenAI SDK

Already using OpenAI? Just change the base URL and the model name. Everything else stays the same.

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="Give me three tips for writing clean Python.",
)
print(response.output_text)

For streaming, tools, structured output and image input, follow my full Grok API Python tutorial. If you also need an OpenAI key for comparison, see how to get an OpenAI API key.

Note: Since November 7, 2026, xAI adds an invisible text watermark to supported Grok models by default. Only team admins can change it, per model. Read more in my Grok API watermark explainer.

Keep Your Key Safe

Anyone with your key can spend your credits. These habits keep that risk small.

  • Never hardcode it: load the key from XAI_API_KEY or a .env file, and add .env to .gitignore.
  • Keep it server side: do not ship the key in a mobile app, browser JavaScript or a public repo.
  • One key per app: separate keys make it easy to see usage and revoke one without breaking the rest.
  • Use tight ACLs: allow only the endpoints and models each app needs.
  • Cap usage: set per-key token limits and a monthly auto top-up maximum.
  • Rotate on any doubt: create a new key, update your app, then delete the old one.

xAI partners with GitHub Secret Scanning. If your key shows up in a public repo, xAI disables it and emails you. You can also disable a key yourself from the menu next to it on the API Keys page, or delete it for good.

On data: xAI says it never trains on API inputs or outputs without your permission. Requests are stored encrypted for 30 days for abuse checks, then deleted. Teams can turn on Zero Data Retention, but that disables features such as Files, Batch and the stateful Responses API.

Rate Limits and Usage Tiers

Each team has per-model limits on tokens per minute and requests per second. Your tier rises automatically with your total API spend since January 1, 2026, and it never drops back.

Tier Total spend needed
Tier 0 $0 (default)
Tier 1 $50
Tier 2 $250
Tier 3 $1,000
Tier 4 $5,000
Enterprise On request
  • Where to check: the Models page in the console shows your tier and your exact limits per model.
  • What counts: prompt, output, reasoning and cached tokens all count toward tokens per minute.
  • Need more: request a higher limit in the console, even without more spend.
  • Images and video: these limits are not tier based. Contact sales to raise them.

Troubleshooting: Common Problems and Fixes

Most xAI errors include a message that names the problem, so read it first. Here are the usual causes.

Error Likely cause Fix
401 Unauthorized Missing header, typo in key, or key disabled Send Authorization: Bearer plus your key. Make a new key if needed.
403 Forbidden The key or your team role lacks permission for that model or endpoint Edit the key ACLs, or ask your team admin for access.
404 Not Found Wrong URL or wrong model name Use https://api.x.ai/v1 and an exact ID like grok-4.7.
422 Unprocessable A field in the body has the wrong format Check the body against the API reference.
429 Too Many Requests Rate limit hit Retry with exponential backoff, or ask for a higher limit.

Quick checks that solve most issues

  1. Test the key itself. Call GET https://api.x.ai/v1/api-key with your key. It returns the key name, status, ACLs and team.
  2. Confirm the team. Keys and credits belong to one team. A key from team A cannot spend credits in team B.
  3. Check the balance. With the default $0 invoice limit, every call fails once credits run out.
  4. Check the env var. Run echo $XAI_API_KEY in the same terminal that runs your code.
  5. Check the status page. Visit status.x.ai if the error does not look like your fault.

Tip: Indian payment cards cannot be saved for later charges. xAI suggests buying prepaid credits through Guest Checkout instead.

Token counts on your bill may be a little higher than the console tokenizer shows. That is normal, because the API adds some fixed tokens to each request. Still stuck? Email support@x.ai with your request, the response and logs.

Frequently Asked Questions

Is the Grok API free?

No. As of November 2026, the xAI docs list no free tier or free signup credits, so you must buy prepaid credits before your first call. Approved startups can apply to the separate Grok for Startups program.

Does SuperGrok include API access?

No. SuperGrok and X Premium cover the Grok chatbot on grok.com, X and the apps. The API uses the same xAI login, but it is billed separately in the xAI Console.

How much does Grok 4.7 cost on the API?

Grok 4.7 costs $2.00 per 1M input tokens, $0.50 per 1M cached input tokens and $6.00 per 1M output tokens. Prompts of 200K tokens or more bill at $4 and $12.

Is the Grok API compatible with the OpenAI SDK?

Yes. Set base_url to https://api.x.ai/v1, use your xAI key, and pick a Grok model ID. Both the Responses and Chat Completions styles work.

What happened to the $150 data sharing credits?

The current xAI billing and pricing docs no longer mention a data sharing credit program. Check the Billing page of your own team, since any offer shown there applies only to that team.

How do I revoke a leaked Grok API key?

Open API Keys in the console, click the menu next to the key, and choose Disable key or Delete key. Then create a new key and update your apps.

Why does my new key return 401?

The header may be missing, the key may be mistyped, or the key may still be spreading across xAI clusters. Wait a few seconds, check the Bearer header, and confirm the key belongs to the right team.

Sources

Free weekly email

Get new AI guides every week

One short weekly email with our newest local AI, ComfyUI and AI tool guides. No spam, no pop-ups, just the good stuff.

Free. Unsubscribe anytime in one click. We only use your email to send this newsletter. See our privacy policy.

Surprised woman in a green hoodie on a neon city street at night holding a Grok card, hook Grok API key in 5 minutes