AI and model APIs

xAI (Grok) API keys: find, store, rotate, and use them safely with AI agents

xAI API keys for the Grok models are created on the API Keys page of the xAI Console (console.x.ai), belong to one team, and are sent as Authorization: Bearer <key> to https://api.x.ai. xAI's SDK and examples read the key from XAI_API_KEY. Each key carries access control lists (ACLs) for models and endpoints, and the Management API can rotate a key's secret with an overlap window of up to 7 days.

xAI (Grok) key types

TypePrefixSecret?ScopeExpiry
xAI API key
The prefix comes from the redacted form (xai-…) that the GET /v1/api-key endpoint returns. Per-key qps, qpm and tpm limits can be set.
xai-YesThe models and endpoints in the key's ACLs, within one team; a key created through the Management API has no access until ACLs are grantedOptional `expireTime`, set at creation and changeable later
Management key
Created in the xAI Console under Settings → Management Keys; requires Management Keys read and write permission.
—YesTeam administration through the Management API at `https://management-api.x.ai`; it does not call models—

How to create a xAI (Grok) key

  1. Sign up or sign in at console.x.ai and add credits to the team.
  2. Check that the console shows the right team, because xAI API keys are tied to one team.
  3. Open API Keys in the sidebar and click Create API Key.
  4. Name the key, choose the models and endpoints it may use, and set any rate limits.
  5. Copy the xAI key and store it in XAI_API_KEY or a secrets manager.

Open xAI (Grok)'s key settings

Limiting what the key can do

xAI keys are limited with ACLs. api-key:model:* and api-key:endpoint:* allow everything; narrower entries such as api-key:endpoint:chat, api-key:endpoint:image or api-key:model:<model name> restrict a key to particular endpoints and models. The Management API lists valid names at GET /auth/teams/{teamId}/endpoints and GET /auth/teams/{teamId}/models. Each key can also carry its own qps, qpm and tpm limits; when the token limit is reached, new requests are rejected while in-flight ones finish. Team admins see every key on the team, and members see only their own.

How to rotate a xAI (Grok) key

  1. Call POST /auth/api-keys/{apiKeyId}/rotate on https://management-api.x.ai with a management key to issue a new secret for the same key.
  2. Set oldSecretExpireTime to how long the old secret should keep working (default 24 hours, at most 7 days).
  3. Deploy the new secret to XAI_API_KEY everywhere before the old one expires.
  4. Without the Management API, create a new key in the console, deploy it, then disable or delete the old key.

Rotation keeps the key's ID, ACLs and limits and permanently invalidates the old secret once its overlap ends. The new secret inherits the old expiry unless expireTime is set. A new key can take a short time to reach every inference cluster; GET /auth/api-keys/{apiKeyId}/propagation reports per-cluster status.

How to revoke a xAI (Grok) key

  1. Open API Keys in the xAI Console for the correct team.
  2. Use the options menu beside the key and choose Disable key to stop it temporarily, or Delete key to remove it permanently.
  3. With the Management API, DELETE /auth/api-keys/{apiKeyId} deletes a key permanently and irrevocably.

If your xAI (Grok) key leaked

  1. Sign in to the xAI Console and switch to the team that owns the key.
  2. Disable or delete the exposed key under API Keys.
  3. Create a replacement key with only the ACLs it needs and update your applications.
  4. Check team usage for unexpected spend; xAI also disables keys it finds through GitHub secret scanning and emails the owner.

Environment variables and CLI

xAI (Grok)'s SDKs and docs read the key from XAI_API_KEY.

Things that catch people out

  • xAI API keys are tied to one team, so a key that works in one team's console view will not appear under another team.
  • A key created through the Management API has no access by default; it fails until ACLs are added.
  • Management keys and API keys use different base URLs (management-api.x.ai versus api.x.ai) and are not interchangeable.
  • xAI disables a key automatically when GitHub secret scanning finds it, so a sudden 401 can mean the key leaked.

Common xAI (Grok) authentication errors

401 Unauthorized

The request sent no Authorization header or an invalid xAI key, including a key that was disabled, deleted or blocked after a leak.

403 Forbidden

The key or user lacks permission for the request, for example a model or endpoint not in the key's ACLs.

429 Too Many Requests

Requests exceeded the key's or team's rate limit.

Using your xAI (Grok) key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a xAI (Grok) key, these intents fit:

IntentWhat BotPassword does
run_withRuns a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
http_requestMakes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

Where do I get an xAI API key for Grok?

Sign in to the xAI Console at console.x.ai, add credits, open API Keys and click Create API Key. Keys belong to the team you are viewing.

What is the xAI API key environment variable?

xAI's quickstart exports XAI_API_KEY, and the xai-sdk Python example passes os.getenv("XAI_API_KEY") to the client. The OpenAI SDK also works with the base URL https://api.x.ai/v1.

Can I restrict an xAI key to certain Grok models?

Yes. Give the key ACLs such as api-key:model:<model name> and api-key:endpoint:chat instead of the wildcard api-key:model:*.

How do I rotate an xAI API key without downtime?

Use the Management API's POST /auth/api-keys/{apiKeyId}/rotate. The old secret keeps working for 24 hours by default, or up to 7 days if you set oldSecretExpireTime.

How should a coding agent use an xAI key?

Give the agent its own xAI key limited by ACLs to the models it needs and with low qpm and tpm limits, so it cannot reach image endpoints or run up a large bill. Disable it in the console when the work is done.

Can an AI agent use my xAI (Grok) key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a .env file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with run_with, http_request, so the agent gets the result and never the value.

Stop pasting xAI (Grok) keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.