Guide · JavaScript & TypeScript
How to send email with Bun
Send email from Bun with the built-in fetch and Bun.env. No packages to install.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
Bun loads .env automatically:
echo "STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx" >> .env
2. Send your first email
const res = await fetch("https://stackemails.com/v1/emails", {
method: "POST",
headers: { Authorization: `Bearer ${Bun.env.STACKEMAILS_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({
from: "Acme <hello@mail.acme.com>",
to: "ada@example.com",
subject: "Hello from Bun",
html: "<p>Sent with Bun and Stack Emails.</p>",
}),
});
console.log(res.status, await res.json());
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.