# Supabase Edge Functions

> Send your first email from a Supabase Edge Function by calling the MailBlastr API with fetch.

Send your first email from a [Supabase Edge Function](https://supabase.com/docs/guides/functions) by calling the MailBlastr REST API. The `mailblastr` SDK targets Node.js/npm — Edge Functions run on Deno, which has a global `fetch`, so you POST JSON to `https://api.mailblastr.com/emails` directly.

## 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.
- The [Supabase CLI](https://supabase.com/docs/guides/cli#installation) installed.

## 1. Create a Supabase function

Create a new function locally.

```bash
supabase functions new mailblastr
```

## 2. Set your API key as a secret

Store your key as a function secret so it is read from the environment, not hardcoded.

```bash
supabase secrets set MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Edit the handler function

Paste the following code into the `index.ts` file.

**index.ts**

```ts
const MAILBLASTR_API_KEY = Deno.env.get('MAILBLASTR_API_KEY');

const handler = async (_request: Request): Promise<Response> => {
  const res = await fetch('https://api.mailblastr.com/emails', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${MAILBLASTR_API_KEY}`,
    },
    body: JSON.stringify({
      from: 'Acme <onboarding@yourdomain.com>',
      to: ['delivered@example.com'],
      subject: 'hello world',
      html: '<strong>it works!</strong>',
    }),
  });

  const data = await res.json();

  return new Response(JSON.stringify(data), {
    status: res.ok ? 200 : res.status,
    headers: { 'Content-Type': 'application/json' },
  });
};

Deno.serve(handler);
```

## 4. Deploy and send email

Run the function locally, then deploy it to Supabase and open the endpoint URL to send an email.

```bash
supabase functions serve mailblastr --no-verify-jwt
supabase functions deploy mailblastr
```

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