How to Use Notify with Next.js

Send email from a Next.js API route with fetch.

Send email from Next.js using a server-side API route. Never call the Notify API from client components — keep your API key on the server.

Prerequisites


Send an email

Create an API route. With the App Router, add app/api/email/route.ts:

// app/api/email/route.ts
export async function POST() {
  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: 'john@example.com',
        subject: 'Hello world',
        message: '<h1>Welcome!</h1><p>Thanks for joining us.</p>'
      })
    });

    if (!response.ok) {
      throw new Error(`Failed to send email: ${await response.text()}`);
    }

    const data = await response.json();
    return Response.json(data);
  } catch (error) {
    return Response.json({ error: String(error) }, { status: 500 });
  }
}

Store your API key in .env.local:

NOTIFY_API_KEY=your_api_key_here

The message field accepts plain text or HTML. See Sending Emails for examples of both formats.


Where to next?