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:

  1. Go to platform.openai.com → API Keys
  2. Check that the key listed matches what you are using (compare the last 4 characters)
  3. If the key has been revoked or does not appear, create a new one
  4. 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" ...
Tip: When using the official OpenAI SDK, the Authorization header is set automatically. This error is most common when making raw HTTP requests with fetch, axios, or cURL.

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:]}")
Warning: Environment variables set in a terminal session do not persist after closing the terminal. Add them to your .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:

Debugging Checklist

  1. Verify the key exists in your OpenAI dashboard
  2. Test the key directly with cURL
  3. Check for whitespace, newlines, or encoding issues
  4. Confirm the environment variable is loaded in your runtime
  5. Verify you are using the correct base URL
  6. Check if the key has been revoked
  7. Ensure the correct organization ID is set (multi-org accounts)
Tip: claude4u.com provides its own API keys that are separate from your OpenAI credentials. This means you never expose your actual OpenAI key to client applications, reducing the risk of key leaks. If a claude4u.com key is compromised, you can rotate it without affecting your upstream OpenAI account.

Still Getting 401?

If none of the above fixes work:

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free