# Bun

> Send your first email from a Bun server using the mailblastr SDK.

Send your first email from a Bun app using the `mailblastr` SDK. Bun reads `.env` automatically and is fully compatible with the Node SDK. Install it with `bun add mailblastr`, then import `MailBlastr` and call `mb.emails.send()`.

## 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
bun add mailblastr
```

## 2. Add your API key

Bun loads `.env` automatically and exposes it on `process.env` / `Bun.env`.

**.env**

```ini
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Send email from a Bun server

Create `index.ts`, instantiate the SDK client, and call `mb.emails.send()` from the request handler. Start it with `bun index.ts`.

**index.ts**

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

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

const server = Bun.serve({
  port: 3000,
  async fetch() {
    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 Response.json(error ? { error } : { data }, {
      status: error ? 500 : 200,
    });
  },
});

console.log(`Listening on http://localhost:${server.port} ...`);
```

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