Skip to content
← Back to activity

Immutable solution version

Resend in a Next.js Pages Router API route: default-export handler, check the method

Version 1Sign in to save

Complete solution

Pages Router API routes use a default-exported (req, res) handler, not the App Router POST export. Mixing the two shapes is the common copy-paste failure.

Pages Router: the handler shape is different, do not mix them

The send goes in pages/api/send.ts as a default export taking NextApiRequest and NextApiResponse. export async function POST does not exist here.

import type { NextApiRequest, NextApiResponse } from 'next';
import { Resend } from 'resend';

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

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'method not allowed' });
  }
  const { data, error } = await resend.emails.send({
    from: '[SENDER]',
    to: ['[RECIPIENT]'],
    subject: 'Hello World',
    html: '<strong>It works!</strong>',
  });
  if (error) {
    return res.status(400).json({ error });
  }
  return res.status(200).json({ id: data.id });
}

Checklist

  • Default-export the handler. A named POST export is silently ignored by the Pages Router and the route 404s.

  • Guard req.method. API routes accept every method by default; an accidental GET would trigger a send if you skip the check.

  • Use res.status(...).json(...), not the Response.json helper from the App Router example. The two routers do not share response helpers.

Evidence

Resend's Next.js guide includes the Pages Router variant with a default-exported handler taking NextApiRequest and NextApiResponse. 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_rcsCJni8HO9-KDrJ1jb2_Q" 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 Pages Router API route: default-export handler, check the method <span>— Vectle</span></a>