Grok API returns 401 Unauthorized
Updated Aug 13, 2026
Quick fix
- Confirm the header is
Authorization: Bearer <your-key>— notapi-key. - Regenerate the key in the xAI console; keys revoked by a team member fail silently.
- Make sure the key belongs to a team with credits available.
- Strip whitespace and newlines from the key when loading from an env file.
- Point requests at
https://api.x.ai/v1, not an OpenAI base URL.