How to Use Notify with Node.js
Send your first email with Notify in a Node.js app in minutes.
Prerequisites
Before you start, make sure you have:
- Your API key – We generated one for you when you signed up. This authenticates your Node.js app and lets you send emails.
- A verified domain (optional) – Not required for testing. Trial accounts can send up to 100 emails total (10/hr limit). To scale beyond that, verify your domain for better deliverability.
1. Install the SDK
Get the Notify Node.js SDK:
# npm
npm install notifycx
# yarn
yarn add notifycx
# pnpm
pnpm add notifycx
2. Send an Email
Set up a simple Node.js script to send an email:
import Notify from 'notifycx';
import dotenv from 'dotenv';
dotenv.config();
const notify = new Notify(process.env.NOTIFY_API_KEY);
async function sendEmail() {
try {
const { data, error } = await notify.sendEmail({
to: 'recipient@example.com',
subject: 'Hello world',
name: 'John Doe',
message: 'Your email content here', // Plain text or HTML
});
if (error) {
console.error('Error sending email:', error);
return;
}
console.log('Email sent successfully:', data);
} catch (error) {
console.error('Failed to send email:', error);
}
}
sendEmail();
Place this in a file like send-email.js
and run it with:
node send-email.js
The message
field can be plain text or HTML, depending on your needs. If you need styling, links, or images, use HTML.
3. Sending Template-Based Emails
At Notify, we recommend you don't hand-code HTML for your emails — use Notify templates instead. Notify offers an intuitive template builder with pre-made and fully customizable templates. These support dynamic variables for personalization.
Once you’ve created a template, grab its ID from here and use it like this:
async function sendTemplatedEmail() {
try {
const { data, error } = await notify.sendEmailFromTemplate({
to: 'recipient@example.com',
from: 'noreply@notify.cx',
templateId: '<your_template_id>',
variables: {
name: 'John Doe',
company: 'Example Inc.',
},
});
if (error) {
console.error('Error sending template email:', error);
return;
}
console.log('Template email sent successfully:', data);
} catch (error) {
console.error('Failed to send template email:', error);
}
}
sendTemplatedEmail();
Both sendEmail
and sendEmailFromTemplate
throw errors if the request fails. Always wrap them in try/catch
.
Where To Next?
- Dive into the NotifyCX docs for advanced features.
- Explore analytics, logs, and transactional email best practices.
- Implement retries for better reliability.
Now you’re ready to send emails in your Node.js app with Notify! 🚀