# AWS Lambda

> Send your first email from an AWS Lambda function using the mailblastr SDK.

Send your first email from an [AWS Lambda](https://aws.amazon.com/lambda/) function using the `mailblastr` Node SDK. Bundle the SDK into your Lambda deployment package with your preferred bundler (esbuild, webpack, etc.) or a Lambda Layer.

## Prerequisites

Before you start, you will need:

- 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
npm install mailblastr
```

## 2. Create an AWS Lambda function

Go to [aws.amazon.com](https://aws.amazon.com) and create a new Lambda function using the **Node.js 20.x** (or later) runtime.

## 3. Store your API key

Under the function's **Configuration → Environment variables**, add `MAILBLASTR_API_KEY` with your key. Reading it from the environment keeps the secret out of source.

**Environment variable**

```sh
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 4. Edit the handler function

Use the SDK client in your handler. It returns `{ data, error }` — no try/catch needed for HTTP errors.

**index.mjs**

```js
import { MailBlastr } from 'mailblastr';

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

export const handler = async (event) => {
  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,
    body: JSON.stringify(error ? { error } : { data }),
  };
};
```

## 5. Deploy and send email

Click **Deploy**, then **Test** at the top of the screen. A successful run returns the created email's `id`.

> **Note:** See the full request body — `cc`, `bcc`, `reply_to`, `attachments`, `tags`, `scheduled_at` — in the [Send an email](https://www.mailblastr.com/docs/api/emails-send) reference.
