Better Auth

Send Better Auth transactional email through Samva with the @samva/better-auth package.

Use @samva/better-auth to send Better Auth email callbacks through Samva. The package provides callback fragments for manual wiring and a withSamva() helper for the common setup.

Better Auth owns authentication. The integration has no from option: Samva sends the rendered email from your organization's default sender.

Install

bun add better-auth samva @samva/better-auth

Wire the common callbacks

import { betterAuth } from "better-auth";
import { withSamva } from "@samva/better-auth";

export const auth = betterAuth(
  withSamva(
    {
      emailAndPassword: {
        enabled: true,
      },
    },
    {
      apiKey: process.env.SAMVA_API_KEY!,
      plugins: {
        emailOTP: true,
        magicLink: true,
      },
    },
  ),
);

withSamva() fills missing email verification and password reset callbacks. It also fills change-email and delete-account callbacks when those Better Auth user flows are already enabled. Existing callbacks are preserved.

Manual wiring

Use samvaEmail() when you want to control each Better Auth option yourself:

import { betterAuth } from "better-auth";
import { emailOTP, magicLink } from "better-auth/plugins";
import { samvaEmail } from "@samva/better-auth";

const samva = samvaEmail({ apiKey: process.env.SAMVA_API_KEY! });

export const auth = betterAuth({
  emailVerification: samva.emailVerification,
  emailAndPassword: {
    enabled: true,
    sendResetPassword: samva.emailAndPassword.sendResetPassword,
  },
  plugins: [
    emailOTP(samva.plugins.emailOTP),
    magicLink(samva.plugins.magicLink),
  ],
});

Covered triggers

The package includes defaults for:

  • email verification
  • password reset
  • change-email confirmation
  • delete-account confirmation
  • email OTP
  • two-factor OTP
  • magic links
  • organization invitations

Override any template with a function returning HTML, { subject, html, text }, or a React Email element. React Email rendering is optional and uses @react-email/render when your template returns an element.

Cookbook and example

For the callback-fragment wiring, the full list of default templates, and template overrides with { subject, html, text } or a React Email element, see the Better Auth cookbook. The Next.js example is a runnable project with both wiring styles.

Related resources

Related documentation

On this page