API Authentication
Learn how to authenticate your requests to the Metigan API using API keys.
Getting Your API Key
To get your API key:
- Log in to your Metigan Dashboard
- Navigate to Settings → API Keys
- Click Create API Key
- Give your key a descriptive name (e.g., "Production Server", "Development")
- Copy and securely store your API key
Your API key grants full access to your Metigan account. Never share it publicly, commit it to version control, or expose it in client-side code.
Using Your API Key
Include your API key in the x-api-key header with every API request:
cURL
curl -X POST https://api.metigan.com/api/email/send \
-H "x-api-key: mtg_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"from": "hello@example.com", "recipients": ["user@example.com"], "subject": "Hello", "content": "<p>Hello World</p>"}'JavaScript (fetch)
const response = await fetch('https://api.metigan.com/api/email/send', {
method: 'POST',
headers: {
'x-api-key': process.env.METIGAN_API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify({
from: 'hello@example.com',
recipients: ['user@example.com'],
subject: 'Hello',
content: '<p>Hello World</p>'
})
});Python (requests)
import os
import requests
response = requests.post(
'https://api.metigan.com/api/email/send',
headers={
'x-api-key': os.environ['METIGAN_API_KEY'],
'Content-Type': 'application/json'
},
json={
'from': 'hello@example.com',
'recipients': ['user@example.com'],
'subject': 'Hello',
'content': '<p>Hello World</p>'
}
)PHP
<?php
$ch = curl_init('https://api.metigan.com/api/email/send');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'x-api-key: ' . getenv('METIGAN_API_KEY'),
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode([
'from' => 'hello@example.com',
'recipients' => ['user@example.com'],
'subject' => 'Hello',
'content' => '<p>Hello World</p>'
])
]);
$response = curl_exec($ch);
curl_close($ch);API Key Types
Metigan supports different API key types for different environments:
| Prefix | Environment | Description |
|---|---|---|
mtg_live_ | Production | Full access, sends real emails, counts against quota |
mtg_test_ | Test/Sandbox | Emails are not sent, for development and testing |
Use mtg_test_ keys during development to avoid sending real emails and consuming your email quota.
Environment Variables
Store your API key in environment variables to keep it secure:
.env file
# Production
METIGAN_API_KEY=mtg_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Development
METIGAN_API_KEY=mtg_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxAdd .env to your .gitignore file to prevent accidentally committing sensitive credentials.
Platform-specific Configuration
Vercel
Add to Settings → Environment Variables
Heroku
heroku config:set METIGAN_API_KEY=mtg_live_xxx
AWS
Use AWS Secrets Manager or Parameter Store
Docker
docker run -e METIGAN_API_KEY=mtg_live_xxx ...
Authentication Errors
Common authentication errors and how to resolve them:
401 Unauthorized
{
"success": false,
"error": "UNAUTHORIZED",
"message": "Invalid or missing API key"
}Solution: Check that your API key is correct and included in thex-api-key header.
403 Forbidden
{
"success": false,
"error": "FORBIDDEN",
"message": "API key does not have permission for this action"
}Solution: Your API key may have restricted permissions or be disabled. Check your API key settings in the dashboard.
429 Rate Limited
{
"success": false,
"error": "RATE_LIMIT_EXCEEDED",
"message": "Too many requests. Please retry after 60 seconds.",
"retryAfter": 60
}Solution: Implement exponential backoff and respect theretryAfter value.
Security Best Practices
✅ Do
- • Store API keys in environment variables
- • Use test keys during development
- • Rotate keys periodically
- • Use different keys for different environments
- • Monitor API key usage in dashboard
❌ Don't
- • Commit API keys to version control
- • Expose keys in client-side JavaScript
- • Share keys via email or chat
- • Use production keys for testing
- • Hard-code keys in your source code
Revoking API Keys
If you suspect your API key has been compromised:
- Go to Settings → API Keys in your dashboard
- Find the compromised key
- Click the Revoke button
- Create a new API key
- Update your application with the new key
Revoking an API key takes effect immediately. All requests using that key will start returning 401 errors.