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:

2

Navigate to API Keys

Go to Settings → API Keys section

3

Create API Key

Click the 'Create API Key' button

4

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:

initialize.tsTypeScript
1
2
3
4
5
6
7
8
9
10
11
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');
}
Never Commit API Keys

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:

.envTerminal
1
METIGAN_API_KEY=sp_your_api_key_here

Load the API key in your code:

with-env.tsTypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
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 LevelDescriptionAccess
full_accessFull access to all API endpoints and resourcesAll Features
sending_accessCan only send emails, cannot manage contacts or audiencesEmail Only
Best Practice: Principle of Least Privilege

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:

error-handling.tsTypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
}