Prerequisites

To get the most out of this guide, you’ll need to:

1. Install

Install Resend for Node.js.

npm install resend

2. Install an SSR adapter

Because Astro builds a static site by default, install an SSR adapter to enable on-demand rendering of routes.

3. Add your API key

Create an API key in Resend and add it to your .env file to keep your API key secret.

.env
RESEND_API_KEY="re_123456789"

4. Send email using HTML

Create an Astro Action under actions/index.ts.

The easiest way to send an email is with the html parameter.

import { ActionError, defineAction } from 'astro:actions';
import { Resend } from 'resend';

const resend = new Resend(import.meta.env.RESEND_API_KEY);

export const server = {
  send: defineAction({
    accept: 'form',
    handler: async () => {
      const { data, error } = await resend.emails.send({
        from: 'Acme <onboarding@resend.dev>',
        to: ['delivered@resend.dev'],
        subject: 'Hello world',
        html: '<strong>It works!</strong>',
      });

      if (error) {
        throw new ActionError({
          code: 'BAD_REQUEST',
          message: error.message,
        });
      }

      return data;
    },
  }),
};

Call the send action from any frontmatter route, script, or component.

5. Try it yourself

Astro Example

See the full source code.