Send an email with Next.js (Route Handler)

Send emails in the Next.js App Router with a server Route Handler using the Publiq SDK.

The Route Handler runs on the server — the safe place for the API key. Install publiq and set PUBLIQ_API_KEY.

import { Publiq } from 'publiq';
import { NextResponse } from 'next/server';

const publiq = new Publiq(process.env.PUBLIQ_API_KEY);

export async function POST(req: Request) {
  const { to } = await req.json();
  const email = await publiq.emails.send({
    from: 'you@yourdomain.com',
    to,
    subject: 'Welcome to Acme',
    templateKey: 'welcome-email',
    variables: { first_name: 'Ana' },
  });
  return NextResponse.json({ id: email.id });
}

Best practices

  • Always call Publiq from the server — never expose the API key on the client.
  • Create the client instance once and reuse it across requests.
  • Prefer templateKey over inline HTML to keep content versioned.
  • Handle PubliqError (status/code); the SDK already retries transient errors with backoff.
  • Send from a verified domain — see Domains.

See also

Send an email with Next.js (Route Handler) — Publiq Docs