How to Use Notify with Express
Send email from an Express.js route with fetch and x-api-key.
Send transactional email from Express using Node’s built-in fetch (Node 18+). Keep your API key on the server — never expose it to the browser.
Prerequisites
- API key — generated when you sign up
- Verified domain (optional) — required for production sends from a custom
fromaddress - Express 4+ and Node.js 18+
Install and configure
npm install express
Store the key in your environment:
NOTIFY_API_KEY=your_api_key_here
Send an email from a route
import express from 'express';
const app = express();
app.use(express.json());
app.post('/api/send-welcome', async (req, res) => {
const { to, name } = req.body;
if (!to) {
return res.status(400).json({ error: 'to is required' });
}
try {
const response = await fetch('https://notify.cx/api/email/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.NOTIFY_API_KEY
},
body: JSON.stringify({
to,
subject: 'Welcome aboard',
message: `<h1>Hi ${name || 'there'}!</h1><p>Thanks for signing up.</p>`
})
});
const data = await response.json();
if (!response.ok) {
return res.status(response.status).json(data);
}
return res.json(data);
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Failed to send email' });
}
});
app.listen(3000);
Successful response from Notify:
{
"success": true,
"data": {
"messageId": "01000194b1dd1c04-b51e7343-6808-4a68-b2af-845feae57f8b-000000"
}
}
The message field accepts plain text or HTML. See Sending Emails.
Helper you can reuse
export async function sendNotifyEmail({ to, subject, message, from }) {
const response = await fetch('https://notify.cx/api/email/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.NOTIFY_API_KEY
},
body: JSON.stringify({ to, subject, message, ...(from ? { from } : {}) })
});
const data = await response.json();
if (!response.ok) {
const err = new Error(data.error || 'Notify send failed');
err.status = response.status;
err.data = data;
throw err;
}
return data;
}