How to get Weights and Biases API key
Weights and Biases API key, sign up or log in at wandb.ai, then navigate to your account settings under Settings > API Keys. Copy the generated key and set it as the environment variable WANDB_API_KEY for secure usage in your Python projects.PREREQUISITES
Python 3.8+Internet accessBrowser to access wandb.aipip install wandb
Setup
Install the official wandb Python package to enable integration with Weights and Biases. Set up your environment variable to securely store your API key.
pip install wandb Step by step
Follow these steps to obtain and use your Weights and Biases API key:
- Go to https://wandb.ai and log in or create a free account.
- Click your profile icon in the top right corner and select Settings.
- Scroll to the API Keys section.
- Click + New API Key if you don't have one, or copy the existing key.
- Set the key as an environment variable in your terminal or shell:
export WANDB_API_KEY="your_api_key_here"(Linux/macOS)
orsetx WANDB_API_KEY "your_api_key_here"(Windows PowerShell) - Use
wandbin your Python code; it will automatically read the key from the environment.
import wandb
# Initialize a new run; wandb reads WANDB_API_KEY from environment
wandb.init(project="my-project")
print("Weights and Biases run initialized successfully.") Weights and Biases run initialized successfully.
Common variations
You can also authenticate using the wandb login CLI command, which prompts you to paste your API key interactively. For CI/CD pipelines, set WANDB_API_KEY as a secret environment variable. Additionally, you can manage multiple API keys from your account settings for different projects or teams.
import wandb
# Using wandb login CLI (run in terminal)
# $ wandb login
# In Python, initialize as usual
wandb.init(project="my-project") Troubleshooting
If you see authentication errors like wandb.errors.CommError: API key missing or invalid, verify that your WANDB_API_KEY environment variable is set correctly and that the key is active in your account. Use wandb login to re-authenticate if needed. Also, ensure your network allows outbound HTTPS connections to api.wandb.ai.
Key Takeaways
- Get your API key from wandb.ai under Settings > API Keys.
- Set the key as the environment variable WANDB_API_KEY for secure access.
- Use the wandb Python package which automatically reads the API key from the environment.
- You can authenticate interactively using the wandb CLI login command.
- Check environment variable and network if authentication errors occur.