Authentication Methods
Bearer Token
The most common authentication method is using a Bearer token in the Authorization header:
Authorization: Bearer YOUR_API_KEY
For Anthropic-compatible endpoints, you can also use the x-api-key header:
Getting Your API Key
Dashboard Method
The recommended method for obtaining an API key is through the MegaLLM dashboard:
- Visit megallm.io/dashboard
- Navigate to API Keys section
- Click “Create New API Key”
- Copy the key (starts with
sk-mega-) and store it securely
CLI Tool
If you have the MegaLLM CLI installed: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
# 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.