Skip to main content
This guide shows you how to integrate BosonMail into a Next.js App Router project. You will create a server-only client module, wire up a route handler for form submissions, add a server action for direct component calls, and expose a status endpoint so your UI can track delivery.
This guide assumes you are using Next.js 14 or later with the App Router. All BosonMail SDK calls must run on the server. Never expose your API key to the browser.

Prerequisites

  • A Next.js project with TypeScript
  • A BosonMail API key
  • Node.js 18 or later

Project setup

1

Install the SDK

Install the BosonMail SDK in your Next.js project.
2

Add environment variables

Create or update .env.local in your project root. Do not prefix the key with NEXT_PUBLIC_, because that would expose it to client bundles.
.env.local

Create a server-only client

Create a shared module that instantiates BosonMailClient once and exports it for use across server code.
lib/bosonmail.ts
You can optionally install the server-only package and add import "server-only" at the top of lib/bosonmail.ts. This causes Next.js to throw a build-time error if the module is ever imported into client code.

Route handler for sending email

Create a route handler that accepts a POST request, validates the body, sends the email, and returns a structured JSON response.
app/api/contact/route.ts
The runtime = "nodejs" export is explicit. The BosonMail SDK relies on web-standard globals available in Node.js 18+, but BosonWare has not verified edge-runtime compatibility, so pinning to the Node.js runtime avoids surprises.

Optional: validate with Zod

If you prefer schema validation, install Zod and replace the manual checks.
app/api/contact/route.ts

Server action for sending email

If you prefer server actions over route handlers, create a callable action that validates inputs and returns a typed result.
app/actions/send-email.ts

Route handler for email status

Expose a status endpoint so your frontend can poll or fetch delivery state. In recent Next.js versions, params is a Promise, so you must await it inside the handler.
app/api/emails/[id]/status/route.ts

Sending attachments via route handler

If your form accepts file uploads, use request.formData() and pass each file to the SDK as a Blob.
app/api/contact-with-attachment/route.ts
Do not accept unauthenticated file uploads in production. Add your own authentication, rate limiting, and file-type validation before passing files to BosonMail.

Deployment notes

  • Set BOSONMAIL_API_KEY in your hosting environment (Vercel project settings, or your deployment platform’s secret manager).
  • Do not commit .env.local to version control.
  • If you use the Node.js runtime explicitly, confirm your platform supports it. Vercel defaults to Node.js for App Router API routes, but the explicit export prevents accidental edge-runtime selection.

Error response mapping

Map SDK results to HTTP status codes so your frontend can react consistently.

Sending emails

SDK reference for sendEmail and its options.

Email status

How to check delivery status with getEmailStatus.

Production best practices

Key management, retries, logging, and monitoring guidance.

Attachments

SDK reference for sending file attachments.