GuideNext.js

Send email from Next.js.

One API key, one small function, and your app sends receipts, sign-in links and notifications from your own domain. No package to install: it uses fetch.

01

Before you start

  • A MySendbox workspace on a plan. Sending by SMTP or the API needs one: see pricing. Each email uses 1 credit.
  • Your domain added under Domains, with its DNS records in place and shown as verified. Email can only be sent from an address on a verified domain.
  • An API key. In MySendbox, open API keys and create a key with only the Send email scope (emails:send). Limit it to your domain, then copy it: it's shown once and starts with msb_live_.

These steps use the App Router (the app folder).

02

Save the key

Add two lines to .env.local in your project's root folder: the key, and the address your emails come from, on your verified domain.

.env.local
MYSENDBOX_API_KEY=msb_live_…
MYSENDBOX_FROM="Acme <hello@your-domain.com>"

Keep this file out of Git, and never start these names with NEXT_PUBLIC_: that would put your key in the browser. See Next.js on environment variables. On your host (Vercel or another), add the same two variables in its settings.

03

Add the function

lib/email.tsTypeScript
// lib/email.ts — runs on the server only
export async function sendEmail(email: {
  to: string | string[];
  subject: string;
  html?: string;
  text?: string;
}) {
  const res = await fetch("https://api.mysendbox.com/v1/emails", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.MYSENDBOX_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ from: process.env.MYSENDBOX_FROM, ...email }),
  });
  const data = await res.json();
  // On an error, data.message (or data.error) says why: no plan, no credits, a domain that isn't verified.
  if (!res.ok) throw new Error(data.message ?? data.error);
  return data as { id: string };
}

Call sendEmail from code that runs on the server: a Route Handler or a Server Action. Don't call it from a Client Component, and don't let a visitor choose who the email goes to and what it says, or strangers can send email as you.

The fields are the same as in Send an email: cc, bcc, reply_to, attachments, scheduled_at and more. They use Resend's names, so an email object written for Resend works as it is.

04

Check that it works

Add a route that sends one email to you:

app/api/test-email/route.tsTypeScript
// app/api/test-email/route.ts — delete this file after the test
import { sendEmail } from "@/lib/email";

export async function GET() {
  const { id } = await sendEmail({
    to: "you@your-domain.com", // a real inbox of yours
    subject: "It works",
    text: "Sent from Next.js with MySendbox.",
  });
  return Response.json({ id });
}
  1. Run your app (npm run dev) and open http://localhost:3000/api/test-email in your browser.
  2. You see an ID such as { "id": "em_01k8…" }, and the email arrives.
  3. In MySendbox, open Emails to see it and its status.
  4. Delete the test route, so nobody else can open it.
05

If it doesn't send

The error thrown has the API's message. The common ones:

  • missing API key or invalid API key: the key isn't reaching the request. Check .env.local and restart npm run dev.
  • No plan or no credits: the message has a link to Billing.
  • The From domain isn't verified, or the key is limited to another domain: check Domains and the key's limits.
  • blocked_address: addresses on example.com and similar are never sent to. Use a real inbox.

All the codes are under Errors. To make sure a retry never sends twice, add an Idempotency-Key header.

06

Add the website chat

Optional. In MySendbox, open Chat widget, click Add a chat, add your site's address and copy the address inside the code it gives you (https://chat.mysendbox.com/w/chn_….js). Load it in your root layout:

app/layout.tsxTypeScript
// app/layout.tsx
import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
      <Script src="https://chat.mysendbox.com/w/chn_….js" />
    </html>
  );
}

This is how Next.js loads a script on every page. A round chat button shows at the bottom right of your site, and messages arrive under Chats.

The chat only shows on the websites listed for it in MySendbox, and the address must match exactly. Without a plan, those must be on a domain verified in your workspace, so it won't show on localhost: check it on your live site.

All setup guides · API reference