Why Is My API Key Not Working? 7 Common Fixes

2026-07-11·
TroubleshootingAPI KeysGuide

The Dreaded 401

You're mid-deploy and suddenly your app throws a 401 Unauthorized. The API key was fine yesterday. Here's how to diagnose and fix it — fast.

1. Key Is Actually Expired or Revoked

Symptoms: 401 on all endpoints, key was working previously.

Fix: Test the key directly:

  • Use Api Key Chain for an instant validation
  • Or curl the provider's models/user endpoint

If invalid, generate a new key from the provider dashboard and rotate immediately.

2. Wrong Key Type (Live vs Test)

Symptoms: Works in dev but not production, or vice versa.

Fix: Check your key prefix:

  • Stripe: sk_live_ ≠ sk_test_
  • OpenAI: Both start with sk-, but created in different projects
  • Most providers: Test keys only work in test/sandbox environments

3. Billing or Quota Exceeded

Symptoms: 429 (Rate Limit) or 402 (Payment Required).

Fix:

4. Key Copied Incorrectly

Symptoms: Immediate failure with invalid format errors.

Fix:

  • Make sure no leading/trailing whitespace
  • Copy the full key — some are 100+ characters
  • Watch for line breaks if copying from email or notes
  • Use Api Key Chain's auto-trim feature

5. Wrong API Endpoint or Header Format

Symptoms: 401 even though the key works elsewhere.

Fix: Each provider expects a different header:

  • OpenAI / Cohere / Mistral: Authorization: Bearer KEY
  • Anthropic: x-api-key: KEY
  • Gemini: ?key=KEY as query parameter
  • GitHub: Authorization: Bearer KEY + Accept header

Api Key Chain handles all formats automatically.

6. IP Restriction or Region Block

Symptoms: Works from one location but not another.

Fix: Check your provider dashboard for:

  • IP allowlists (Stripe, some enterprise tiers)
  • Region restrictions (some providers geo-block)

7. CORS or Network Issues

Symptoms: Works in curl/Postman but not in browser.

Fix: Browser-to-provider requests may be blocked by CORS. Use:

  • A server-side proxy (Vercel edge functions, Cloudflare Workers)
  • Api Key Chain's built-in CORS handling
  • A backend API route that proxies requests

Quick Diagnostic Checklist

CheckHow
Key valid?Test on apikeychain.app
Right environment?Compare prefix to provider docs
Billing OK?Check provider dashboard
Correct header?Match provider's API docs
CORS issue?Try from terminal vs browser
🛠️ Save time: Instead of debugging manually, paste your key into Api Key Chain. It auto-detects the provider, sends the correct request, and shows exactly what went wrong.

Related Posts