In this categoryTroubleshooting ยท 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to Fix an API Key That Loads as Undefined
Solve the classic 'undefined API key' bug by loading your .env file correctly in Node and Python.
Your key is right but the app still says it is undefined. This is one of the most common AI setup bugs and it has nothing to do with the provider. The environment variable is simply not reaching your code, usually because the .env file is in the wrong place, loaded too late, or named incorrectly.
- A project using dotenv (Node) or python-dotenv
- A .env file with your key
- A terminal to run quick checks
Step 1: Confirm the file name and location
The file must be named exactly .env with the leading dot, and it must sit in the directory where you run the process, not a subfolder. A file called env.txt or one in src/ while you run from the root will not be found.
Step 2: Load dotenv before you read the variable
In Node, you must call dotenv config before any code reads process.env. If you import a module that reads the key at the top of the file before config runs, the value is undefined. Load it first, ideally at the very top of your entry file.
import "dotenv/config"; // must be the first import
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
console.log("key loaded:", Boolean(process.env.OPENAI_API_KEY));Step 3: Check for quotes and spaces
A key wrapped in quotes or with spaces around the equals sign can load with the quotes included or not load at all. Keep each line as KEY=value with no spaces and no surrounding quotes.
Step 4: Verify nothing else overrides it
A shell variable, a Docker env, or a deploy platform secret can override or shadow the .env value. Print the final resolved value once at startup to be sure the process sees what you expect.
Result
Once dotenv loads first and the file has clean KEY=value lines at the root, the startup log prints key loaded: true and the API call succeeds. In a frequent variant, a developer imported the OpenAI client in a separate module that ran before dotenv config, so moving the import dotenv/config line to the top of the entry file fixed it.
Watch related tutorials
19:27
14:22
1:42:18
28:14
41:09
9:47New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.