Automating emails based on database events can save significant time and effort in building responsive applications. Whether it's notifying users, sending welcome emails, or alerting stakeholders, StarbaseDB's Resend Plugin makes integrating email functionality straightforward.
In this guide, we’ll walk through how to set up the Resend StarbaseDB Plugin to send emails in response to database mutations. With just a few steps, you'll have a seamless email-sending workflow up and running.
Step 1: Create Resend Account
To get started, you’ll need an account with Resend. Resend is an API for sending transactional emails with ease.
Steps:
Sign up for a Resend account
Obtain your API key from the Resend dashboard
Store the API key securely, we’ll need it to configure our Resend Plugin
Step 2: Add Email Logic
Now that you have your Resend account, it’s time to integrate email-sending logic into your StarbaseDB instance.
Example Code
The following code demonstrates how to set up the Resend Plugin alongside the Change Data Capture (CDC) Plugin to send emails when a new order is inserted into the database:
const resendPlugin = new ResendPlugin({ apiKey: 'YOUR_RESEND_API_KEY' });
const cdcPlugin = new ChangeDataCapturePlugin({
stub,
broadcastAllEvents: false,
events: [{
action: 'INSERT',
schema: 'main',
table: 'orders',
}]
});
cdcPlugin.onEvent(async (payload) => {
if (payload.action === 'INSERT' && payload.table === 'orders') {
const recipient = payload.data.email
const orderId = payload.data.id
await resendPlugin.sendEmail(
'Acme <onboarding@resend.dev>',
[recipient],
`Order Confirmation #${orderId}`,
`<p><strong>Thank you!</strong> We have received your order.</p>`
);
}
}, ctx);
How It Works:
Resend Plugin – COnfigured with your API key to handle email sending functionality.
CDC Plugin – Monitors the
orders
table forINSERT
events.Event Listener – When a new order is inserted, the
onEvent
callback sends an email using the Resend Plugin.
This setup helps make it easier to ensure that emails are automatically sent in response to specific database mutations.
Step 3: Setup DNS
To improve email deliverability, Resend requires you to set up DNS records for your domain. This ensures that your emails aren’t flagged as spam.
Steps:
In Cloudflare go to your “Account Home” and click your domain
On the right hand side click on “DNS Records”
In Resend click on “+ Add Domain”
Add the
MX
and twoTXT
entries provided by Resend to your Cloudflare DNS settingsDMARC is optional, but recommended
With DNS properly configured, your emails will have higher deliverability rates and better reliability.
Conclusion
The Resend StarbaseDB Plugin streamlines the process of sending emails triggered by database events. By combining the power of StarbaseDB’s CDC Plugin with Resend’s email API, you can create real-time, automated workflows with minimal effort.
Additional Use Cases
Welcome Emails – Send greeting emails when new users register
Order Confirmations – Notify customers when their orders are placed
Alerts – Send critical updates to stakeholders when events happen
Ready to integrate? Start using the Resend StarbaseDB Plugin today and experience the simplicity of automated email workflows. For more details about the code contributions involved you can reference the introduction pull request.