Skip to content
  • Email: Live

Send email with Next.js

Install the bavimail package, add a Next.js route handler that emails the signed-in user, then check delivery. Code tested against the live API on Oct 15, 2026.

A code sample that sends a welcome email from a Next.js route handler with the Bavimail SDK.

Key takeaways

  • Install bavimail, put BAVIMAIL_API_KEY and BAVIMAIL_ALIAS_ID in .env.local, and call client.emails.send() from a route handler.
  • The handler runs on the server, so the API key never reaches the browser.
  • The handler 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.

Install bavimail, keep your API key in .env.local, and call client.emails.send() from an App Router route handler. The code below ran against the live Bavimail API on October 15, 2026, with bavimail 0.3.7 and Next.js 16.4.1. Bavimail email is live, with a free plan.

What you need

  • A Bavimail account. Start free, no credit card.
  • A verified domain with at least one address on it. See domains.
  • An API key from the dashboard, and the id of the address you send from (find it).
  • A Next.js app with the App Router.

1. Install the SDK and set your keys

npm install bavimail

In .env.local:

BAVIMAIL_API_KEY=your_api_key
BAVIMAIL_ALIAS_ID=your_alias_id

2. Send from a route handler

The handler 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.

Create app/api/welcome/route.ts:

import { Bavimail } from 'bavimail'
import { getCurrentUser } from '@/lib/auth'

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

export async function POST() {
  const user = await getCurrentUser()
  if (!user) {
    return Response.json({ error: 'Sign in first' }, { status: 401 })
  }

  const sent = await client.emails.send({
    aliasId: process.env.BAVIMAIL_ALIAS_ID!,
    toEmail: user.email,
    subject: 'Welcome aboard',
    body: '<p>Thanks for signing up.</p>',
  })

  return Response.json({ id: sent.id, status: sent.status })
}

getCurrentUser stands for your auth library's session lookup. It returns the signed-in user, or null.

Call it from your app once the user has signed in, for example right after sign-up:

await fetch('/api/welcome', { method: 'POST' })

It returns the email id and queued. Without a signed-in user it returns a 401 and sends nothing. The route runs on the server, so the key never reaches the browser. Also limit how often one user can call it, so a single account can't use up your daily sending.

3. Check delivery

client.emails.get(id) returns the email with its status. In our test the email was delivered within 6 seconds of being sent. To act on delivery, bounces or replies as they happen, use 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 are in Send email with Python.

FAQ

How do I send an email from Next.js?

Install bavimail, set BAVIMAIL_API_KEY and BAVIMAIL_ALIAS_ID in .env.local, and call client.emails.send() inside a route handler such as app/api/welcome/route.ts.

Can I send email from a client component?

No. Send from a route handler or another server-side function, so the API key stays on the server.

Is it safe to send email from a route handler?

Only if the handler 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.

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.