SendHQ

Send email from Next.js

Connect a server-only receipt helper to a confirmed order in your Next.js App Router application.

Set up the Next.js server environment

Use the App Router with a Node.js runtime on Node.js 20 or newer. Configure MAILFORGE_BASE_URL, MAILFORGE_API_KEY and MAILFORGE_FROM in the server environment. Use the API origin for your installation and a key with the send scope. Keep all three out of NEXT_PUBLIC_ variables.

The sender must belong to a verified domain. In the SES sandbox, use a verified recipient or the SES mailbox simulator. Review the quickstart prerequisites.

Install the SDK and server-only marker

Use the release tarball from your operator, or build it from the SendHQ checkout:

npm ci --prefix sdk
npm pack ./sdk

From your Next.js project, install the tarball with the server-only marker. Replace the sample path with the local tarball path.

npm install /path/to/mailforge-0.1.0.tgz server-only

Write the receipt helper

Create lib/send-receipt.ts. The server-only import keeps this module out of Client Components. The receipt key ties repeated calls to one confirmed order.

import "server-only";
import { MailForge } from "mailforge";

export async function sendReceipt(orderId: string, recipient: string) {
  const from = process.env.MAILFORGE_FROM;
  if (!from) throw new Error("Set MAILFORGE_FROM.");
  return new MailForge().send({
    from,
    to: recipient,
    subject: "Order confirmed",
    text: "We received your order.",
  }, { idempotencyKey: `receipt-${orderId}` });
}

Call it after an order is confirmed

Inside your existing authenticated server-side order flow, load the confirmed order and recipient from your database. Pass those trusted values to the helper. This call-site example assumes your application has already loaded order.

import { sendReceipt } from "@/lib/send-receipt";

const email = await sendReceipt(order.id, order.customerEmail);
// Persist email.id with the order to look up its delivery later.

Keep the order ID and the receipt content stable when retrying within 24 hours. If a shopper changes their email address, treat a new receipt as a separate business event. A route that accepts arbitrary recipient addresses from the browser could let someone send through your account.

Follow the receipt after the request

A successful call returns a queued email ID. A read-scoped reporting service can call GET /api/emails/{id}. For an event-driven order workflow, register a public HTTPS receiver and match the webhook’s data.email_id to the saved order.

Verify and deduplicate the webhook or inspect delivery with the Node.js SDK.