Authentication
Learn how to authenticate with the Metigan API using API keys. All API requests require authentication via an API key in your request headers.
API Keys
Metigan uses API keys to authenticate requests. You can create and manage your API keys in your Metigan Dashboard. API keys are scoped to your account and provide access to all features based on your plan.
Getting Your API Key
Follow these steps to get your API key:
Log in to Dashboard
Navigate to API Keys
Go to Settings → API Keys section
Create API Key
Click the 'Create API Key' button
Copy Your Key
Copy your API key immediately (you'll only see it once!)
Initializing the Client
Initialize the Metigan client with your API key. Always use environment variables in production:
import Metigan from 'metigan';
// Initialize with your API key from environment variable
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY || 'your_api_key_here'
});
// Make sure API key is set
if (!process.env.METIGAN_API_KEY) {
throw new Error('METIGAN_API_KEY environment variable is required');
}Always use environment variables for your API keys. Never commit them to version control. Use services like dotenv for local development and environment variables for production.
Using Environment Variables
Store your API key in an environment variable for security. This is the recommended approach for all environments:
Create a .env file:
METIGAN_API_KEY=sp_your_api_key_hereLoad the API key in your code:
import Metigan from 'metigan';
// Load API key from environment variable
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY!
});
// Validate that API key is set
if (!process.env.METIGAN_API_KEY) {
throw new Error('METIGAN_API_KEY environment variable is not set');
}
console.log('✅ Metigan client initialized successfully');API Key Permissions
API keys can have different permission levels depending on your plan and configuration:
| Permission Level | Description | Access |
|---|---|---|
| full_access | Full access to all API endpoints and resources | All Features |
| sending_access | Can only send emails, cannot manage contacts or audiences | Email Only |
Use the principle of least privilege. Create separate API keys for different environments (development, staging, production) and use the minimum permissions required for each use case. This limits the impact if a key is compromised.
Error Handling
If your API key is invalid or missing, the SDK will throw an error. Here's how to handle authentication errors:
import Metigan from 'metigan';
import { MetiganError } from 'metigan';
try {
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY || 'invalid_key'
});
const result = await metigan.email.sendEmail({
from: 'sender@example.com',
recipients: ['recipient@example.com'],
subject: 'Test Email',
content: '<p>Test content</p>'
});
console.log('Email sent:', result);
} catch (error) {
if (error instanceof MetiganError) {
if (error.message.includes('API key') || error.message.includes('authentication')) {
console.error('⌠Authentication failed. Please check your API key.');
console.error('Make sure METIGAN_API_KEY environment variable is set correctly.');
} else {
console.error('⌠Metigan error:', error.message);
}
} else {
console.error('⌠Unexpected error:', error);
}
throw error;
}