# RedwoodJS

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

Send your first email from a RedwoodJS app using the `senddart` Node SDK. Install it with `npm install senddart`, then import `SendDart` 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 SendDart [API key](https://www.senddart.com/docs/api-keys/overview).
- A [verified domain](https://www.senddart.com/docs/domains/managing) to send from.

## 1. Install the SDK

```bash
yarn workspace api add senddart
```

## 2. Add your API key

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

**.env**

```ini
SENDDART_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 { SendDart } from 'senddart';

const mb = new SendDart(process.env.SENDDART_API_KEY!);

export const handler = async (_event: APIGatewayEvent, _context: Context) => {
  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>',
  });

  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.senddart.com/docs/api/emails-send) reference.
