OpenAI API Key Invalid or Revoked — Fix It Fast
The 'invalid API key' error occurs when OpenAI's API cannot authenticate your request due to a missing, revoked, expired, or incorrectly formatted key. This error is commonly seen by developers integrating the OpenAI API into applications or scripts. Resolving it typically takes just a few minutes by verifying or regenerating your credentials.
Why does this error happen?
How to fix it
Generate a New API Key
Log in to platform.openai.com and navigate to the API Keys section under your account settings. Click 'Create new secret key', give it a recognizable name, and copy it immediately — OpenAI will not display the full key again after this step.
Remove Leading and Trailing Spaces
When pasting an API key into a .env file or configuration panel, invisible whitespace characters can be accidentally included. Open your environment file and ensure the key value starts directly with 'sk-' and has no spaces or newline characters before or after it.
Verify Your Organization ID
If your account belongs to multiple OpenAI organizations, requests must include the correct Organization ID in the 'OpenAI-Organization' header. Find your Organization ID at platform.openai.com/account/org-settings and confirm it matches what is set in your application configuration.
Confirm Active Billing on Your Account
Navigate to platform.openai.com/account/billing and confirm that a valid payment method is on file and your usage limits have not been exceeded. Accounts with failed payments or exhausted free-tier credits may have API access suspended, which invalidates otherwise correct keys.
💡 Pro Tip
Store your API key exclusively in environment variables or a secrets manager such as AWS Secrets Manager or HashiCorp Vault — never commit it to source control. Rotate your keys every 90 days and set up usage alerts in the OpenAI dashboard to catch unauthorized use before it leads to revocation.
Frequently Asked Questions
Why does my API key work in one project but fail in another?
Can an OpenAI API key expire automatically?
How do I know if my API key has been compromised?
Does the free trial API key work the same as a paid key?
Quick diagnostic checklist
Before diving into the full fix, run through these quick checks — they resolve the issue in most cases without additional steps:
Common root causes
Understanding why this error occurs helps you prevent it in the future. The most frequent causes are:
- OpenAI server outages or degraded performance
- Browser extensions blocking scripts or cookies
- Expired or corrupted session tokens
- Account-level restrictions or subscription issues
- Rate limits on free or Plus plans
Still not working?
If none of the steps above resolved the issue, the next step is to contact ChatGPT support directly. When reaching out, include:
- • The exact error message or code you see
- • The steps you already tried from this guide
- • Your account plan and the approximate time the error started
- • Your browser/OS version if it is a web interface issue
About ChatGPT
ChatGPT is an AI assistant developed by OpenAI, launched in November 2022. It runs on GPT-4o and earlier GPT models and is used by over 180 million people worldwide for writing, coding, research, and conversation. ChatGPT is available as a free tier and a Plus subscription ($20/month) with higher limits and priority access.
Browse all ChatGPT error guides →