> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crewship.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment Variables

> Configure secrets and runtime settings for your crew

## Overview

Environment variables allow you to:

* Store API keys and secrets securely
* Configure runtime behavior without code changes
* Use different settings across environments

## Setting Variables

### Via CLI

```bash theme={null}
crewship env set OPENAI_API_KEY=sk-proj-...
```

### Via Console

1. Open the [Crewship Console](https://console.crewship.dev)
2. Navigate to your project
3. Go to **Settings** → **Environment Variables**
4. Click **Add Variable**

## Common Variables

### LLM API Keys

```bash theme={null}
# OpenAI
crewship env set OPENAI_API_KEY=sk-proj-...

# Anthropic
crewship env set ANTHROPIC_API_KEY=sk-ant-...

# Google AI
crewship env set GOOGLE_API_KEY=...

# Azure OpenAI
crewship env set AZURE_OPENAI_API_KEY=...
crewship env set AZURE_OPENAI_ENDPOINT=https://...
```

### Tool API Keys

```bash theme={null}
# Web search (Serper)
crewship env set SERPER_API_KEY=...

# Browserless
crewship env set BROWSERLESS_API_KEY=...

# Firecrawl
crewship env set FIRECRAWL_API_KEY=...
```

### Custom Configuration

```bash theme={null}
# Logging level
crewship env set LOG_LEVEL=debug

# Feature flags
crewship env set ENABLE_CACHING=true

# Custom settings
crewship env set MAX_RETRIES=3
```

## Accessing in Code

Variables are available as standard environment variables:

```python theme={null}
import os

# Direct access
api_key = os.environ["OPENAI_API_KEY"]

# With default
log_level = os.environ.get("LOG_LEVEL", "info")

# Check if set
if os.environ.get("ENABLE_CACHING"):
    enable_cache()
```

### CrewAI Integration

CrewAI automatically uses standard environment variables:

```python theme={null}
from crewai import Agent, LLM

# Uses OPENAI_API_KEY automatically
agent = Agent(
    role="Researcher",
    llm=LLM(model="gpt-4o")
)

# Anthropic (uses ANTHROPIC_API_KEY)
agent = Agent(
    role="Writer",
    llm=LLM(model="claude-3-5-sonnet-20241022")
)
```

## Reserved Variables

Crewship sets these automatically. They're read-only:

| Variable                 | Description                   |
| ------------------------ | ----------------------------- |
| `CREWSHIP_RUN_ID`        | Current run ID (`run_abc123`) |
| `CREWSHIP_DEPLOYMENT_ID` | Deployment ID (`dep_xyz789`)  |
| `CREWSHIP_PROJECT`       | Project name                  |
| `CREWSHIP_ENVIRONMENT`   | `production` or `preview`     |

Use them for logging and debugging:

```python theme={null}
import os

run_id = os.environ.get("CREWSHIP_RUN_ID")
print(f"Starting run {run_id}")
```

## Security

### Encryption

All environment variables are:

* **Encrypted at rest** using AES-256
* **Encrypted in transit** via TLS
* **Never logged** in build or run output
* **Access controlled** by project permissions

### Best Practices

<AccordionGroup>
  <Accordion title="Never commit secrets">
    Add `.env` to `.gitignore`. Use `crewship env set` instead.
  </Accordion>

  <Accordion title="Use specific keys">
    Create API keys specific to Crewship. Easier to rotate and audit.
  </Accordion>

  <Accordion title="Fail fast">
    Don't use fallback values for required secrets:

    ```python theme={null}
    # ❌ Bad - silently fails
    api_key = os.environ.get("API_KEY", "")

    # ✅ Good - fails immediately
    api_key = os.environ["API_KEY"]
    ```
  </Accordion>

  <Accordion title="Document requirements">
    List required variables in your README:

    ```markdown theme={null}
    ## Environment Variables

    - `OPENAI_API_KEY` - Required. OpenAI API key
    - `SERPER_API_KEY` - Required for web search
    - `LOG_LEVEL` - Optional. Default: info
    ```
  </Accordion>
</AccordionGroup>

## Loading from .env

For local development, use a `.env` file:

```text .env theme={null}
OPENAI_API_KEY=sk-proj-...
SERPER_API_KEY=...
LOG_LEVEL=debug
```

Then load with `python-dotenv`:

```python theme={null}
from dotenv import load_dotenv
load_dotenv()
```

<Warning>
  The `.env` file is **not** used in Crewship deployments. Always set production variables via
  `crewship env set`.
</Warning>

## Variable Scope

Variables are scoped to **projects**, not deployments:

```
Project: my-crew
├── OPENAI_API_KEY = sk-...
├── SERPER_API_KEY = ...
│
├── Deployment: dep_abc (uses these vars)
├── Deployment: dep_xyz (uses these vars)
└── All runs use these vars
```

### Updating Variables

Changes take effect immediately for new runs:

```bash theme={null}
# Update a variable
crewship env set OPENAI_API_KEY=sk-new-key-...

# Next run uses new value (no redeploy needed)
crewship invoke --input '{"topic": "test"}'
```

## Environment Separation

Use separate projects for different environments:

```bash theme={null}
# Production
crewship env set OPENAI_API_KEY=sk-prod-... --project my-crew
crewship env set LOG_LEVEL=warning --project my-crew

# Staging
crewship env set OPENAI_API_KEY=sk-test-... --project my-crew-staging
crewship env set LOG_LEVEL=debug --project my-crew-staging
```

## Related

<CardGroup cols={2}>
  <Card title="CLI env Command" icon="terminal" href="/cli/env">
    Full CLI reference
  </Card>

  <Card title="Configuration" icon="gear" href="/configuration/crewship-toml">
    Project configuration
  </Card>
</CardGroup>
