Node.js Examples
Node.js integration examples using the Metigan SDK. Learn how to send emails, manage contacts, and integrate Metigan into your Node.js applications.
Basic Setup
index.jsJavaScript
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
const Metigan = require('metigan');
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY
});
// Send an email
async function sendEmail() {
try {
const result = await metigan.email.sendEmail({
from: 'sender@example.com',
recipients: ['recipient@example.com'],
subject: 'Hello from Node.js!',
content: '<p>This email was sent from a Node.js application.</p>'
});
if (result.success) {
console.log('Email sent successfully!');
} else {
console.error('Failed to send email:', result.message);
}
} catch (error) {
console.error('Error:', error);
}
}
sendEmail();OTP Email (Verification Code)
Send One-Time Password emails with priority delivery and built-in rate limiting.
otp-example.jsJavaScript
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
30
31
32
33
34
35
36
37
38
39
40
const Metigan = require('metigan');
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY
});
// Send OTP with default template
async function sendVerificationCode(userEmail) {
const code = Math.floor(100000 + Math.random() * 900000).toString();
const result = await metigan.email.sendOtp({
to: userEmail,
from: 'security@myapp.com',
code: code,
appName: 'MyApp',
expiresInMinutes: 10
});
if (result.success) {
console.log('OTP sent! Tracking:', result.trackingId);
return { success: true, code }; // Store code for verification
}
return { success: false };
}
// Send OTP with custom template
async function sendBrandedOtp(userEmail) {
const code = Math.floor(100000 + Math.random() * 900000).toString();
const result = await metigan.email.sendOtp({
to: userEmail,
from: 'security@myapp.com',
code: code,
appName: 'MyApp',
expiresInMinutes: 10,
templateId: 'your-otp-template-id' // Custom branded template
});
return result;
}Transactional Email (Fast Lane)
Send time-sensitive transactional emails like password resets, order confirmations, etc.
transactional-example.jsJavaScript
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
30
31
32
33
34
35
36
37
38
39
40
41
const Metigan = require('metigan');
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY
});
// Password reset email
async function sendPasswordReset(userEmail, resetLink) {
const result = await metigan.email.sendTransactional({
to: userEmail,
from: 'security@myapp.com',
subject: 'Reset Your Password',
content: `
<h1>Password Reset</h1>
<p>Click the link below to reset your password:</p>
<a href="${resetLink}" style="padding: 12px 24px; background: #2563eb; color: white;
text-decoration: none; border-radius: 8px; display: inline-block;">
Reset Password
</a>
<p>This link expires in 1 hour.</p>
`
});
return result;
}
// Order confirmation email
async function sendOrderConfirmation(order) {
const result = await metigan.email.sendTransactional({
to: order.customerEmail,
from: 'orders@myshop.com',
subject: `Order Confirmed #${order.id}`,
content: `
<h1>Thank you for your order!</h1>
<p>Order #${order.id} has been confirmed.</p>
<p>Total: $${order.total.toFixed(2)}</p>
`
});
return result;
}Email with Template & Variables
Use pre-created templates with dynamic variable substitution.
template-example.jsJavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const Metigan = require('metigan');
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY
});
// Send email using a template with variables
async function sendWelcomeEmail(user) {
const result = await metigan.email.sendEmail({
from: 'Welcome Team <welcome@myapp.com>',
recipients: [user.email],
subject: `Welcome, ${user.firstName}!`,
templateId: 'welcome-template-id',
variables: {
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
dashboardUrl: 'https://myapp.com/dashboard'
}
});
return result;
}Express.js Integration
express-route.jsJavaScript
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express');
const Metigan = require('metigan');
const router = express.Router();
const metigan = new Metigan({
apiKey: process.env.METIGAN_API_KEY
});
// Send email endpoint
router.post('/send-email', async (req, res) => {
try {
const { to, subject, content } = req.body;
const result = await metigan.email.sendEmail({
from: 'noreply@example.com',
recipients: [to],
subject,
content
});
if (result.success) {
res.json({ success: true, message: 'Email sent successfully' });
} else {
res.status(400).json({ success: false, error: result.message });
}
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
// OTP endpoint
router.post('/send-otp', async (req, res) => {
try {
const { email } = req.body;
const code = Math.floor(100000 + Math.random() * 900000).toString();
const result = await metigan.email.sendOtp({
to: email,
from: 'security@myapp.com',
code,
appName: 'MyApp',
expiresInMinutes: 5
});
if (result.success) {
// Store code in session/cache for verification
res.json({ success: true, message: 'Verification code sent' });
} else {
res.status(400).json({ success: false, error: result.error });
}
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
module.exports = router;Environment Variables
Use dotenv package to load environment variables: require('dotenv').config()