Environment Variable Management & Security¶
This guide provides comprehensive documentation for managing environment variables and API keys securely in the Swarms framework.
Overview¶
Swarms uses environment variables for configuration management and secure credential storage. This approach keeps sensitive information like API keys out of your code and allows for easy configuration changes across different environments.
Core Environment Variables¶
Framework Configuration¶
-
SWARMS_VERBOSE_GLOBAL
: Controls global logging verbosity -
WORKSPACE_DIR
: Defines the workspace directory for agent operations
API Keys¶
Model Provider Keys¶
- OpenAI
-
OPENAI_API_KEY
: Authentication for GPT models -
Anthropic
-
ANTHROPIC_API_KEY
: Authentication for Claude models -
Google
-
GEMINI_API_KEY
: Authentication for Gemini models -
Hugging Face
-
HUGGINGFACE_TOKEN
: Access to Hugging Face models -
Perplexity AI
-
PPLX_API_KEY
: Access to Perplexity models -
AI21
AI21_API_KEY
: Access to AI21 models
Tool Provider Keys¶
- Search Tools
BING_BROWSER_API
: Bing search capabilitiesBRAVESEARCH_API_KEY
: Brave search integrationTAVILY_API_KEY
: Tavily search services-
YOU_API_KEY
: You.com search integration -
Analytics & Monitoring
AGENTOPS_API_KEY
: AgentOps monitoring-
EXA_API_KEY
: Exa.ai services -
Browser Automation
MULTION_API_KEY
: Multi-browser automation
Security Best Practices¶
1. Environment File Management¶
- Create a
.env
file in your project root - Never commit
.env
files to version control - Add
.env
to your.gitignore
:
2. API Key Security¶
- Rotate API keys regularly
- Use different API keys for development and production
- Never hardcode API keys in your code
- Limit API key permissions to only what's necessary
- Monitor API key usage for unusual patterns
3. Template Configuration¶
Create a .env.example
template without actual values:
# Required Configuration
OPENAI_API_KEY=""
ANTHROPIC_API_KEY=""
WORKSPACE_DIR="agent_workspace"
# Optional Configuration
SWARMS_VERBOSE_GLOBAL="False"
4. Loading Environment Variables¶
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Access variables
workspace_dir = os.getenv("WORKSPACE_DIR")
openai_key = os.getenv("OPENAI_API_KEY")
Environment Setup Guide¶
-
Install Dependencies:
-
Create Environment File:
-
Configure Variables:
- Open
.env
in your text editor - Add your API keys and configuration
-
Save the file
-
Verify Setup:
Environment-Specific Configuration¶
Development¶
Production¶
Testing¶
Troubleshooting¶
Common Issues¶
- Environment Variables Not Loading
- Verify
.env
file exists in project root - Confirm
load_dotenv()
is called before accessing variables -
Check file permissions
-
API Key Issues
- Verify key format is correct
- Ensure key has not expired
-
Check for leading/trailing whitespace
-
Workspace Directory Problems
- Confirm directory exists
- Verify write permissions
- Check path is absolute when required