# Astro

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

Send your first email from an Astro app using the `mailblastr` Node SDK. Install it with `npm install mailblastr`, then import `MailBlastr` and call `mb.emails.send()`. Because Astro runs server code on demand, an [Astro Action](https://docs.astro.build/en/guides/actions/) is the natural place to make the call.

## Prerequisites

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.
- An [SSR adapter](https://docs.astro.build/en/guides/server-side-rendering/) installed so routes can run on demand.

## 1. Install the SDK

```bash
npm install mailblastr
```

## 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**

```ini
MAILBLASTR_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**

```ts
import { ActionError, defineAction } from 'astro:actions';
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr(import.meta.env.MAILBLASTR_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@example.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.

> **Note:** 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](https://www.mailblastr.com/docs/api/emails-send) reference.
