Skip to content
DocumentationAPI referenceBuild something worth sending.

Guides

Send Auth.js emails with SendPanda

Send Auth.js magic links from a custom email provider. The built-in Resend provider posts to a hardcoded api.resend.com URL.

Auth.js can send its magic-link email through any function you provide. The built-in Resend provider posts to a hardcoded https://api.resend.com/emails and does not read RESEND_BASE_URL, so setting that variable does not move those emails to SendPanda. Write a small email provider instead.

1. Install

npm install @sendpanda/sdk

Create a Send emails key and store it as SENDPANDA_API_KEY. The from address has to be on a domain verified in that project. See domain setup. A queued status means the API accepted the email, not that the recipient's server delivered it.

2. Provide sendVerificationRequest

import NextAuth from "next-auth";
import type { EmailConfig } from "next-auth/providers";
import { SendPanda } from "@sendpanda/sdk";

const panda = new SendPanda(process.env.SENDPANDA_API_KEY!);

const sendpanda: EmailConfig = {
  id: "sendpanda",
  type: "email",
  name: "Email",
  from: "Your app <hello@your-verified-domain.com>",
  maxAge: 24 * 60 * 60,
  async sendVerificationRequest({ identifier, url, provider }) {
    await panda.emails.send(
      {
        from: provider.from,
        to: [identifier],
        subject: "Sign in",
        text: `Sign in: ${url}`,
        html: `<p><a href="${url}">Sign in</a></p>`,
      },
      { idempotencyKey: `auth-${identifier}-${url}` },
    );
  },
  options: {},
};

export const { handlers, auth } = NextAuth({
  providers: [sendpanda],
});

identifier is the address Auth.js is signing in, and url is the link it generated. Sending the link is the point of this provider, so this is not an open relay in the way a public form route is. The idempotency key includes the link so a retried request for the same link does not send a second email. A new sign-in link is a new send.

If you would rather keep the resend package for other mail, the migration guide covers RESEND_BASE_URL. It does not apply to this built-in provider, because that provider never reads the variable.

↑↓ to navigate↵ to openesc to close