Skip to main content

Environment Setup

1. Store Your API Key Securely

  • Linux/macOS
  • Windows
  • .env File
Add to your shell configuration file:
# ~/.bashrc or ~/.zshrc
export MEGALLM_API_KEY="your-api-key-here"
Then reload:
source ~/.bashrc
# or
source ~/.zshrc
Or use a .env file:
echo "MEGALLM_API_KEY=your-api-key-here" >> .env

2. Install SDK

  • Python
  • JavaScript/TypeScript
  • Go
  • Other Languages
# OpenAI SDK (recommended)
pip install openai

# Or Anthropic SDK
pip install anthropic

# For environment variables
pip install python-dotenv

3. Configure Your Client

  • Python - OpenAI Format
  • Python - Anthropic Format
  • JavaScript - OpenAI Format
  • JavaScript - Anthropic Format
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://ai.megallm.io/v1",
    api_key=os.getenv("MEGALLM_API_KEY")
)

# Test the connection
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Project Setup

For New Projects

  1. Create project directory:
    mkdir my-ai-project
    cd my-ai-project
    
  2. Initialize project:
    # Python
    python -m venv venv
    source venv/bin/activate
    pip install openai python-dotenv
    
    # JavaScript
    npm init -y
    npm install openai dotenv
    
  3. Create .env file:
    echo "MEGALLM_API_KEY=your-key-here" > .env
    echo ".env" >> .gitignore
    
  4. Create first script: See First Request Guide

For Existing Projects

If you’re already using OpenAI or Anthropic:
  1. Update base URL:
    # Before
    client = OpenAI(api_key="sk-...")
    
    # After
    client = OpenAI(
        base_url="https://ai.megallm.io/v1",
        api_key="your-megallm-key"
    )
    
  2. That’s it! All your existing code works.

IDE Setup

VS Code

Install recommended extensions:
  • Python extension (for Python)
  • ESLint (for JavaScript)
  • REST Client (for testing APIs)

PyCharm / IntelliJ

Configure environment variables in Run Configurations.

AI Coding Assistants

MegaLLM provides a CLI to configure AI coding tools:
npx megallm@latest
This sets up:
  • Claude Code
  • Codex/Windsurf
  • OpenCode
See CLI Documentation for details.

Verify Setup

Test your configuration:
  • Python
  • JavaScript
  • cURL
import os
from openai import OpenAI

# Check API key
api_key = os.getenv("MEGALLM_API_KEY")
if not api_key:
    print("❌ API key not set!")
    exit(1)
print("✅ API key found")

# Test connection
client = OpenAI(
    base_url="https://ai.megallm.io/v1",
    api_key=api_key
)

try:
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Say 'Setup successful!'"}],
        max_tokens=10
    )
    print("✅ Connection successful!")
    print(f"Response: {response.choices[0].message.content}")
except Exception as e:
    print(f"❌ Error: {e}")

Troubleshooting

Problem: Environment variable not setSolution:
  • Check variable name: MEGALLM_API_KEY
  • Reload shell: source ~/.bashrc
  • Verify: echo $MEGALLM_API_KEY
Problem: Invalid API keySolution:
  • Verify key starts with sk-mega-
  • Check for extra spaces
  • Generate new key at dashboard
Problem: Network or firewall issuesSolution:
  • Check internet connection
  • Verify firewall settings
  • Try without VPN
Problem: SDK not installedSolution:
pip install openai  # Python
npm install openai  # JavaScript

Next Steps