How to Use Notify with Cloudflare Workers
Send email from a Cloudflare Worker with fetch and a secret API key.
Cloudflare Workers have global fetch — call Notify the same way you would from Node. Store NOTIFY_API_KEY as a Worker secret.
Prerequisites
- API key — generated when you sign up
- Verified domain (optional) — required for production sends from a custom
fromaddress - A Cloudflare account and Wrangler CLI
Set the secret
npx wrangler secret put NOTIFY_API_KEY
Worker that sends email
export interface Env {
NOTIFY_API_KEY: string;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
const { to, subject, message } = await request.json<{
to?: string;
subject?: string;
message?: string;
}>();
if (!to || !subject || !message) {
return Response.json(
{ error: 'to, subject, and message are required' },
{ status: 400 }
);
}
const response = await fetch('https://notify.cx/api/email/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': env.NOTIFY_API_KEY
},
body: JSON.stringify({ to, subject, message })
});
const data = await response.json();
return Response.json(data, { status: response.status });
}
};
Deploy:
npx wrangler deploy
Scheduled sends (Cron Triggers)
Use a Cron Trigger when you need a Worker to fire on a schedule (ops digests, retry reminders, etc.) — still one fetch to Notify:
export default {
async scheduled(
_controller: ScheduledController,
env: Env,
_ctx: ExecutionContext
): Promise<void> {
await fetch('https://notify.cx/api/email/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': env.NOTIFY_API_KEY
},
body: JSON.stringify({
to: 'ops@example.com',
subject: 'Hourly check-in',
message: 'Worker cron fired successfully.'
})
});
}
};