pull/831/head
Kye Gomez 1 week ago
parent 9b1bc95237
commit 1b85f60ca0

@ -1,6 +1,4 @@
# Environment Variable Management & Security # Environment Variables
This guide provides comprehensive documentation for managing environment variables and API keys securely in the Swarms framework.
## Overview ## Overview
@ -10,80 +8,106 @@ Swarms uses environment variables for configuration management and secure creden
### Framework Configuration ### Framework Configuration
- `SWARMS_VERBOSE_GLOBAL`: Controls global logging verbosity === "Configuration Variables"
```bash
SWARMS_VERBOSE_GLOBAL="True" # Enable verbose logging | Variable | Description | Example |
SWARMS_VERBOSE_GLOBAL="False" # Disable verbose logging |----------|-------------|---------|
``` | `SWARMS_VERBOSE_GLOBAL` | Controls global logging verbosity | `True` or `False` |
| `WORKSPACE_DIR` | Defines the workspace directory for agent operations | `agent_workspace` |
- `WORKSPACE_DIR`: Defines the workspace directory for agent operations
```bash ### LLM Provider API Keys
WORKSPACE_DIR="agent_workspace"
``` === "OpenAI"
```bash
### API Keys OPENAI_API_KEY="your-openai-key"
```
#### Model Provider Keys
=== "Anthropic"
1. **OpenAI** ```bash
- `OPENAI_API_KEY`: Authentication for GPT models ANTHROPIC_API_KEY="your-anthropic-key"
```bash ```
OPENAI_API_KEY="your-openai-key"
``` === "Groq"
```bash
2. **Anthropic** GROQ_API_KEY="your-groq-key"
- `ANTHROPIC_API_KEY`: Authentication for Claude models ```
```bash
ANTHROPIC_API_KEY="your-anthropic-key" === "Google"
``` ```bash
GEMINI_API_KEY="your-gemini-key"
3. **Google** ```
- `GEMINI_API_KEY`: Authentication for Gemini models
=== "Hugging Face"
4. **Hugging Face** ```bash
- `HUGGINGFACE_TOKEN`: Access to Hugging Face models HUGGINGFACE_TOKEN="your-huggingface-token"
```
5. **Perplexity AI**
- `PPLX_API_KEY`: Access to Perplexity models === "Perplexity AI"
```bash
6. **AI21** PPLX_API_KEY="your-perplexity-key"
- `AI21_API_KEY`: Access to AI21 models ```
#### Tool Provider Keys === "AI21"
```bash
1. **Search Tools** AI21_API_KEY="your-ai21-key"
- `BING_BROWSER_API`: Bing search capabilities ```
- `BRAVESEARCH_API_KEY`: Brave search integration
- `TAVILY_API_KEY`: Tavily search services === "Cohere"
- `YOU_API_KEY`: You.com search integration ```bash
COHERE_API_KEY="your-cohere-key"
2. **Analytics & Monitoring** ```
- `EXA_API_KEY`: Exa.ai services
=== "Mistral AI"
3. **Browser Automation** ```bash
- `MULTION_API_KEY`: Multi-browser automation MISTRAL_API_KEY="your-mistral-key"
```
=== "Together AI"
```bash
TOGETHER_API_KEY="your-together-key"
```
### Tool Provider Keys
=== "Search Tools"
```bash
BING_BROWSER_API="your-bing-key"
BRAVESEARCH_API_KEY="your-brave-key"
TAVILY_API_KEY="your-tavily-key"
YOU_API_KEY="your-you-key"
```
=== "Analytics & Monitoring"
```bash
EXA_API_KEY="your-exa-key"
```
=== "Browser Automation"
```bash
MULTION_API_KEY="your-multion-key"
```
## Security Best Practices ## Security Best Practices
### 1. Environment File Management ### Environment File Management
- Create a `.env` file in your project root 1. Create a `.env` file in your project root
- Never commit `.env` files to version control 2. Never commit `.env` files to version control
- Add `.env` to your `.gitignore`: 3. Add `.env` to your `.gitignore`:
```bash ```bash
echo ".env" >> .gitignore echo ".env" >> .gitignore
``` ```
### 2. API Key Security ### API Key Security
- Rotate API keys regularly !!! warning "Important Security Considerations"
- Use different API keys for development and production - Rotate API keys regularly
- Never hardcode API keys in your code - Use different API keys for development and production
- Limit API key permissions to only what's necessary - Never hardcode API keys in your code
- Monitor API key usage for unusual patterns - Limit API key permissions to only what's necessary
- Monitor API key usage for unusual patterns
### 3. Template Configuration ### Template Configuration
Create a `.env.example` template without actual values: Create a `.env.example` template without actual values:
@ -91,13 +115,14 @@ Create a `.env.example` template without actual values:
# Required Configuration # Required Configuration
OPENAI_API_KEY="" OPENAI_API_KEY=""
ANTHROPIC_API_KEY="" ANTHROPIC_API_KEY=""
GROQ_API_KEY=""
WORKSPACE_DIR="agent_workspace" WORKSPACE_DIR="agent_workspace"
# Optional Configuration # Optional Configuration
SWARMS_VERBOSE_GLOBAL="False" SWARMS_VERBOSE_GLOBAL="False"
``` ```
### 4. Loading Environment Variables ### Loading Environment Variables
```python ```python
from dotenv import load_dotenv from dotenv import load_dotenv
@ -113,75 +138,65 @@ openai_key = os.getenv("OPENAI_API_KEY")
## Environment Setup Guide ## Environment Setup Guide
1. **Install Dependencies**: === "1. Install Dependencies"
```bash ```bash
pip install python-dotenv pip install python-dotenv
``` ```
2. **Create Environment File**: === "2. Create Environment File"
```bash ```bash
cp .env.example .env cp .env.example .env
``` ```
3. **Configure Variables**: === "3. Configure Variables"
- Open `.env` in your text editor - Open `.env` in your text editor
- Add your API keys and configuration - Add your API keys and configuration
- Save the file - Save the file
4. **Verify Setup**: === "4. Verify Setup"
```python ```python
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
assert os.getenv("OPENAI_API_KEY") is not None, "OpenAI API key not found" assert os.getenv("OPENAI_API_KEY") is not None, "OpenAI API key not found"
``` ```
## Environment-Specific Configuration ## Environment-Specific Configuration
### Development === "Development"
```bash
WORKSPACE_DIR="agent_workspace"
SWARMS_VERBOSE_GLOBAL="True"
```
```bash === "Production"
WORKSPACE_DIR="agent_workspace" ```bash
SWARMS_VERBOSE_GLOBAL="True" WORKSPACE_DIR="/var/swarms/workspace"
``` SWARMS_VERBOSE_GLOBAL="False"
```
### Production
```bash
WORKSPACE_DIR="/var/swarms/workspace"
SWARMS_VERBOSE_GLOBAL="False"
```
### Testing
```bash === "Testing"
WORKSPACE_DIR="test_workspace" ```bash
SWARMS_VERBOSE_GLOBAL="True" WORKSPACE_DIR="test_workspace"
``` SWARMS_VERBOSE_GLOBAL="True"
```
## Troubleshooting ## Troubleshooting
### Common Issues ### Common Issues
1. **Environment Variables Not Loading** ???+ note "Environment Variables Not Loading"
- Verify `.env` file exists in project root - Verify `.env` file exists in project root
- Confirm `load_dotenv()` is called before accessing variables - Confirm `load_dotenv()` is called before accessing variables
- Check file permissions - Check file permissions
2. **API Key Issues**
- Verify key format is correct
- Ensure key has not expired
- Check for leading/trailing whitespace
3. **Workspace Directory Problems**
- Confirm directory exists
- Verify write permissions
- Check path is absolute when required
## Additional Resources
- [Swarms Documentation](https://docs.swarms.world) ???+ note "API Key Issues"
- [Security Best Practices](https://swarms.world/security) - Verify key format is correct
- [API Documentation](https://swarms.world/docs/api) - Ensure key has not expired
- Check for leading/trailing whitespace
???+ note "Workspace Directory Problems"
- Confirm directory exists
- Verify write permissions
- Check path is absolute when required

Loading…
Cancel
Save