Loading...
Loading...
Bi-weekly engineering deep dives on auth, notifications, and developer infrastructure. No spam.
Sign Up for UpdatesPublished by Zyphr
Transactional emails are the messages your app sends in response to user actions: welcome emails, password resets, order confirmations, invoice receipts. They need to be reliable, fast, and well-formatted.
In this tutorial, you'll learn how to send transactional email from a Node.js application using Zyphr's SDK.
npm install @zyphr-dev/node-sdk
import Zyphr from '@zyphr-dev/node-sdk'; const zyphr = new Zyphr(process.env.ZYPHR_API_KEY);
Store your API key in an environment variable — never commit it to source control.
const { data } = await zyphr.emails.send({
from: 'hello@yourapp.com',
to: 'user@example.com',
subject: 'Welcome to YourApp!',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
});
console.log('Sent:', data.message_id);
That's it — one API call. Zyphr handles delivery via AWS SES, tracks opens and clicks, and manages bounces automatically.
Hardcoding HTML isn't sustainable. Create a template in the Zyphr dashboard, then reference it by ID:
const { data } = await zyphr.emails.send({
from: 'hello@yourapp.com',
to: 'user@example.com',
template: 'welcome-email',
variables: {
userName: 'Jane',
loginUrl: 'https://yourapp.com/login',
},
});
Templates use Handlebars syntax. You can use {{userName}} in your template HTML and it will be replaced with the variable value.
try {
const { data } = await zyphr.emails.send({
from: 'hello@yourapp.com',
to: 'user@example.com',
subject: 'Your order has shipped',
template: 'order-shipped',
variables: { trackingUrl, orderNumber },
});
console.log('Sent:', data.message_id);
} catch (error) {
if (error.status === 422) {
console.error('Invalid email address');
} else if (error.status === 429) {
console.error('Rate limited — retry later');
} else {
console.error('Send failed:', error.message);
}
}
Zyphr tracks delivery status automatically. You can check the status of any message:
const status = await zyphr.emails.get(messageId); console.log(status.data.state); // 'delivered', 'bounced', 'opened', etc.
Or subscribe to delivery webhooks to get real-time notifications.
import Zyphr from '@zyphr-dev/node-sdk';
const zyphr = new Zyphr(process.env.ZYPHR_API_KEY);
async function sendWelcomeEmail(user: { email: string; name: string }) {
const { data } = await zyphr.emails.send({
from: 'hello@yourapp.com',
to: user.email,
template: 'welcome-email',
variables: {
userName: user.name,
loginUrl: 'https://yourapp.com/login',
},
});
return data.message_id;
}
That's a production-ready transactional email setup in under 20 lines of code.