Blog

/

Published Thursday January 23, 2025

Send Emails with Resend StarbaseDB Plugin

Brayden Wilmoth

Brayden Wilmoth

@BraydenWilmoth
Send Emails with Resend StarbaseDB Plugin

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:

  1. Sign up for a Resend account

  2. Obtain your API key from the Resend dashboard

  3. 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:

  1. Resend Plugin – COnfigured with your API key to handle email sending functionality.

  2. CDC Plugin – Monitors the orders table for INSERT events.

  3. 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:

  1. In Cloudflare go to your “Account Home” and click your domain

  2. On the right hand side click on “DNS Records”

  3. In Resend click on “+ Add Domain”

  4. Add the MX and two TXT entries provided by Resend to your Cloudflare DNS settings

  5. DMARC 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.

Space, at your fingertips
astronaut

What will you discover?