Fix: 401 Unauthorized / Invalid API Key
Step-by-step fix for '401 Unauthorized' or 'Invalid API Key' errors when using OpenClaw with OpenAI or Anthropic.
This page helps you fix API authentication errors when OpenClaw tries to connect to AI providers.
🔴 Raw Error Log (What You Actually See)
You may see one of these errors:
Error: Request failed with status code 401Or:
Error: Invalid authentication
Error: Incorrect API key provided
Error: Invalid API keyOr OpenClaw seems to work but:
- Bot replies with empty messages
- Bot says "I cannot help with that"
If your situation matches, you are in the right place.
🔍 Why This Happens (Root Cause)
The API key in your .env file is:
- Missing entirely
- Copied incorrectly (extra spaces, quotes, or line breaks)
- Expired or revoked
- Wrong key for the service (e.g., using OpenAI key for Anthropic)
✅ How to Fix It (Step-by-Step)
Step 1 — Check for Invisible Spaces (Most Common!)
Open your .env file and look carefully:
# ❌ WRONG - has trailing space
OPENAI_API_KEY=sk-1234567890abcdef
# ❌ WRONG - has quotes
OPENAI_API_KEY="sk-1234567890abcdef"
# ❌ WRONG - has line break in middle
OPENAI_API_KEY=sk-1234567890
abcdef
# ✅ CORRECT - clean, no extras
OPENAI_API_KEY=sk-1234567890abcdefStep 2 — Verify Key is Active
For OpenAI:
- Go to OpenAI API Keys
- Check if your key is listed and not revoked
- Check Billing — you need credits!
For Anthropic:
- Go to Anthropic Console
- Verify your API key is active
- Ensure you have credits loaded ($5 minimum)
Step 3 — Generate a New Key (If Needed)
If your key might be compromised or isn't working:
- Delete the old key from the provider's dashboard
- Create a new key
- Update your
.envfile with the new key
Step 4 — Restart OpenClaw
⚠️ Critical: OpenClaw only reads
.envwhen it starts!
# Stop the bot (Ctrl+C)
# Then restart
npm run start🔎 Verify the Fix
After applying the fix, run:
npm run startThen try sending a message to the bot.
✅ Expected Result
The bot responds with an intelligent reply instead of:
- Empty messages
- "I cannot help with that"
- Error messages
❌ Still Not Working?
- If you see
429errors: Check Fix: 429 Too Many Requests - If you see timeout errors: Your network may be blocking API access
🧭 Advanced Notes
Using Multiple API Keys?
If your .env has keys for multiple services:
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...Make sure both keys are valid if OpenClaw uses both services.
🔗 Related: Common Errors Summary | Error Dictionary
Internal Ref: ERROR_API_001