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

Source: https://botpassword.com/keys/xai
Last checked: 2026-11-08

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| xAI API key | `xai-` | Yes | The models and endpoints in the key's ACLs, within one team; a key created through the Management API has no access until ACLs are granted | Optional `expireTime`, set at creation and changeable later |
| Management key | — | Yes | Team 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.

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

## 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 the 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

- `XAI_API_KEY`

## Gotchas

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

## Using this key with AI agents (BotPassword)

- `run_with`: Runs 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_request`: Makes 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.

## FAQ

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

## Sources

- https://docs.x.ai/developers/quickstart
- https://docs.x.ai/developers/api-reference
- https://docs.x.ai/docs/key-information/using-management-api
- https://docs.x.ai/developers/rest-api-reference/management/auth
- https://docs.x.ai/developers/rest-api-reference/inference/other
- https://docs.x.ai/console/faq/security
- https://docs.x.ai/developers/debugging
