High severity HTTP 401 beginner · Fix: 2-5 min

AuthenticationError

wandb.errors.CommError.AuthenticationError

What this error means
Weights & Biases (wandb) throws AuthenticationError when the API key is not set or not found in environment variables.

Stack trace

traceback
wandb.errors.CommError.AuthenticationError: API key not set. Please set your WANDB_API_KEY environment variable or run `wandb login`.
QUICK FIX
Set the WANDB_API_KEY environment variable with your API key before running your script.

Why it happens

Weights & Biases requires an API key to authenticate your client with their servers. If the API key is missing from environment variables or not configured via `wandb login`, the client cannot authenticate and raises this error.

Detection

Check for AuthenticationError exceptions when initializing wandb or starting a run; verify if WANDB_API_KEY is set in your environment before running your script.

Causes & fixes

1

WANDB_API_KEY environment variable is not set or empty

✓ Fix

Set your API key in the environment by running `export WANDB_API_KEY='your_api_key'` on Linux/macOS or `setx WANDB_API_KEY 'your_api_key'` on Windows.

2

User did not run `wandb login` to configure API key locally

✓ Fix

Run `wandb login` in your terminal and enter your API key to store it securely for wandb usage.

3

API key is set but in the wrong environment or session

✓ Fix

Ensure the environment variable is set in the same shell or environment where your Python script runs, including IDE or container environments.

Code: broken vs fixed

Broken - triggers the error
python
import wandb
wandb.init(project='my-project')  # triggers AuthenticationError if API key not set
Fixed - works correctly
python
import os
import wandb
os.environ['WANDB_API_KEY'] = 'your_api_key_here'  # Set your API key securely
wandb.init(project='my-project')  # now authenticates successfully
print('wandb run started')
Added setting WANDB_API_KEY in environment variables before wandb.init to authenticate properly and avoid AuthenticationError.

Workaround

Catch AuthenticationError and prompt the user to set the API key or fallback to offline mode with `wandb.init(mode='offline')` to avoid crashes.

Prevention

Always configure your W&B API key in environment variables or run `wandb login` before running scripts that use wandb to ensure seamless authentication.

Python 3.6+ · wandb >=0.10.0 · tested on 0.15.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.