Skip to main content
This guide shows you how to integrate BosonMail into an Express application written in TypeScript and ESM. You will set up a shared client, create POST and GET endpoints for sending emails and checking status, handle file uploads with multer, and protect your routes so you do not create an open email relay.
This guide assumes Node.js 18 or later, Express 4 or 5, and TypeScript. The BosonMail SDK is ESM-only, so the examples use ESM syntax.

Prerequisites

  • Node.js 18 or later
  • A BosonMail API key
  • Express with TypeScript configured for ESM

Project setup

1

Install dependencies

Install the SDK and optional multer for file uploads.
2

Configure environment variables

Create a .env file and load it with dotenv in your entry point.
.env

Shared client module

Create a single BosonMailClient instance and export it for reuse across routes.
lib/bosonmail.ts

Application entry point

Wire up JSON parsing, routes, and centralized error handling in your main file.
src/index.ts

Sending attachments with multer

Use multer with memoryStorage so you can pass file buffers directly to the SDK without writing to disk.
src/index.ts
Multer limits are set to match the SDK’s per-attachment limit (10 MB) and maximum file count (10). The SDK also enforces a 20 MB total attachment limit. Validate uploads in your own middleware before they reach multer when possible.

Protecting your endpoints

Never leave a send endpoint open to the public internet without authentication. At minimum:
  • Require an API key or session cookie on /api/send.
  • Add rate limiting per user or IP (for example, with express-rate-limit).
  • For public contact forms, use a CAPTCHA and strict recipient allowlists so you cannot be used as an open relay.

Error response mapping

Return consistent HTTP status codes so callers can distinguish client errors from upstream failures.

Sending emails

SDK reference for sendEmail and its options.

Email status

How to check delivery status with getEmailStatus.

Production best practices

Key management, retries, logging, and monitoring guidance.

Attachments

SDK reference for sending file attachments.