Tickd.ai
API errors

Fix Claude API Key Environment Variable Not Found Error

Updated 9/24/2026

When setting up the Anthropic SDK to work with Claude, the library expects your API key to be stored in an environment variable named ANTHROPIC_API_KEY. If this variable is missing, incorrectly named, or loaded out of order, you will receive an initialization error such as:

AnthropicError: The ANTHROPIC_API_KEY environment variable is not set

This guide explains how to properly expose your API key to your local shell, your development projects, and your production containers.

Step 1: Verify the Variable Name and Scope

The official Anthropic SDKs strictly look for the string ANTHROPIC_API_KEY. A common mistake is naming the variable CLAUDE_API_KEY or ANTHROPIC_KEY.

Additionally, environment variables set in a terminal window are scoped only to that specific terminal session. If you open a new terminal window or run your code in an IDE debugger, the variable may not be present.

To check if the variable is set in your current terminal session, run the following command:

  • Mac/Linux: echo $ANTHROPIC_API_KEY
  • Windows (PowerShell): $env:ANTHROPIC_API_KEY
  • Windows (CMD): echo %ANTHROPIC_API_KEY%

If the output is blank, the variable is not set. Temporarily export it in your current terminal run:

  • Mac/Linux: export ANTHROPIC_API_KEY="your_actual_key_here"
  • Windows (PowerShell): $env:ANTHROPIC_API_KEY="your_actual_key_here"
  • Windows (CMD): set ANTHROPIC_API_KEY="your_actual_key_here"

*Note: Do not include the angled brackets or spaces around the = sign when setting the variable.*

Step 2: Configure Local Development (.env files)

Setting variables in the terminal is temporary. For local projects, using a .env file is standard practice. However, you must ensure your application loads this file before initializing the Anthropic client.

In Python

1. Install the dotenv package: pip install python-dotenv 2. Create a .env file in the root directory of your project: `env ANTHROPIC_API_KEY=sk-ant-api03-xxxxxxxxx ` 3. Load the .env file at the very top of your entry point file (main.py or app.py) *before* importing or calling the Anthropic client: `python from dotenv import load_dotenv import os # Load variables from .env first load_dotenv() from anthropic import Anthropic # Initialize without passing explicit keys client = Anthropic() `

In Node.js / TypeScript

1. Install the dotenv package: npm install dotenv 2. Create your .env file in the root directory. 3. Load the environment config before calling the SDK: `javascript import 'dotenv/config'; // Loads variables immediately import Anthropic from '@anthropic-ai/sdk'; const anthropic = new Anthropic(); `

Step 3: Configure IDE Debuggers

If your code works when run directly from the terminal, but fails when you hit "Run/Debug" inside VS Code, PyCharm, or WebStorm, your IDE is executing the script in an isolated environment that does not inherit your system's shell profile (.bashrc or .zshrc).

- VS Code: Open your .vscode/launch.json file and add the env block to your run configuration: `json { "configurations": [ { "type": "python", "request": "launch", "name": "Python: Debug File", "program": "${file}", "env": { "ANTHROPIC_API_KEY": "your_actual_key_here" } } ] } ` - PyCharm: Open the "Run/Debug Configurations" dropdown, choose your script, click on "Environment variables," and add a new entry with the name ANTHROPIC_API_KEY and your key as the value.

Step 4: Pass the Key Explicitly (Fallback)

If your production host restrictively strips environment variables for security reasons, you can pass the API key explicitly as an initialization parameter.

*Warning: Never hardcode your API key string in your repository code. Read it from an alternative database or config manager instead.*

For Python: `python client = Anthropic(api_key=my_custom_config_lookup()) `

For Node.js: `javascript const anthropic = new Anthropic({ apiKey: myCustomConfigLookup() }); `

When to escalate

If you have confirmed that your environment variable is correctly set and loaded by your app, but you receive a 401 Unauthorized or Invalid API Key error, your environment variable is working, but the key itself has been deactivated, deleted, or contains a copy-paste typo. Go to the Anthropic Developer Console, generate a fresh API key, and replace the old one in your configuration.

Quick fixes

  • Claude is down or not loading
  • Claude Pro billing or payment problem
  • Can't sign in to Claude

While you're here

Tickd is more than troubleshooting — these three are free and take seconds.

Agent BuilderDesign your own AI agent and export it to ChatGPT, Claude, Gemini or Grok.Build one free