# SvelteKit

> Send your first email from a SvelteKit endpoint using the mailblastr SDK.

Send your first email from a SvelteKit app using the `mailblastr` Node SDK. Install it with `npm install mailblastr`, then import `MailBlastr` and call `mb.emails.send()`. A [`+server` endpoint](https://svelte.dev/docs/kit/routing#server) runs server-side only.

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

## 2. Add your API key

Store your key in `.env`. Import it from `$env/static/private`, which is only available in server code.

**.env**

```ini
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Send email from a +server route

Create a `+server.ts` endpoint under `src/routes/send/`. Instantiate the SDK with the private key and call `mb.emails.send()`.

**src/routes/send/+server.ts**

```ts
import { MAILBLASTR_API_KEY } from '$env/static/private';
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr(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: '<p>Hello world</p>',
  });

  if (error) {
    return Response.json({ error }, { status: 500 });
  }

  return Response.json({ data });
}
```

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