Fix OpenAI API key environment variable not working
Updated 10/9/2026
When integrating OpenAI’s API into your applications, using an environment variable is the recommended way to secure your API key. However, developer setups frequently run into errors where the SDK fails to recognize the variable, resulting in common errors like No API key provided or initialization crashes.
This guide explains why your system or SDK is failing to read your OPENAI_API_KEY variable and provides practical, step-by-step solutions to fix it across different operating systems and programming languages.
1. Verify the Variable Name and Formatting
The OpenAI SDKs (both Python and Node.js) are hardcoded to look for a specific, case-sensitive environment variable name. If there is a typo, the SDK will fail to initialize.
- Exact Name Required: The variable must be exactly OPENAI_API_KEY. It cannot be Openai_Api_Key or OPENAI_KEY.
- No Spaces or Quotes: Avoid putting spaces around the equals sign when setting the variable. In some systems, wrapping the key in quotes can cause the key to be read with the quotes included, causing authentication to fail.
2. Set the Variable Correctly for Your Operating System
Environment variables set in a terminal session are usually temporary and disappear when you close the terminal window. To fix this, set them correctly for your OS or write them to your shell profile.
On macOS and Linux (Terminal) To set it temporarily for your current terminal session: ```bash export OPENAI_API_KEY="your-actual-api-key-here" ``` To make this change permanent, add it to your shell configuration file (e.g., `~/.bashrc`, `~/.zshrc`, or `~/.bash_profile`): 1. Open the file in a text editor: `nano ~/.zshrc` 2. Add the export line to the bottom of the file: `export OPENAI_API_KEY="your-actual-api-key-here"` 3. Save and exit (Ctrl+O, Enter, Ctrl+X in nano). 4. Apply the changes: `source ~/.zshrc`
On Windows (Command Prompt) To set it temporarily in CMD: ```cmd set OPENAI_API_KEY=your-actual-api-key-here ``` To make it permanent in Windows: 1. Open the Start Menu, search for "Edit the system environment variables", and click it. 2. Click the **Environment Variables...** button. 3. Under "User variables for [YourUsername]", click **New...**. 4. Set **Variable name** to `OPENAI_API_KEY` and **Variable value** to your actual key. 5. Click **OK** to save and close all windows.
On Windows (PowerShell) To set it temporarily in PowerShell: ```powershell $env:OPENAI_API_KEY="your-actual-api-key-here" ```
3. Configure Local Environment Files (.env)
If you are using a local development configuration file like .env, the SDK will not automatically read it unless you use a loader package.
For Python Projects 1. Ensure you have the `python-dotenv` package installed: ```bash pip install python-dotenv ``` 2. Create a `.env` file in your root project directory (where your main script runs): ```env OPENAI_API_KEY=your-actual-api-key-here ``` 3. Load the variables explicitly at the very top of your Python script before initializing the client: ```python import os from dotenv import load_dotenv from openai import OpenAI
load_dotenv() # This loads the variables from .env
client = OpenAI() # Automatically picks up the loaded env var `
For Node.js Projects 1. Install the `dotenv` package: ```bash npm install dotenv ``` 2. Create a `.env` file in your project root: ```env OPENAI_API_KEY=your-actual-api-key-here ``` 3. Load the package at the absolute entry point of your application: ```javascript require('dotenv').config(); const { OpenAI } = require('openai');
const openai = new OpenAI(); // Automatically reads process.env.OPENAI_API_KEY `
4. Restart Your IDE or Terminal
This is one of the most common oversights. IDEs like VS Code, PyCharm, or WebStorm do not automatically detect system environment variables added *after* the IDE was opened.
- Completely close your IDE.
- Close any running terminal or command prompt windows.
- Reopen your terminal or IDE.
- Run a quick check in your terminal to ensure the system sees it:
- macOS/Linux: echo $OPENAI_API_KEY
- Windows CMD: echo %OPENAI_API_KEY%
- Windows PowerShell: echo $env:OPENAI_API_KEY
If the command prints your key, your applications can now read it.
5. Explicitly Pass the Key (Troubleshooting Check)
If you still get errors, test if the SDK itself is functioning by passing the key directly into the client constructor. This helps isolate whether the issue is with the variable configuration or the SDK code.
* Python Test: `python client = OpenAI(api_key="your-actual-api-key-here") ` * Node.js Test: `javascript const openai = new OpenAI({ apiKey: 'your-actual-api-key-here' }); `
*Note: Do not commit files containing hardcoded keys to public repositories like GitHub. Use this step only for troubleshooting.*
When to Escalate
If the SDK initialization works when you pass the key directly, but fails when relying on environment variables (even after a full computer restart), check for security software or corporate Group Policies blocking script execution or access to environment blocks.
If you can read the variable, but the OpenAI API continues to return a 401 Unauthorized or Invalid API Key error, your key may have been deleted, revoked, or assigned to a different project. Log into your OpenAI developer dashboard, generate a brand new API key, and verify that your billing account has an active credit balance.