API errors
How to Fix OpenAI API 401 Unauthorized Error
Updated 10/14/2026
The OpenAI API 401 Unauthorized error indicates that your request failed authentication. This typically means the API key you are using is missing, invalid, deleted, or misconfigured in your codebase.
Follow these step-by-step diagnostic and fix procedures to resolve the error and restore your application's connection to OpenAI.
1. Verify Your API Key Validity The most common cause of a 401 error is an invalid or deleted API key. 1. Log in to your [OpenAI Platform Dashboard](https://platform.openai.com/). 2. Navigate to **Dashboard** > **API Keys**. 3. Check the status of the key you are using. If it has been deleted or revoked, you must generate a new key. 4. Click **Create new secret key**, copy it immediately, and replace your old key in your project configuration. Note: OpenAI will never show the full key again once you close the modal.
2. Check for Whitespace or Formatting Issues When copying and pasting API keys, it is easy to accidentally include extra characters. 1. Ensure your API key starts with the standard prefix (e.g., `sk-proj-` for newer project keys or `sk-` for older legacy keys). 2. Check your configuration file or environment variables for accidental leading or trailing spaces, quotation marks (`"` or `'`), or semicolons. 3. If you are loading the key from a `.env` file, ensure you are not wrapping the key in quotes unless your library specifically requires it. Example of correct syntax: ```env OPENAI_API_KEY=sk-proj-yourActualKeyHereWithoutQuotes ```
3. Verify Environment Variable Loading If your code relies on the SDK automatically detecting the `OPENAI_API_KEY` environment variable, the 401 error often occurs because the program cannot find the variable and sends an empty string. * **For Linux/macOS:** Run `echo $OPENAI_API_KEY` in your terminal to verify it is set. If empty, export it: ```bash export OPENAI_API_KEY="your-api-key" ``` To make this permanent, add it to your `~/.bashrc`, `~/.zshrc`, or `~/.bash_profile` file. * **For Windows (PowerShell):** Run `$env:OPENAI_API_KEY` to verify. Set it temporarily with: ```powershell $env:OPENAI_API_KEY="your-api-key" ``` * **In Node.js (dotenv):** If using `dotenv`, ensure you call `require('dotenv').config()` at the absolute top of your entry file (e.g., `index.js`), before importing or initializing the OpenAI client. * **In Python:** Ensure you have `python-dotenv` installed and load it: ```python from dotenv import load_dotenv import os load_dotenv() ```
4. Explicitly Pass the API Key in the Client If environment variables continue to fail, bypass automatic detection by explicitly passing the API key into the SDK initializer. This helps isolate whether the issue is with your system configuration or your environment setup. * **Python SDK (v1.0.0+):** ```python from openai import OpenAI client = OpenAI( api_key="sk-proj-YOUR_ACTUAL_KEY" ) ``` * **Node.js SDK (v4.0.0+):** ```javascript import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: 'sk-proj-YOUR_ACTUAL_KEY', }); ```
5. Validate Organization and Project Configuration If your OpenAI account belongs to multiple organizations or projects, and you restrict access via specific headers, an incorrect Organization or Project ID will trigger a 401 error. 1. Go to your OpenAI platform settings and retrieve your **Organization ID** (starts with `org-`) and **Project ID** (starts with `proj-`). 2. Pass these explicitly into your client initialization to ensure you are targeting the correct billing account: ```python client = OpenAI( api_key="sk-proj-...", organization="org-...", project="proj-..." ) ```
6. Test with a Minimal curl Command To rule out library-specific bugs or SDK wrapper issues, execute a direct raw HTTP request from your terminal: ```bash curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY" ``` * If this command succeeds (returns a list of models), your API key is valid. The issue lies entirely in how your application code loads or handles the key. * If this command returns a `401 Unauthorized` JSON payload, the API key itself is inactive, invalid, or has been revoked by OpenAI.
When to Escalate * **Suspended Account:** Check the [OpenAI Usage Dashboard](https://platform.openai.com/usage). If you see a banner stating your account is suspended or blocked, your API keys are automatically disabled, resulting in 401 errors. You must resolve outstanding billing issues or contact OpenAI support directly. * **Enterprise SSO / Access Policies:** If you are on an enterprise plan, your workspace administrator may have enforced IP restrictions or single sign-on constraints that block your local development environment from authenticating. Contact your workspace administrator to check access policies.
Related OpenAI help
Set up your AI once so you repeat yourself less.
Try AI Setup ProfileTickd is independent and not affiliated with any AI company.