Setting up AutoSend as the email provider in NextAuth / Auth.js for magic links

We’re using NextAuth.js (Auth.js) for authentication in a Next.js App Router project and want to use AutoSend to deliver passwordless magic link sign-in emails instead of standard SMTP or other providers.

What’s the cleanest way to configure the sendVerificationRequest callback with AutoSend’s REST API, and are there any specific payload or suppression details to be aware of?

To use AutoSend as your magic link provider in NextAuth (Auth.js), the recommended approach is configuring the EmailProvider with a custom sendVerificationRequest handler calling AutoSend’s REST API (POST https://api.autosend.com/v1/mails/send).


1. Environment Configuration

Add your AutoSend Project API key and verified sender address to .env.local:

AUTOSEND_API_KEY=AS_your_project_api_key
EMAIL_FROM=auth@yourdomain.com

Note: Ensure your sending domain has completed SPF and DKIM verification in your AutoSend dashboard before sending.


2. NextAuth Configuration

Auth.js / NextAuth v5 (auth.ts or app/api/auth/[...nextauth]/route.ts)

import NextAuth from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { PrismaAdapter } from "@auth/prisma-adapter";
import prisma from "@/lib/prisma";

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    EmailProvider({
      id: "autosend",
      name: "AutoSend",
      from: process.env.EMAIL_FROM,
      sendVerificationRequest: async ({ identifier: email, url, provider }) => {
        const host = new URL(url).host;

        const response = await fetch("https://api.autosend.com/v1/mails/send", {
          method: "POST",
          headers: {
            "Authorization": `Bearer ${process.env.AUTOSEND_API_KEY}`,
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            from: provider.from,
            to: email,
            subject: `Sign in to ${host}`,
            html: `
              <div style="font-family: sans-serif; max-width: 540px; margin: 0 auto; padding: 24px;">
                <h2>Sign in to ${host}</h2>
                <p>Click the button below to sign in to your account. This link will expire shortly.</p>
                <div style="margin: 24px 0;">
                  <a href="${url}" style="background-color: #000; color: #fff; padding: 12px 24px; border-radius: 6px; text-decoration: none; display: inline-block;">
                    Sign in to ${host}
                  </a>
                </div>
                <p style="color: #666; font-size: 13px;">If you didn't request this email, you can safely ignore it.</p>
              </div>
            `,
            text: `Sign in to ${host}\n\nClick the link below to sign in:\n${url}\n\nIf you didn't request this email, you can safely ignore it.`,
          }),
        });

        if (!response.ok) {
          const errorText = await response.text();
          throw new Error(`AutoSend API failed to send verification email (${response.status}): ${errorText}`);
        }
      },
    }),
  ],
});

3. Key Implementation Details

  1. Suppression Management: AutoSend manages suppression lists and bounce hygiene automatically on the backend. Do not pass manual groupId or suppressionGroupId fields when sending transactional verification emails via POST /v1/mails/send.
  2. API Key Scope: Use a Project-scoped API Key (AS_...). If using an Account-level key (ASA_...), remember to include the x-project-id: <your_project_id> header in the request.
  3. Alternative SMTP Transport: If your stack requires standard SMTP instead of HTTP fetch, you can point NextAuth’s server configuration to AutoSend’s SMTP host (smtp.autosend.com:587) using your API key as the password.

Relevant Documentation