# Send emails with Node.js

> Send email from Node.js using the mailblastr npm SDK — install once and call mb.emails.send().

The `mailblastr` npm package is the fastest way to send email from Node.js. Install it once and use the typed SDK — no manual `fetch` calls or header wrangling required.

This guide sends a single email with `mb.emails.send()`. Every other SDK method follows the same `{ data, error }` pattern.

## Prerequisites

1. A **verified domain** — add one under Domains and publish its SPF/DKIM/DMARC records so your `from` address is allowed. ([guide](https://www.mailblastr.com/docs/domains/managing))
2. An **API key** — create an `mb_` key under API Keys; it is shown once at creation. ([Authentication](https://www.mailblastr.com/docs/authentication))
3. **Node 18 or newer** and `npm install mailblastr` (or `yarn add mailblastr` / `pnpm add mailblastr`).

## Send an email

Read the key from `process.env.MAILBLASTR_API_KEY`. Every SDK call returns `{ data, error }` — check `error` before using `data`.

**send.mjs**

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

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

const { data, error } = await mb.emails.send({
  from: 'Acme <hello@yourdomain.com>',
  to: ['delivered@example.com'],
  subject: 'Hello from Node.js',
  html: '<p>Sent with the MailBlastr SDK.</p>',
});

if (error) {
  throw new Error(error.message);
}

console.log('Sent email', data.id);
```

## Handling the response

Every SDK method returns `{ data, error }`. On success `data` holds the API response (`{ id }` for a send); on failure `error` is populated and `data` is `null`. Store the returned `id` to [retrieve the email](https://www.mailblastr.com/docs/api/emails-get) later or to correlate [webhook](https://www.mailblastr.com/docs/webhooks/overview) events.

```json
{
  "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"
}
```

> **Warning:** Keep your `mb_` API key on the **server**. Never bundle it into client-side JavaScript or expose it in the browser — anyone with the key can send mail as you. Load it from an environment variable (or your secrets manager), never hard-code it.

## Next steps

- See the full [Send Email API](https://www.mailblastr.com/docs/api/emails-send) reference for every body parameter (`cc`, `bcc`, `reply_to`, `attachments`, `tags`, `scheduled_at`).
- Explore [the SDKs reference](https://www.mailblastr.com/docs/resources/sdks) for all available methods.
- Calling from a Next.js app? See [Send emails with Next.js](https://www.mailblastr.com/docs/integrations/nextjs).
