Markdown

Groundmail with NestJS

Use nestjs-groundmail to register a Groundmail client once and inject it into any NestJS provider. It works with NestJS 10, 11 and 12, in CommonJS and ESM apps.

Updated Oct 16, 20261 min read

Install

Terminal
npm install nestjs-groundmail
Terminal
export GROUNDMAIL_API_KEY=gm_your_key_here

Register the module

TypeScript
// app.module.ts
import { Module } from '@nestjs/common'
import { GroundmailModule } from 'nestjs-groundmail'

@Module({
  imports: [GroundmailModule.forRoot({ apiKey: process.env.GROUNDMAIL_API_KEY, isGlobal: true })],
})
export class AppModule {}

To read the key from @nestjs/config, use forRootAsync:

TypeScript
GroundmailModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({ apiKey: config.getOrThrow('GROUNDMAIL_API_KEY') }),
  isGlobal: true,
})

Send an email

TypeScript
// receipts.service.ts
import { Injectable } from '@nestjs/common'
import { InjectGroundmail, type Groundmail } from 'nestjs-groundmail'

@Injectable()
export class ReceiptsService {
  constructor(@InjectGroundmail() private readonly groundmail: Groundmail) {}

  sendReceipt(to: string) {
    return this.groundmail.emails.send({
      from: 'Acme <receipts@yourdomain.com>',
      to,
      subject: 'Your receipt',
      html: '<p>Thanks for your purchase.</p>',
    })
  }
}

The injected client is the full Node SDK, so templates, idempotency keys, scheduling, domains and webhooks are all available.

Options

Option Purpose
apiKey Groundmail API key. Falls back to GROUNDMAIL_API_KEY
baseUrl Override the API base URL
isGlobal Make the client injectable in every module
client Use an existing Groundmail instance (tests)

You can also inject by token with @Inject(GROUNDMAIL_CLIENT).

Source

Groundmail/nestjs-groundmail

Was this page helpful?