Skip to main content
This guide shows you how to use the BosonMail SDK with Bun. You will initialize a project, install the SDK, load environment variables automatically, send emails from scripts and HTTP servers, read file attachments with Bun.file, and write tests with mocked clients.
BosonWare builds the BosonMail package with Bun (prepublishOnly: bun run build) and documents bun add in the README. Bun loads .env files automatically, so no extra dotenv package is required.

Prerequisites

  • Bun 1.0 or later
  • A BosonMail API key

Project setup

1

Initialize the project

Create a new Bun project with TypeScript support.
2

Install the SDK

Add the BosonMail SDK from the npm registry.
3

Add environment variables

Create a .env file in the project root. Bun loads it automatically on startup.
.env

Sending email from a script

Create a standalone script that reads the API key from Bun.env or process.env and sends a simple email.
send.ts
Run the script:

Sending attachments with Bun.file

Read files directly into ArrayBuffer and pass them to the SDK.
send-with-attachment.ts
Bun.file returns a BunFile that implements Blob. Calling .arrayBuffer() gives you an ArrayBuffer, which the SDK accepts directly. You can also pass the BunFile itself as the content because it is a Blob subclass.

HTTP server with Bun.serve

Expose a minimal HTTP endpoint that accepts JSON and sends email.
server.ts
Run the server:

Testing with Bun test

Bun has a built-in test runner. Mock the client by injecting a stub or by replacing the module import.
send.test.ts
Run tests:
For integration tests, start a local HTTP interceptor or a mock server on a custom baseUrl. The SDK never throws for API or network failures, so you can assert on result.success and result.statusCode directly.

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.