# Cloudflare Workers

> Send your first email from a Cloudflare Worker using the mailblastr SDK.

Send your first email from a [Cloudflare Worker](https://developers.cloudflare.com/workers/) using the `mailblastr` SDK. The SDK uses the standard `fetch` API and runs without modification on the Workers edge runtime — install it with `npm install mailblastr`.

## 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.
- A Cloudflare Worker — recommended to bootstrap with `npm create cloudflare`.

## 1. Install the SDK

```bash
npm install mailblastr
```

## 2. Store your API key as a secret

Add your API key as an encrypted Worker secret so it is never committed to source.

```bash
npx wrangler secret put MAILBLASTR_API_KEY
# paste mb_xxxxxxxxx when prompted
```

## 3. Send the email from your Worker

In your Worker's `fetch` handler, read the key from `env`, instantiate the SDK client, and call `mb.emails.send()`.

**src/index.ts**

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

export default {
  async fetch(request, env, ctx): Promise<Response> {
    const mb = new MailBlastr(env.MAILBLASTR_API_KEY);
    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,
    });
  },
} satisfies ExportedHandler<{ MAILBLASTR_API_KEY: string }>;
```

## 4. Deploy and send email

Run `wrangler deploy` and wait for it to finish. It will give you a URL like `https://my-worker.your-name.workers.dev` that you can open to verify the email was sent.

> **Note:** See the full set of body fields in the [Send an email](https://www.mailblastr.com/docs/api/emails-send) reference.
