Skip to content
  • Email: Live

Send email with Node.js and Express

Install the bavimail package, add an Express route that emails the signed-in user, check delivery, then receive the reply on a signed webhook and answer in the same thread.

A code sample that sends a welcome email from an Express route with the Bavimail SDK.

Key takeaways

  • Install bavimail with npm, create a client with your API key, and call client.emails.send() from an Express route.
  • The route emails only the signed-in user's own address, never an address from the request, so nobody else can send mail from your account.
  • A new email starts as queued; client.emails.get() shows when it is delivered.
  • Replies arrive as signed email.inbound.received webhook events; verify them with verifyWebhookSignature and answer with client.emails.send() using the message's threadId and inReplyTo.

Install bavimail, create a client with your API key, and call client.emails.send() from an Express route. The snippets below use bavimail 1.0.0, the current release on npm, with Express 5 on Node.js 18 or later. The last section receives the reply on a signed webhook and answers in the same thread. Bavimail email is live, with a free plan.

What you need

  • A Bavimail plan with a domain allowance: Developer ($5 a month) includes 10 domains with 10 inboxes each, and the catalog lists no custom-domain allowance on Free. See pricing.
  • A verified domain with at least one inbox on it. See domains.
  • An API key from the dashboard.
  • Node.js 18 or later.

1. Install the SDK and set your key

npm install express bavimail
export BAVIMAIL_API_KEY=your_api_key

The examples use ES modules, so set "type": "module" in package.json.

2. Find the address to send from

Each address on your domain is an inbox with an id. emails.send() takes the id.

import { Bavimail } from 'bavimail'

const client = new Bavimail({ apiKey: process.env.BAVIMAIL_API_KEY })

for (const inbox of await client.inboxes.list()) {
  console.log(inbox.id, inbox.fullEmail)
}

No inbox yet? client.inboxes.create({ domainId, name: 'agent' }) creates [email protected] on a verified domain.

Set the id of the address you want as BAVIMAIL_INBOX_ID.

3. Send from an Express route

The route emails the signed-in user's own address, never an address from the request. A route that emails whatever address it's given lets anyone who finds it send mail from your account.

import express from 'express'
import { Bavimail } from 'bavimail'

const app = express()
// Add your auth middleware here (Passport or a session library). It sets req.user.

const client = new Bavimail({ apiKey: process.env.BAVIMAIL_API_KEY })

function requireUser(req, res, next) {
  if (!req.user?.email) return res.status(401).json({ error: 'Sign in first' })
  next()
}

app.post('/welcome', requireUser, async (req, res) => {
  const email = await client.emails.send({
    inboxId: process.env.BAVIMAIL_INBOX_ID,
    toEmails: [req.user.email],
    subject: 'Welcome aboard',
    body: '<p>Thanks for signing up.</p>',
  })
  res.json({ id: email.id, status: email.status })
})

app.listen(3000, () => console.log('Listening on http://localhost:3000'))

Call it from your app once the user has signed in, for example right after sign-up. It returns the email id and queued. A request without a signed-in user gets a 401 and sends nothing. Also limit how often one user can call it, for example with express-rate-limit, so a single account can't use up your daily sending.

4. Check delivery

import { Bavimail } from 'bavimail'

const client = new Bavimail({ apiKey: process.env.BAVIMAIL_API_KEY })

const email = await client.emails.get('EMAIL_ID')
console.log(email.status)

In our October 15 test it returned delivered within 12 seconds of the send.

5. Read replies

import { Bavimail } from 'bavimail'

const client = new Bavimail({ apiKey: process.env.BAVIMAIL_API_KEY })

const page = await client.inboundEmails.list({
  inboxId: process.env.BAVIMAIL_INBOX_ID,
  limit: 10,
})
for (const message of page.data) {
  console.log(message.receivedAt, message.fromAddress, message.subject)
}

To get replies as they arrive instead of polling, use a webhook, as below.

Receive the reply and answer in the thread

Every message to the inbox fires an email.inbound.received event. Its data carries email_id, inbox_id, inbox_address, from_email, from_name, subject, body_preview, attachment_count and received_at; the SDK hands it over in camelCase (emailId, inboxId, …). The route verifies the signature over the raw body, acknowledges at once, then fetches the full message and replies from the same inbox with its threadId and inReplyTo, so the answer lands in the sender's existing thread.

import express from 'express'
import {
  Bavimail,
  isWebhookTestEvent,
  isWebhookVerificationChallenge,
  verifyWebhookSignature,
} from 'bavimail'

const client = new Bavimail({ apiKey: process.env.BAVIMAIL_API_KEY })
const app = express()
const answered = new Set() // event ids already handled; use your database in production

async function answer(emailId) {
  const message = await client.inboundEmails.get(emailId)
  if (message.fromAddress === message.inboxAddress) return
  const reply = '<p>Thanks, we received your message.</p>' // have your agent write this from message.bodyText
  const subject = /^re:/i.test(message.subject) ? message.subject : `Re: ${message.subject}`
  await client.emails.send({
    inboxId: message.inboxId,
    toEmails: [message.replyTo ?? message.fromAddress],
    subject,
    body: reply,
    threadId: message.threadId,
    inReplyTo: message.messageId,
  })
}

app.post('/webhooks/bavimail', express.raw({ type: 'application/json' }), async (req, res) => {
  let payload
  try {
    payload = await verifyWebhookSignature(
      req.body.toString('utf8'),
      req.get('x-webhook-signature') ?? '',
      req.get('x-webhook-timestamp') ?? '',
      process.env.BAVIMAIL_WEBHOOK_SECRET,
    )
  } catch {
    return res.sendStatus(403)
  }

  if (isWebhookVerificationChallenge(payload)) {
    await client.webhooks.verify(process.env.BAVIMAIL_WEBHOOK_ID, payload.verificationCode)
    return res.sendStatus(200)
  }

  res.sendStatus(200)
  if (isWebhookTestEvent(payload) || payload.eventType !== 'email.inbound.received') return
  if (answered.has(payload.eventId)) return
  answered.add(payload.eventId)
  answer(payload.data.emailId).catch((err) => console.error('Reply failed', err))
})

app.listen(3000, () => console.log('Listening on http://localhost:3000'))

Run it behind a public HTTPS URL, then register the webhook once:

const webhook = await client.webhooks.create({
  url: 'https://agent.example.com/webhooks/bavimail',
  eventTypes: ['email.inbound.received'],
})
console.log(webhook.id, webhook.secret) // the secret is shown once

Set BAVIMAIL_WEBHOOK_ID and BAVIMAIL_WEBHOOK_SECRET from that output, restart the app, and call client.webhooks.resendVerification(webhook.id). The app receives the signed challenge and activates the webhook with webhooks.verify; until then the webhook delivers nothing.

What the code relies on:

  • Signature. X-Webhook-Signature is sha256=<hex>, an HMAC-SHA256 of {X-Webhook-Timestamp}.{raw body} keyed with the hex-decoded secret. verifyWebhookSignature checks it, rejects timestamps more than 5 minutes old and returns the parsed payload. Mount express.raw() on this route only; express.json() would hand you re-serialized JSON whose signature no longer matches.
  • Acknowledge first. Bavimail waits 10 seconds for an answer. A non-2xx answer is retried, up to 6 attempts in all; a timeout is not retried and leaves the delivery for an operator to resolve. So the route answers 200 before it writes the reply.
  • Duplicates. eventId stays the same across redeliveries, so the route records it and answers once.
  • Threading. threadId keeps the reply in the same Bavimail thread, and Bavimail writes the In-Reply-To and References headers so mail clients thread it too.
  • Loops. The route skips mail the inbox sent to itself. If your agent may talk to other automated senders, add your own stop rule.

On Web Standard runtimes (Next.js App Router, Hono, Bun, Deno, Workers), client.createHandler({ secret, onVerification }) does the verification and dispatches to handlers registered with client.on('email.inbound.received', ...). See webhooks.

What it costs

The free plan sends 6,000 emails a month, at most 200 a day, with no card. Developer is US$5 a month for 50,000 emails (pricing).

Emails a month on each Bavimail planEach gridline is 10 times the one before.
  1. Free6,000
  2. Developer$5 a month50,000
  3. Startup$50 a month300,000
  4. Scale$500 a month2,500,000

Source: Bavimail pricing, checked October 15, 2026.

The same steps in Python, with a FastAPI webhook, are in Send email with Python.

FAQ

How do I send an email with Node.js?

Install bavimail with npm, set BAVIMAIL_API_KEY, and call client.emails.send() with an inbox id, a list of recipients, a subject and an HTML body.

Is it safe to send email from an Express route?

Only if the route checks who is calling. Send to the signed-in user's own address, never to an address from the request body, and limit how often one user can call it.

Which Node.js versions does the SDK support?

Node.js 18 or later.

How do I know the email was delivered?

Call client.emails.get(id) and read status. A new email starts as queued and moves to delivered.

How do I receive replies and answer them in Express?

Subscribe a webhook to email.inbound.received, read the raw body with express.raw(), verify it with verifyWebhookSignature, fetch the message with client.inboundEmails.get(emailId), and answer with client.emails.send() from the same inbox with threadId and inReplyTo set from the message.

How much does it cost?

The free plan has 6,000 emails a month and 200 a day, with no credit card. The Developer plan is US$5 a month for 50,000 emails.