How to Use Notify with Next.js

Send your first email with Notify in Next.js 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 NextJS app and lets you send emails from it.
  • 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 with us so we can take the training wheels off (and get you far 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

Create an API route for sending emails. If using the App Router, create app/api/email/route.ts.

// app/api/email/route.ts
import Notify from 'notifycx';

const notify = new Notify(process.env.NOTIFY_API_KEY);

export async function POST() {
  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) {
      return Response.json({ error }, { status: 500 });
    }

    return Response.json(data);
  } catch (error) {
    return Response.json({ error }, { status: 500 });
  }
}

The message field can contain either plain text or HTML, depending on your needs. If you provide plain text, the email will be sent as a simple text email. If you include HTML, it will be rendered as rich text in email clients.

If you want more control over how your emails appear, using HTML allows for styling, links, and images. Which brings us to...


3. Sending Template-Based Emails

We cannot stress how much you should skip writing HTML. Use Notify templates instead to send emails faster. Notify offers an intuitive template builder with pre-made and fully customizable templates that support dynamic variables {like_this} for personalization.

When you're done designing a template, just grab its ID from here, and do this in your app:

await notify.sendEmailFromTemplate({
  to: 'recipient@example.com',
  from: 'noreply@notify.cx',
  templateId: '<your_template_id>',
  variables: {
    name: 'John Doe',
    company: 'Example Inc.',
  },
});

Both sendEmail and sendEmailFromTemplate throw errors if the request fails. Always wrap them in try/catch.


Where To Next?

Now you’re ready to send emails in your Next.js app with Notify! 🚀