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();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
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 });
}
});
module.exports = router;Environment Variables
Use dotenv package to load environment variables: require('dotenv').config()