Fix OpenAI 401 Invalid API Key
Fix OpenAI 401 Unauthorized and Invalid API Key Errors
The 401 Unauthorized error from the OpenAI API means your request failed authentication. This is almost always an API key issue. This guide covers every possible cause and walks you through the exact steps to diagnose and fix it.
Common Error Messages
The 401 error can appear with different messages depending on the root cause:
{
"error": {
"message": "Incorrect API key provided: sk-xxxx...xxxx. You can find your API key at https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"code": "invalid_api_key"
}
}
{
"error": {
"message": "You didn't provide an API key.",
"type": "invalid_request_error",
"code": null
}
}
Cause 1: Incorrect or Expired API Key
The most common cause is simply using the wrong key. Verify your key:
- Go to platform.openai.com → API Keys
- Check that the key listed matches what you are using (compare the last 4 characters)
- If the key has been revoked or does not appear, create a new one
- Copy the new key and update your environment
# Quick test with cURL
curl https://claude4u.com/v1/chat/completions \
-H "Authorization: Bearer sk-your-key-here" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o","messages":[{"role":"user","content":"test"}]}'
Cause 2: Missing Authorization Header
The API key must be sent in the Authorization header with the Bearer prefix:
# Wrong - missing Bearer prefix
curl -H "Authorization: sk-your-key" ...
# Wrong - using wrong header name
curl -H "api-key: sk-your-key" ...
# Correct
curl -H "Authorization: Bearer sk-your-key" ...
Cause 3: Environment Variable Not Loaded
Your code may reference an environment variable that is not set:
# Check if the variable is set
echo $OPENAI_API_KEY
# If empty, set it
export OPENAI_API_KEY="sk-your-key-here"
export OPENAI_BASE_URL="https://claude4u.com/v1"
# Python - verify the key is loaded
import os
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable is not set")
print(f"Key loaded: sk-...{api_key[-4:]}")
.bashrc, .zshrc, or .env file for persistence.
Cause 4: Extra Whitespace or Newlines
Invisible characters in your API key will cause authentication to fail:
# Python - strip whitespace
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY", "").strip(),
base_url="https://claude4u.com/v1"
)
// Node.js - strip whitespace
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY?.trim(),
baseURL: 'https://claude4u.com/v1'
});
Cause 5: Wrong Organization or Project
If your account belongs to multiple organizations, you may need to specify which one:
from openai import OpenAI
client = OpenAI(
api_key="sk-your-key",
organization="org-your-org-id",
base_url="https://claude4u.com/v1"
)
Cause 6: Using a Revoked Key
If someone on your team revoked the key, or if it was automatically revoked due to a detected leak (e.g., committed to a public GitHub repo), it will return 401. Check your email for notifications from OpenAI about revoked keys.
Cause 7: Using a Key from the Wrong Service
Make sure you are not accidentally using an API key from a different service:
- OpenAI keys start with
sk-orsk-proj- - Anthropic (Claude) keys start with
sk-ant- - Google (Gemini) keys have a different format entirely
Debugging Checklist
- Verify the key exists in your OpenAI dashboard
- Test the key directly with cURL
- Check for whitespace, newlines, or encoding issues
- Confirm the environment variable is loaded in your runtime
- Verify you are using the correct base URL
- Check if the key has been revoked
- Ensure the correct organization ID is set (multi-org accounts)
Still Getting 401?
If none of the above fixes work:
- Create a brand new API key and test with a minimal script
- Check OpenAI's status page for ongoing authentication issues
- Contact OpenAI support if your account may be suspended
- Consider using claude4u.com as an intermediary to simplify authentication management
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI