Sign inGet Started

Node.js SDK

Send email with NestJS

Wrap Parcel Wing in an injectable NestJS service for transactional and lifecycle email.

Install the SDK

The official SDK is published as @parcelwing/node.

Terminal

npm install @parcelwing/node

Mail service

Create one service that owns the Parcel Wing client and exposes application-specific email methods.

mail.service.ts

import { Injectable } from "@nestjs/common";
import { ParcelWing } from "@parcelwing/node";
 
@Injectable()
export class MailService {
private readonly parcelWing = new ParcelWing({
apiKey: process.env.PARCEL_WING_API_KEY!,
});
 
async sendWelcomeEmail(to: string, firstName: string) {
const [message] = await this.parcelWing.emails.send({
from: "Acme <[email protected]>",
to,
template_alias: "welcome_email",
template_params: { first_name: firstName },
});
 
return message;
}
}

Controller

Call your mail service from controllers, queues, or event handlers after validation and persistence.

mail.controller.ts

import { Body, Controller, Post } from "@nestjs/common";
import { MailService } from "./mail.service";
 
@Controller("mail")
export class MailController {
constructor(private readonly mailService: MailService) {}
 
@Post("welcome")
async sendWelcome(@Body() body: { email: string; firstName: string }) {
const message = await this.mailService.sendWelcomeEmail(body.email, body.firstName);
return { id: message?.id };
}
}