Skip to content

PinkyCodeMaster/resend-audience-sync-better-auth

Repository files navigation

Contributors Forks Stargazers Issues License LinkedIn


Logo

Resend Audience Sync Better Auth Plugin

Auto-sync users' marketing preferences with a Resend audience during sign-up or profile update in Better Auth.
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Built With
  3. Getting Started
  4. Usage
  5. Roadmap
  6. Contributing
  7. License
  8. Contact
  9. Acknowledgments

About The Project

Product Name Screen Shot

Resend Audience Sync Better Auth Plugin automatically adds or unsubscribes users from a Resend audience based on their marketing opt-in status during sign-up or profile update in Better Auth.

  • Adds users to a specified Resend audience if they opt in to marketing
  • Unsubscribes users if they opt out
  • Optionally updates contact info if preferences change
  • Uses the Resend Node SDK
  • Audience ID and API key are configurable

(back to top)

Built With

(back to top)

Getting Started

Prerequisites

  • Node.js
  • Better Auth
  • Resend account and API key

Installation

  1. Install the plugin (from npm or local path):
    pnpm add resend-audience-sync-better-auth
    # or
    npm install resend-audience-sync-better-auth
  2. Add your Resend API key and Audience ID to your environment:
    RESEND_API_KEY=your_resend_api_key
    RESEND_AUDIENCE_ID=your_audience_id
    

(back to top)

Usage

Add the plugin to your Better Auth config:

import { resendAudienceSync } from "resend-audience-sync-better-auth";

betterAuth({
  // ...other config
  plugins: [
    resendAudienceSync({
      apiKey: process.env.RESEND_API_KEY!,
      audienceId: process.env.RESEND_AUDIENCE_ID!,
      paths: ["/sign-up/email", "/profile/update"],
      getUserInfo: (ctx) => ({
        email: ctx.body.email,
        firstName: ctx.body.firstName,
        lastName: ctx.body.lastName,
        marketingOptIn: ctx.body.marketingOptIn,
      }),
    }),
  ],
});

Frontend:
Add a checkbox to your sign-up/profile form and send marketingOptIn in the payload.

<input
  type="checkbox"
  checked={marketingOptIn}
  onChange={e => setMarketingOptIn(e.target.checked)}
/>

Frontend Integration Note

Because the Better Auth client types do not include custom fields like marketingOptIn by default, you must use a type assertion when sending this field in your sign-up payload. This ensures the value is sent to the backend and picked up by the Resend Audience Sync plugin.

Example:

async function onSubmit(e: React.FormEvent) {
  e.preventDefault();
  setError(null);
  // Pass marketingOptIn to the plugin endpoint after signup
  const res = await authClient.signUp.email({
    email,
    password,
    name,
    marketingOptIn,
  } as any);
  if (res.error) return setError(res.error.message || "Signup failed");
}

Note: The as any is required because the Better Auth client does not recognize marketingOptIn as a valid property. This is safe as long as your backend/plugin expects and handles the field.

(back to top)

Roadmap

  • Add support for custom fields/tags
  • Add logging and error reporting
  • Add more flexible path matching

See the open issues for a full list.

(back to top)

Contributing

Contributions are welcome! Please fork the repo and open a pull request.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

PinkyCodeMaster - @PinkyCodeMaster

Project Link: https://github.com/PinkyCodeMaster/resend-audience-sync-better-auth

(back to top)

Acknowledgments

(back to top)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published