Fix Grok API Error 401 Unauthorized
Updated 10/12/2026
An HTTP 401 Unauthorized error from the xAI API indicates that your application failed to authenticate with the Grok servers. This error stops your code from executing and usually points to an invalid API key, an incorrectly formatted request header, or a configuration issue in your local environment variables.
Use these step-by-step troubleshooting procedures to isolate the cause and restore your Grok API access.
1. Verify the Format of Your Authorization Header
The most common cause of a 401 Unauthorized error is an incorrectly formatted HTTP header. The xAI API requires a Bearer token format. If you are building raw HTTP requests rather than using an official SDK, check your request headers.
* Correct Header Format: Authorization: Bearer xai-xxxxxxxxxxxxxxxxxxxx * Common Mistakes: * Omitting the word Bearer before the key. * Adding an extra space or a colon between Bearer and the key (e.g., Bearer: xai-...). * Failing to include the xai- prefix that prepends all xAI API keys.
Verify your header structure by outputting the raw headers to your console (excluding the actual key secret) before sending the request.
2. Check for Missing or Empty Environment Variables
If your code loads the API key from a .env file or environment variables, the system may be passing an empty or undefined string to the request client.
1. Print the loaded variable: Add a temporary debug line to your code to print the length or the first few characters of the loaded key (do not log the entire key in production logs): `python # Python example import os key = os.getenv("XAI_API_KEY") print(f"Key loaded: {key[:5]}... Length: {len(key) if key else 0}") ` 2. Confirm the dotenv configuration: If using Node.js or Python, ensure you call dotenv.config() or load_dotenv() *before* trying to access the environment variable. 3. Check spelling: Ensure the variable name matches exactly in both your code and your host environment (e.g., XAI_API_KEY vs GROK_API_KEY).
3. Isolate the Issue with a Direct cURL Request
Rule out any issues with your software stack, SDK, or framework wrapper by executing a direct cURL call from your command-line interface. This establishes whether the issue lies with the API key itself or with your code's implementation.
Run the following command in your terminal (replace your_api_key_here with your actual xAI API key):
`bash curl https://api.x.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your_api_key_here" \ -d '{ "model": "grok-2-1212", "messages": [ {"role": "user", "content": "Ping"} ] }' `
- If this command returns a 200 OK: Your API key is valid. The 401 error is caused by how your SDK or code is initializing, loading, or passing the key.
- If this command still returns a 401 Unauthorized: Your API key is invalid, inactive, or restricted.
4. Check Key Status in the xAI Console
An API key can stop working if it was deleted, disabled, or if the underlying account is restricted.
- Log in to the xAI Console (console.x.ai).
- Navigate to the API Keys section.
- Verify that the key you are using matches one listed in the dashboard and that its status is marked as Active.
- If the key has been compromised or is behaving unexpectedly, click Create API Key to generate a new token. Replace the old token in your configuration and test again.
5. Verify Your Billing and Credit Balance
In some instances, the xAI API returns authorization-related errors if your account has an inactive billing profile or has run out of credits.
- In the xAI Console, go to the Billing or Usage tab.
- Check if your account has prepaid credits remaining. If your credit balance is $0.00, the API may reject your credentials.
- Ensure your linked credit card is valid and has not expired. If a automatic recharge failed, your keys may be temporarily restricted.
When to Escalate
If your direct cURL requests fail with a 401 error using a brand-new API key, and your xAI Console billing dashboard shows a positive credit balance, there may be an issue with your account status or the xAI authentication servers.
Collect your cURL request output (redact the API key), the timestamp of the failed requests, and your account ID, then contact xAI Support through the console help portal.
Related Grok help
Set up your AI once so you repeat yourself less.
Try AI Setup ProfileTickd is independent and not affiliated with any AI company.