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 instantiatesBosonMailClient once and exports it for use across server code.
lib/bosonmail.ts
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, userequest.formData() and pass each file to the SDK as a Blob.
app/api/contact-with-attachment/route.ts
Deployment notes
- Set
BOSONMAIL_API_KEYin your hosting environment (Vercel project settings, or your deployment platform’s secret manager). - Do not commit
.env.localto 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.Related
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.