Markdown

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.

Updated Oct 21, 20261 min read

Install the SDK

Terminal
npm install groundmail

Set your API key

Terminal
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.

Send from an API route

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

TypeScript
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:

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

Full walkthrough: Send React Email.

v0

v0 generates Next.js apps, so the pattern above applies: send from a Route Handler (app/api/.../route.ts) or a Server Action.

  1. For local work, put GROUNDMAIL_API_KEY=gm_… in .env.local.
  2. In Vercel, open the project, then Settings, then Environment Variables, and add GROUNDMAIL_API_KEY for Production, Preview and Development.
  3. Redeploy. Vercel only picks up a new variable on the next deployment.

Never give the key a NEXT_PUBLIC_ name: Next.js puts those variables in the browser bundle. Don't read it in a client component either.

To have v0 write the integration, paste the builder prompt with this as rule 6:

6. Send from a Route Handler (app/api/.../route.ts) or a Server Action, using the groundmail npm package. GROUNDMAIL_API_KEY is a server-only Vercel environment variable: never use a NEXT_PUBLIC_ name for it. Note in the README that GROUNDMAIL_API_KEY must be set in Vercel before deploying.

Until your domain is verified, send from noreply@mail.groundmail.dev to your own Groundmail account email. To send to other people, verify your domain, add a payment method under Settings → Billing (the Free plan is not charged), set from to an address on your domain, and confirm GROUNDMAIL_API_KEY is set for Production in Vercel.

Was this page helpful?