
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
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
- Node.js
- Better Auth
- Resend account and API key
- Install the plugin (from npm or local path):
pnpm add resend-audience-sync-better-auth # or npm install resend-audience-sync-better-auth
- Add your Resend API key and Audience ID to your environment:
RESEND_API_KEY=your_resend_api_key RESEND_AUDIENCE_ID=your_audience_id
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)}
/>
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 recognizemarketingOptIn
as a valid property. This is safe as long as your backend/plugin expects and handles the field.
- Add support for custom fields/tags
- Add logging and error reporting
- Add more flexible path matching
See the open issues for a full list.
Contributions are welcome! Please fork the repo and open a pull request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
PinkyCodeMaster - @PinkyCodeMaster
Project Link: https://github.com/PinkyCodeMaster/resend-audience-sync-better-auth