Getting Started
Authentication
Learn how to authenticate with MegaLLM API
Authentication
MegaLLM uses API keys for authentication. This guide covers all authentication methods and best practices.
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
Option 1: Device Flow Authentication
The recommended method for obtaining an API key is through device flow:
# Start the authentication process
curl -X POST https://ai.megallm.io/auth/device \
-H "Content-Type: application/json"
You'll receive a response with:
device_code
: Your unique device identifieruser_code
: Code to enter for authenticationverification_uri
: URL to visit for authentication
Option 2: Dashboard
You can generate an API key from the MegaLLM dashboard:
- Visit the MegaLLM dashboard
- Navigate to API Keys section
- Generate a new API key
- Copy the key and store it securely
Security Best Practices
Never expose your tokens: Always store tokens in environment variables or secure vaults, never in code.
Environment Variables
# 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
# Set environment variable
[System.Environment]::SetEnvironmentVariable("MEGALLM_API_KEY", "your_api_key_here", "User")
# Or use command prompt
setx MEGALLM_API_KEY "your_api_key_here"
# In Dockerfile
ENV MEGALLM_API_KEY=${MEGALLM_API_KEY}
# Or in docker-compose.yml
environment:
- MEGALLM_API_KEY=${MEGALLM_API_KEY}
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 Code | Message | Solution |
---|---|---|
401 | Unauthorized | Check if your API key is valid and not expired |
403 | Forbidden | Verify API key has required access |
429 | Rate Limited | Wait 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.