# RedwoodJS

> Send your first email from a RedwoodJS serverless function using the mailblastr SDK.

Send your first email from a RedwoodJS app using the `mailblastr` Node SDK. Install it with `npm install mailblastr`, then import `MailBlastr` and call `mb.emails.send()`. Redwood serverless functions live in the `api` side, so the call (and your key) stay on the server.

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

## 1. Install the SDK

```bash
yarn workspace api add mailblastr
```

## 2. Add your API key

Add your key to the project `.env`. Redwood loads it into the `api` side automatically.

**.env**

```ini
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Generate a function

Scaffold a serverless function on the `api` side.

```bash
yarn rw g function send
```

## 4. Send email from the function

Edit the generated handler to use the SDK.

**api/src/functions/send/send.ts**

```ts
import type { APIGatewayEvent, Context } from 'aws-lambda';
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr(process.env.MAILBLASTR_API_KEY);

export const handler = async (_event: APIGatewayEvent, _context: Context) => {
  const { data, error } = await mb.emails.send({
    from: 'Acme <onboarding@yourdomain.com>',
    to: ['delivered@example.com'],
    subject: 'hello world',
    html: '<strong>it works!</strong>',
  });

  return {
    statusCode: error ? 500 : 200,
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(error ? { error } : { data }),
  };
};
```

> **Note:** A successful call returns the created email's `id`. See the full request body in the [Send an email](https://www.mailblastr.com/docs/api/emails-send) reference.
