Grok API: Get a Key and Make Your First Request
Get a Grok API key, send the official first request, choose a model, and see how billing and rate limits work when you call Grok from code.
Published

To call the Grok API from code, create a key in the SpaceXSI developer console at console.x.ai and fund the team that owns it. You can then send a coding question to Grok 4.7 from your terminal. If you only want to see how Grok answers, the console includes a free Playground.
The Grok API gives your software a way to send work to Grok and read its reply. On November 26, 2026, the developer documentation names the maker SpaceX Super Intelligence. Its API product page uses SpaceXSI. The console and API still use addresses ending in x.ai. Resellers sell access to Grok at their own prices. This guide covers the maker's own API.
How to create a Grok API key
Create the key in the team that will pay for the calls. Think of it as a library card: it identifies whose account you're using. Your prompt belongs in a separate part of the request.
Choose your team
Sign in or create a free console account. If you belong to multiple teams, select the one that owns this project before changing billing. For a first self-serve call, add prepaid credits under Billing. Purchases affect everyone who uses that team's balance. See the billing guide.
Generate the key
Open API Keys, create a key and copy its value. Keep it on the machine or server sending the request. Putting it in a web page would give visitors access to the credential your app uses.
Load the key in your terminal
In Bash, run the commands below. At the prompt, paste the key and press Enter. Bash hides what you paste and makes the value available to commands in that shell.
read -rsp 'Grok API key: ' XAI_API_KEY
printf '\n'
export XAI_API_KEYThe first request in the Grok API documentation
Start with the cURL example from the official quickstart. It asks Grok to repair a small JavaScript function. The task has a useful property for a first call: you can check the answer yourself.
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.7",
"input": "Fix this function and explain the bug: function median(a){a.sort();return a[a.length/2]}"
}'The address ending in /responses tells cURL where to send the request. The authorization header carries your key. The JSON body names the model and gives it the function to fix. When you later replace the sample with your own question, change input and leave the address and header alone.

How to check the reply: Look for Grok's answer in the returned response. Give the proposed function the array [2, 10, 3]. Its median should be 3. The original function sorts numbers as strings and tries to read a fractional array index for an odd-length list. A repair that only changes the sort still fails this example. For an even-length list, check that the function averages the two middle values.
Can an OpenAI client call Grok?
Yes. If your Python app already uses the OpenAI client library, configure its base_url as https://api.x.ai/v1 and pass the Grok key as api_key. The quickstart calls client.responses.create with model and input, then reads response.output_text. The library can stay in your app when you change providers. Keep each provider's credential in its own variable. The OpenAI key and billing walkthrough covers the OpenAI side.
Check any optional fields your application relies on. For example, Grok 4.20 and newer silently ignore logprobs and top_logprobs, settings some apps use to inspect how likely individual tokens were. A successful request can still leave a scoring feature without the data it needs.
Which Grok API models should you use?
Use grok-4.7 for the coding example. The maker recommends it for code and chat and directs audio, image and video jobs to their own APIs. These are the identifiers in the published model catalogue as of November 26, 2026.
For a product mockup, start with the Grok Imagine workflow. For a teammate that operates a cloud computer, see how Grok Bot works. If you're waiting on the next model, keep the Grok 5 release information separate from the IDs you put in code.
Does the model see live events? The first request above supplies everything Grok needs to inspect the function. If you change the prompt to ask what a company announced this morning, you also need to enable a search tool, such as Web Search or X Search. The model guide explains how Grok gets current information.
What happens when requests arrive together?
Grok limits both how often your team can call a text model and how much text it can process in a minute. For Grok 4.7, the published Tier 0 limits are 150 requests per second and 50 million tokens per minute. Tokens are the chunks of text a model processes. Check Models in your team's console for its current limits, which depend on the model and the team's cumulative spend. Per-model rate limits
A scheduled job can send too many requests in the same second even if each asks for one short answer. Space the calls out. A document-processing job can hit the other limit with fewer, longer requests. Count the prompt and output, including cached prompt tokens and reasoning tokens where applicable. If the API returns 429 Too Many Requests, wait progressively longer between retries and cap the attempts.

Questions before your first call
Grok API free credits
The public offer includes a free console Playground. The billing guide also lets you redeem a promo code if you have one. Neither the quickstart nor the API offer promises every new account a credit balance. If you're looking for the consumer app, Grok's free chat options cover that separately.
How do I generate my own API key?
Sign in to the SpaceXSI console, select your team, open API Keys and create a key. Copy it into the environment where your code runs.
Can I get the API for free?
You can try prompts in the Playground before adding billing. For a request from your own code, the self-serve quickstart tells you to fund your team with credits.
How much does Grok API cost?
For Grok 4.7 prompts under 200,000 tokens, the rate is $2 per million uncached input tokens and $6 per million output tokens. Official API pricing
If your goal is choosing an assistant to use yourself, compare ChatGPT, Claude, Gemini and Grok for the work you do.
- Published
- Category
- Build








