Send email from Next.js

Send welcome emails, magic links, receipts and alerts from a Next.js Route Handler or Server Action with TrivSend's Node.js SDK. Your API key stays on the server, and every email is logged with its delivery result.

Start free · Next.js quickstart in the docs

Install and send

bash
npm install trivsend
# .env.local
TRIVSEND_API_KEY=ts_...
ts
// app/api/welcome/route.ts: runs on the server only
import { TrivSend } from "trivsend";

const trivsend = new TrivSend(process.env.TRIVSEND_API_KEY);

export async function POST(req: Request) {
  const { email } = await req.json();
  const { data, error } = await trivsend.emails.send(
    {
      from: "Acme <noreply@notify.example.com>",
      to: email,
      subject: "Welcome to Acme",
      html: "<p>Your account is ready.</p>",
    },
    { idempotencyKey: `welcome-${email}` },
  );
  if (error) return Response.json({ error: error.message }, { status: 502 });
  return Response.json({ id: data!.id });
}

The same call works in a Server Action. On Vercel, add TRIVSEND_API_KEY under Settings → Environment Variables.

Designed emails with React Email

Build the email as a React component and render it to HTML before sending; the developer guide shows how. The SDK has no dependencies and works on Node.js 18 and later.

Built for app email

Also see: Sending OTP emails from Node.js · OTP email