Grok API 401 Invalid API Key Error: Fix
Every reason api.x.ai rejects your key, and how to confirm which one applies.
Updated Aug 13, 2026
A 401 from the Grok API means the key was not accepted — the request never reached a model.
Check the header format
The API expects Authorization: Bearer xai-.... An api-key header or a missing Bearer prefix both return 401.
Check the base URL
Requests must go to https://api.x.ai/v1. Reusing an OpenAI-compatible SDK without changing the base URL is the most common cause of a sudden 401 on working code.
Check the key itself
- Regenerate it in the xAI console if a teammate may have revoked it.
- Strip trailing newlines when reading from
.env. - Confirm the key belongs to a team with credit remaining — an exhausted team can present as an auth failure in some SDKs.
Condensed checklist: Grok API returns 401.
401 vs 429
If the key works intermittently, it is not an auth problem — read Grok API 429 rate limits.