Quick setup examples

Astro

Send your first email from an Astro Action using the senddart SDK.

Send your first email from an Astro app using the senddart Node SDK. Install it with npm install senddart, then import SendDart and call mb.emails.send(). Because Astro runs server code on demand, an Astro Action is the natural place to make the call.

Prerequisites

1. Install the SDK

npm install senddart

2. Add your API key

Add your key to a .env file at the project root. Astro exposes it server-side via import.meta.env.

.env
SENDDART_API_KEY=mb_xxxxxxxxx

3. Send email from an Action

Create an Astro Action under src/actions/index.ts. Instantiate the client with the key from import.meta.env, call mb.emails.send(), and surface any error as an ActionError.

src/actions/index.ts
import { ActionError, defineAction } from 'astro:actions';
import { SendDart } from 'senddart';

const mb = new SendDart(import.meta.env.SENDDART_API_KEY);

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

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

      return data;
    },
  }),
};

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

A successful call returns the created email's id. See every supported field — cc, bcc, reply_to, attachments, scheduled_at — in the Send an email reference.