# How to Set Up AutoHedge with Your API Keys

> Easily set up AutoHedge using your API keys. Learn how to configure your environment variables and start automating your hedging strategies effectively.

- Repository: [Swarms/AutoHedge](https://github.com/The-Swarm-Corporation/AutoHedge)
- Tags: how-to-guide
- Published: 2026-09-09

---

**AutoHedge reads all required secrets from a `.env` file at startup, with [`autohedge/env_loader.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/env_loader.py) validating critical variables like `OPENAI_API_KEY` while individual tool modules lazily load service-specific keys via `os.getenv()`.**

Setting up AutoHedge from The-Swarm-Corporation/AutoHedge requires configuring multiple API keys to enable AI swarms, Solana trading, and data aggregation services. The project uses a centralized environment variable system that loads secrets from a `.env` file at the project root, ensuring credentials remain secure and separate from source code.

## Required API Keys and Environment Variables

AutoHedge integrates with several external services, each requiring specific authentication tokens. These variables are read from the environment at runtime using standard `os.getenv()` calls.

### Core Required Variables

- **`OPENAI_API_KEY`** – Required for all AI swarm operations. The [`autohedge/env_loader.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/env_loader.py) module verifies this key exists via the `has_openai_key()` function, which returns `bool(os.getenv("OPENAI_API_KEY"))`.
- **`JUPITER_API_KEY`** – Required for Solana DEX operations. Used in [`autohedge/tools/ultra_tools.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/ultra_tools.py), [`autohedge/tools/jupiter_search.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/jupiter_search.py), and [`autohedge/tools/jupiter_price.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/jupiter_price.py).
- **`SOLANA_PRIVATE_KEY`** – Base58-encoded private key for signing Solana transactions. Loaded in [`autohedge/tools/ultra_tools.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/ultra_tools.py) alongside the Jupiter API key.
- **`MASSIVE_API_KEY`** – Required for Polygon market data access via [`autohedge/tools/polygon_api.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/polygon_api.py).
- **`EXA_API_KEY`** – Required for web search capabilities in [`autohedge/tools/exa_search_tool.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/exa_search_tool.py).

### Optional and Lazy-Loaded Keys

Because AutoHedge fetches keys lazily, you only need to provide credentials for the services you intend to use. For example, if your workflow does not require Polygon data, you can omit `MASSIVE_API_KEY` without preventing the application from starting.

## Creating Your .env Configuration File

The project expects environment variables in a `.env` file located at the project root, alongside [`autohedge/main.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/main.py).

### Step 1: Copy the Template

Create your configuration by copying the provided template:

```bash
cp .env.example .env

```

### Step 2: Populate Your API Keys

Edit the `.env` file and replace placeholder values with your actual credentials:

```text

# OpenAI Configuration

OPENAI_API_KEY=sk-your-openai-key-here

# Solana/Jupiter Trading

JUPITER_API_KEY=your-jupiter-api-key
SOLANA_PRIVATE_KEY=your-base58-encoded-private-key

# Data and Search Services

MASSIVE_API_KEY=your-massive-api-key
EXA_API_KEY=your-exa-api-key

```

### Step 3: Verify File Location

Ensure the `.env` file resides in the project root directory. The loader in [`autohedge/main.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/main.py) imports `autohedge.env_loader`, which initializes `python-dotenv` to read from this location automatically.

## How the AutoHedge Environment Loader Works

The environment loading mechanism operates through two distinct phases: validation and lazy access.

### Startup Validation

When you launch the CLI, [`autohedge/cli.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/cli.py) imports the validation helper and checks for the OpenAI key:

```python

# autohedge/cli.py

from autohedge.env_loader import has_openai_key

if not has_openai_key():
    console.print("[yellow]Warning: OPENAI_API_KEY not set. Set it in .env or export it.[/]")

```

This warning appears if `OPENAI_API_KEY` is missing, but the application continues loading to allow partial functionality.

### Lazy Loading in Tool Modules

Individual tools fetch their specific credentials only when invoked. For example, the Jupiter price tool retrieves its API key at execution time:

```python

# autohedge/tools/jupiter_price.py

import os

def get_price(pair):
    key = os.getenv("JUPITER_API_KEY")
    # ... API call logic

```

Similarly, [`autohedge/tools/ultra_tools.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/ultra_tools.py) reads both `JUPITER_API_KEY` and `SOLANA_PRIVATE_KEY` via `os.getenv()` when preparing signed transactions.

## Verifying Your Setup

After configuring your `.env` file, verify the installation by running the CLI:

```bash
python -m autohedge.cli run

```

If any required keys are missing, you will see yellow warning messages in the console output. To test specific tools without running the full application:

```python
from autohedge.tools.jupiter_price import get_price

# This will only work if JUPITER_API_KEY is set in .env

price = get_price("SOLUSDT")
print(f"Current SOL price: {price}")

```

Successful execution confirms that the environment variables are properly loaded and accessible to the tool modules.

## Summary

- **AutoHedge uses a `.env` file** at the project root to manage all API keys and secrets.
- **[`autohedge/env_loader.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/env_loader.py)** validates the presence of `OPENAI_API_KEY` at startup, while other keys are loaded lazily by their respective tools.
- **Five primary keys** are supported: `OPENAI_API_KEY`, `JUPITER_API_KEY`, `SOLANA_PRIVATE_KEY`, `MASSIVE_API_KEY`, and `EXA_API_KEY`.
- **Copy `.env.example`** to `.env` and populate it with your credentials to configure the system.
- **Missing keys trigger warnings** in [`autohedge/cli.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/cli.py) but do not prevent the application from running if the functionality requiring those keys is not invoked.

## Frequently Asked Questions

### What happens if I don't set all API keys in AutoHedge?

AutoHedge employs lazy loading for API keys, meaning each tool only fetches its credentials when actually called. If you omit `MASSIVE_API_KEY` or `EXA_API_KEY`, the application will start normally, but attempting to use Polygon data or Exa search will fail at runtime. However, `OPENAI_API_KEY` triggers a warning in the CLI if missing, as it is required for core swarm functionality.

### Can I use system environment variables instead of a .env file?

Yes. While AutoHedge looks for a `.env` file by default through `python-dotenv`, the `os.getenv()` calls in modules like [`autohedge/tools/jupiter_price.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/jupiter_price.py) and [`autohedge/tools/ultra_tools.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/ultra_tools.py) will read from your shell environment if the variables are exported there. The `.env` file simply provides a convenient way to manage multiple keys without polluting your global environment.

### Where should I place the .env file for AutoHedge?

Place the `.env` file in the project root directory, adjacent to [`autohedge/main.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/main.py). This location ensures that when [`autohedge/env_loader.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/env_loader.py) initializes, `python-dotenv` can automatically locate and load the variables before any tool modules attempt to access them.

### Is the SOLANA_PRIVATE_KEY required for all AutoHedge features?

No. The `SOLANA_PRIVATE_KEY` is only required when executing Solana-based trades through tools in [`autohedge/tools/ultra_tools.py`](https://github.com/The-Swarm-Corporation/AutoHedge/blob/main/autohedge/tools/ultra_tools.py). If you are using AutoHedge purely for AI swarm operations with OpenAI or data aggregation with Massive, you can omit this key. The private key is base58-encoded and used specifically for signing transactions on the Solana network.