A Grok API key is the secret credential that authenticates your code against the Grok API ([https://api.x.ai/v1](https://api.x.ai/v1)), letting applications call Grok models—such as grok-4.7 for general reasoning and coding, alongside image, video, and voice endpoints. Keys begin with xai-, attach to an xAI team rather than an individual user, and draw from prepaid team credits or invoiced billing.
How Grok API Keys Work Under the Hood
When you generate a key in the xAI Console (console.x.ai), you are issuing a bearer credential tied to a specific team workspace. Because usage, rate limits, and billing live at the team level, two details catch developers off guard:
- Teams own the keys and balance: If a teammate leaves, their key stays with the team. Deleting a team permanently destroys all of its API keys and forfeits remaining prepaid credits.
- Separate billing from consumer plans: Signing in with the same account you use for Grok or X does not share API billing. Consumer subscriptions do not automatically fund API requests; API calls require a positive credit balance or active billing on the team.
- One-time secret reveal: The full
xai-string is displayed only once when created. Afterward, the console masks it to the prefix and last four characters, along with metadata like spend, owner, and last use.
Getting and Testing Your Key
Setting up a key takes only a few minutes if you fund the team first and store the secret cleanly:
- Sign in and pick the right team: Open
console.x.ai, sign in, and confirm which team should own the integration and its billing. - Add prepaid credits: Open Billing in the console and add credits (starting from $5) so requests are not rejected at a zero balance.
- Create a scoped key: Go to API Keys, name the key after its workload and environment (for example,
support-triage-prod), restrict endpoints or models if needed, and copy thexai-secret immediately. - Inject via environment variable: Export it locally or load it from a secrets manager as
XAI_API_KEY—never hardcode it in source files or commit.envfiles to Git.
export XAI_API_KEY="xai-your-secret-key-here"
curl https://api.x.ai/v1/responses
-H "Authorization: Bearer $XAI_API_KEY"
-H "Content-Type: application/json"
-d '{
"model": "grok-4.7",
"input": "Reply in one sentence: why should API keys be rotated periodically?"
}'
Because the API is OpenAI-compatible at [https://api.x.ai/v1](https://api.x.ai/v1), you can also point existing OpenAI SDK clients at that base URL with XAI_API_KEY instead of rewriting your request layer.
Costs, Tiers, and Key Hygiene
Understanding how a key behaves in production prevents surprise 401 errors, throttled traffic, or runaway token bills:
| Area | What to Know | Practical Takeaway |
|---|---|---|
| Pricing & Context | grok-4.7 lists at $2.00 / 1M input tokens and $6.00 / 1M output tokens under 200k tokens (with a 500k context window), while rates double at 200k tokens or more. |
Watch long-context prompts and verbose outputs—output tokens cost up to triple input tokens. |
| Rate-Limit Tiers | Teams have per-model requests-per-second (RPS) and tokens-per-minute (TPM) limits that rise across spend tiers (from Tier 0 at $0 up through Tier 4 at $5,000+). | Expect lower throughput on a brand-new team until cumulative spend climbs. |
Auth Errors (401) |
Usually caused by a missing Authorization: Bearer header, accidental leading/trailing whitespace, or a deleted/disabled key. |
Verify the key status in the console and inspect how your deployment injects XAI_API_KEY. |
| Key Rotation | Create a replacement key first, update your secret store and redeploy, verify traffic, then disable and delete the old key. | Issue separate keys per app and environment so revoking a leaked staging key never takes down production. |
A Grok API key is simple to mint, yet its real power—and risk—comes from how you organize it: team ownership governs who pays, scoping and secret stores govern what gets exposed, and disciplined rotation keeps your integrations resilient as your product scales.
Leave a Reply