Fix Anthropic API Key Not Found Environment Variable
Updated 9/7/2026
Why Is the SDK Failing to Find the API Key?
When initializing the Anthropic SDK in Python or Node.js, the client library looks for a system-level environment variable named ANTHROPIC_API_KEY. If this variable is empty, undefined, or named incorrectly, your code will fail to initialize with an initialization error, such as AnthropicError: The ANTHROPIC_API_KEY environment variable is not set.
Hardcoding API keys directly into your application code is a security vulnerability. Setting up a system environment variable protects your credentials and ensures your application compiles correctly across development and production environments.
Step 1: Set the Variable permanently on macOS and Linux
Setting a variable temporarily using your terminal terminal only works for your current terminal session. To ensure the key remains configured when your system restarts or when your IDE opens a new terminal process, add it directly to your shell configuration profile.
1. Open your terminal. 2. Determine which shell you are using by running echo $SHELL. Usually, this is /bin/zsh (macOS default) or /bin/bash (Linux default). 3. Open your shell configuration file using a text editor like Nano: * For Zsh: nano ~/.zshrc * For Bash: nano ~/.bashrc 4. Scroll to the bottom of the file and paste the export line, replacing the placeholder with your actual API key: `bash export ANTHROPIC_API_KEY="sk-ant-api03-xxxxxx" ` 5. Save the file by pressing Ctrl + O, then exit Nano by pressing Ctrl + X. 6. Reload your shell configurations immediately without restarting your terminal: * For Zsh: source ~/.zshrc * For Bash: source ~/.bashrc 7. Verify the variable is set by running echo $ANTHROPIC_API_KEY. Your key should print to the terminal screen.
Step 2: Set the Variable permanently on Windows
On Windows systems, you must configure the environment variable using either Command Prompt, PowerShell, or the Windows Graphical User Interface (GUI).
Using PowerShell (Recommended) 1. Open PowerShell. 2. Run the following command to set the variable for your specific user account: ```powershell [System.Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', 'sk-ant-api03-xxxxxx', 'User') ``` 3. Close your PowerShell window and reopen a new one to apply the change.
Using Command Prompt 1. Open Command Prompt. 2. Run the following command: ```cmd setx ANTHROPIC_API_KEY "sk-ant-api03-xxxxxx" ``` 3. Close and reopen the command prompt to apply the change.
Using the Windows GUI 1. Press the Windows Key and type "environment variables". Select **Edit the system environment variables**. 2. Click the **Environment Variables** button near the bottom right. 3. In the **User variables** section, click **New**. 4. Enter `ANTHROPIC_API_KEY` as the variable name, and enter your actual API key as the variable value. 5. Click **OK** to save and exit out of all open dialog menus.
Step 3: Configure Environment Variables in Your IDE
Even if you set the environment variable system-wide, integrated development environments (IDEs) like VS Code, PyCharm, or WebStorm may not automatically load new variables unless they are fully restarted.
1. Save all your work in your IDE. 2. Close the IDE completely. 3. Reopen the IDE from the newly updated terminal context, or restart your computer to force the editor to reload all system paths. 4. If using a local file like .env in a Node.js or Python environment, ensure you are loading the file explicitly in your code using libraries like dotenv (Node) or python-dotenv (Python) before initializing the Anthropic client: * Python: `python from dotenv import load_dotenv load_dotenv() # This must run before importing or initializing the SDK ` * Node.js: `javascript require('dotenv').config(); `
Step 4: Pass the Key Directly in Code (Debugging Only)
If you need to bypass environment variable resolution to determine if your code itself is working, you can pass your API key directly into the Anthropic client constructor.
*Warning: Only do this for local diagnostic testing. Do not commit hardcoded API keys to public code repositories like GitHub.*
* Python SDK: `python from anthropic import Anthropic
client = Anthropic( api_key="sk-ant-api03-xxxxxx", ) `
* Node.js SDK: `javascript import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({ apiKey: 'sk-ant-api03-xxxxxx', }); `
When to Escalate
If you have confirmed that your environment variable is correctly set and prints properly via terminal check tools, but your hosting platform (like AWS, Vercel, Heroku, or Render) fails to authenticate, double-check your platform's specific deployment settings. Environment variables on cloud providers must be manually set within their web consoles or CI/CD pipelines, as local machine variables do not transfer to cloud runtimes automatically.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude