401 Unauthorized
API Key Revoked or Invalid – Fix It Fast
Your API key got revoked or flagged invalid. Here's the quick fix and why it happens—no fluff.
You're stuck with a revoked or invalid API key error.
It's frustrating, I get it. But the fix is usually straightforward. Let's skip the panic and get you back to work.
The Straight Fix
- Check the key's status. Go to your API provider's dashboard (like AWS IAM, Google Cloud Console, or a third-party service like Stripe). Look for the key in question—it'll often show as
Revoked,Disabled, orExpired. If it's revoked, you can't re-enable it. You'll need to generate a new one. - Generate a new key. Most platforms have a button for that. Hit it, copy the new key immediately (you won't see it again), and replace the old one in your application's configuration file or environment variables. Restart your app or service so it picks up the change.
- If you get a 401 but the dashboard shows the key as active, the issue is usually a mismatch. Double-check you copied the key correctly—no leading or trailing spaces. Also confirm you're using the right key for the right environment (production vs. sandbox keys are separate things).
Why This Happens
The culprit here is almost always one of three things:
- Security policy rotation: Your security team or automated tool forced a key rotation (common in orgs with compliance requirements like SOC2). The old key gets revoked automatically.
- Suspicious activity: The provider detected unusual traffic (like a spike from a new IP) and flagged the key as compromised. Some providers auto-revoke in that case.
- Accidental manual action: Someone on your team accidentally revoked the key in the dashboard. It happens more than you'd think.
Don't bother trying to decode the 401 error body—most providers just say Unauthorized with no detail. The dashboard is your friend here.
Less Common Variations
Sometimes the error isn't about the key itself but the context:
- Expired key with no renewal option: Some older API keys have a hard expiration. AWS IAM keys, for example, can be set with an expiration date. If yours expired, you can't renew it—you must create a new one.
- Permissions stripped: The key is still active, but the associated service account lost its roles or permissions. Check the IAM or service account settings. For instance, a Google Cloud API key might be valid but the service account it maps to no longer has access to the specific API.
- Key format mismatch: Some providers use different key types (e.g., a public key vs. a secret key). If you're using the wrong one in the wrong field, you'll get a 401. For AWS, you need both an access key and a secret key—mixing them up won't work.
- OAuth token vs. API key: If you switched from API key auth to OAuth, your old key will be ignored. Make sure your code is using the right authentication method.
Here's a quick table for common platforms:
| Provider | Key Type | Common Revocation Reason |
|---|---|---|
| AWS IAM | Access Key + Secret | Rotation policy, suspicious activity |
| Google Cloud | API Key | Expiration, service account removal |
| Stripe | Secret Key | Manual revoke, security flag |
| GitHub | Personal Access Token | User revoke, org policy |
Prevention for Next Time
You don't want to deal with this again. Here's what I'd do:
- Set up key rotation. Automate key regeneration every 90 days using a tool like HashiCorp Vault or AWS Secrets Manager. That way you don't get surprised by a policy enforcement.
- Use environment variables. Never hardcode keys in your codebase. Store them in
.envfiles or a secrets manager. If a key gets revoked, you just update one variable. - Monitor key usage. Most providers give you logs of key usage. Set up alerts for unusual activity (like a 401 spike) so you catch revocations early.
- Document who can revoke keys. If you're in a team, limit dashboard access to a few people. Accidental revocations drop when you have fewer hands on the controls.
That's it. Fix the key, check the context, and lock down your process. You'll spend less time on this and more on actual work.
Was this solution helpful?