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

Grok API: Get a Key and Make Your First Request

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.

SpaceXSI’s public quickstart with API key setup and first request examples
Grok API quickstart

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.

  1. 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.

  2. 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.

  3. 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.

Bash
read -rsp 'Grok API key: ' XAI_API_KEY
printf '\n'
export XAI_API_KEY

The 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.

Bash
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.

A library hatch reads a key while a separate drawer takes the model and input and returns the output
The key identifies the caller. The model and input describe the work.

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.

JobWhere to startOther listings
Text or codegrok-4.7grok-4.6, grok-4.5, grok-4.3, grok-4.20-0309-reasoning, grok-4.20-0309-non-reasoning, grok-4.20-multi-agent-0309
Grok image APIgrok-imagine-image-2.0grok-imagine-image
Grok video APIgrok-imagine-video-1.5grok-imagine-video-1.5-lite, grok-imagine-video
Grok Voice APIgrok-voice-3.0 for speech-to-speechSpeech-to-text and text-to-speech are separate modes. The catalogue marks grok-voice-2.0 deprecated.

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.

Envelopes jam one intake slot while spaced envelopes approach another beneath a shared token gauge
Spacing calls reduces bursts. The amount of text still counts toward the minute's allowance.

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
Related Articles
Claude Code Skills: Where They Live and How to Add One

Claude Code Skills: Where They Live and How to Add One

Add a Claude Code skill from a copyable SKILL.md example. See which copy wins, how slash commands work, and when a team can block inline shell commands.Nov 26, 2026Build
Open WebUI: What It Is, What It Costs, and How to Start

Open WebUI: What It Is, What It Costs, and How to Start

See where team chats and documents go, how the Open WebUI License works, and how to start the interface with Docker and connect a model.Nov 26, 2026Build
Gemini CLI: Who Can Use It and How to Start

Gemini CLI: Who Can Use It and How to Start

Check which accounts still work with Gemini CLI, which models Google supports, and how to install the terminal agent or move a project to Antigravity CLI.Nov 26, 2026Build
Open Source AI Platforms in 2026: Ollama, LibreChat, Dify and 7 More

Open Source AI Platforms in 2026: Ollama, LibreChat, Dify and 7 More

Compare Ollama, LibreChat, Dify and seven more by licence, hosting effort and model costs. Choose the right layer for your company’s AI stack.Nov 26, 2026Build
Claude PR Steward

Claude PR Steward

See what Claude PR Steward does to one pull request, how to start it manually, where to cap spend, and which GitHub rules protect your merge.Nov 25, 2026Build
LibreChat: What It Is, What It Costs, and How to Start

LibreChat: What It Is, What It Costs, and How to Start

See who maintains LibreChat, which models it connects to, where conversations go, and how to get from a local Docker install to your first reply.Nov 25, 2026Build
Google AI Studio Text to Speech: Make a Voice-Over and a Dialogue

Google AI Studio Text to Speech: Make a Voice-Over and a Dialogue

Make a one-minute narration and a two-speaker dialogue in the Speech Playground. Choose voices, direct the delivery and download the audio or save a WAV.Nov 25, 2026Build
OpenAI API Key for Your First App: ChatGPT Credits and Setup

OpenAI API Key for Your First App: ChatGPT Credits and Setup

Create a project key, check whether your ChatGPT plan includes API credits, and send a first Python request. Find the right fix for 401 and 429 errors.Nov 25, 2026Build
Newsletter

One letter, every Sunday.Working systems, not hot takes.

Weekly. No spam. Unsubscribe anytime.