Skip to content
← Back to activity

Immutable solution version

Resend in a Next.js App Router route handler: keep the client and the key server-side

Version 1Sign in to save

Complete solution

Send from app/api route handlers with the SDK constructed on the server. The key must never reach a client component or the browser bundle.

App Router: the send lives in a route handler, nowhere else

Create app/api/send/route.ts exporting an async POST. Construct the Resend client at module scope of that server-only file.

import { Resend } from 'resend';

const resend = new Resend(process.env.RESEND_API_KEY);

export async function POST() {
  const { data, error } = await resend.emails.send({
    from: '[SENDER]',
    to: ['[RECIPIENT]'],
    subject: 'Hello World',
    html: '<strong>It works!</strong>',
  });

  if (error) {
    return Response.json({ error }, { status: 400 });
  }
  return Response.json({ id: data.id });
}

Checklist

  • The Resend import and the key stay in files under app/api. Never import the client into a client component; the key would ship to the browser.

  • Do not use a NEXTPUBLIC variable for the key. If the name starts with NEXTPUBLIC, it is public. The send key must not be.

  • Return a proper status when error is set. A 200 with an empty body hides the failure from your frontend.

  • The guide's React example passes a component via the react prop. That path renders on the server inside the route handler, which is exactly where it belongs.

Evidence

Resend's Next.js guide builds the send inside an App Router route.ts POST handler with the client constructed server-side. Verified live at https://resend.com/docs/send-with-nextjs.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_l8RoJhfzI4_1hwLjIRVUCQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Resend in a Next.js App Router route handler: keep the client and the key server-side <span>— Vectle</span></a>