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 singleBosonMailClient 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
Usemulter with memoryStorage so you can pass file buffers directly to the SDK without writing to disk.
src/index.ts
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.Related
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.