Skip to main content

Authentication Methods

Bearer Token

The most common authentication method is using a Bearer token in the Authorization header:
Authorization: Bearer YOUR_API_KEY

API Key Header (Anthropic Format)

For Anthropic-compatible endpoints, you can also use the x-api-key header:
x-api-key: YOUR_API_KEY

Getting Your API Key

1

Dashboard Method

The recommended method for obtaining an API key is through the MegaLLM dashboard:
  1. Visit megallm.io/dashboard
  2. Navigate to API Keys section
  3. Click “Create New API Key”
  4. Copy the key (starts with sk-mega-) and store it securely
2

CLI Tool

If you have the MegaLLM CLI installed:
npx megallm@latest
Follow the interactive prompts to set up your API key.

Security Best Practices

Never expose your tokens: Always store tokens in environment variables or secure vaults, never in code.

Environment Variables

  • Linux/Mac
  • Windows
  • Docker
# Add to ~/.bashrc or ~/.zshrc
export MEGALLM_API_KEY="your_api_key_here"

# Or use a .env file
echo "MEGALLM_API_KEY=your_api_key_here" >> .env

Using SDKs

OpenAI SDK

from openai import OpenAI

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

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

Anthropic SDK

from anthropic import Anthropic

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

message = client.messages.create(
    model="claude-3.5-sonnet",
    max_tokens=100,
    messages=[{"role": "user", "content": "Hello!"}]
)

LangChain Integration

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://ai.megallm.io/v1",
    api_key=os.getenv("MEGALLM_API_KEY"),
    model="gpt-4"
)

response = llm.invoke("Hello!")

Troubleshooting

Common Authentication Errors

Error CodeMessageSolution
401UnauthorizedCheck if your API key is valid and not expired
403ForbiddenVerify API key has required access
429Rate LimitedWait and retry or contact support

Debugging Authentication

Enable debug mode to see detailed authentication information:
curl https://ai.megallm.io/v1/chat/completions \
  -H "Authorization: Bearer $MEGALLM_API_KEY" \
  -H "X-Debug-Auth: true" \
  -d '{"model": "gpt-4", "messages": [...]}'
Need Help? Check our FAQ or contact support if you’re experiencing authentication issues.