# Vercel Functions

> Send your first email from a Vercel Function using the mailblastr SDK.

Send your first email from a [Vercel Function](https://vercel.com/docs/functions) using the `mailblastr` Node SDK. The SDK works in both the Node.js and Edge runtimes. 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.
- The [Vercel CLI](https://vercel.com/docs/cli#installing-vercel-cli) installed.

## 1. Install the SDK

```bash
npm install mailblastr
```

## 2. Set up environment variables

Add your API key to `.env.local` for local development. Never hardcode the key in source.

**.env.local**

```sh
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Create the function

Create a route file under `app/api/send/route.ts` if you are using the Next.js [App Router](https://nextjs.org/docs/app/building-your-application/routing/route-handlers).

**app/api/send/route.ts**

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

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

export async function POST() {
  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,
  });
}
```

## 4. Send email locally

Run the function locally and open the endpoint URL to send an email at `http://localhost:3000/api/send`.

```bash
npm run dev
```

## 5. Send email in production

Deploy with the Vercel CLI, then add your `MAILBLASTR_API_KEY` environment variable in your Vercel project settings. Open `https://your-project.vercel.app/api/send` to send an email.

```bash
vercel
```

> **Warning:** Only call the MailBlastr API from server-side code (Route Handlers, Server Actions, API routes). Never ship your API key to the browser.

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