Groundmail with Next.js

Use Groundmail from a server context in Next.js: Route Handlers, Server Actions, background jobs, or any trusted backend code. Do not expose your gm_… API key in the browser.

1. Install the SDK

npm install groundmail

2. Set your API key

export GROUNDMAIL_API_KEY=gm_your_key_here

In Vercel or Kubernetes, store the same value as an environment variable instead of committing it to the repo.

3. Send from an API route

Create app/api/send-welcome/route.ts:

import { NextResponse } from 'next/server'
import { Groundmail } from 'groundmail'

const tx = new Groundmail(process.env.GROUNDMAIL_API_KEY!)

export async function POST() {
  const result = await tx.emails.send({
    from: 'onboarding@yourdomain.com',
    to: 'user@example.com',
    subject: 'Welcome to your app',
    html: '<p>Your account is ready.</p>',
  })

  return NextResponse.json(result)
}

React Email

Author components with React Email, then pass them to the SDK:

npm install @react-email/render react
await tx.emails.send({
  from: 'onboarding@yourdomain.com',
  to: 'user@example.com',
  subject: 'Welcome',
  react: WelcomeEmail({ name: 'Ada' }),
})

Full walkthrough: Send React Email.