npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@onivoro/server-sendgrid

v24.33.2

Published

A simple SendGrid integration module for NestJS applications, providing basic email sending functionality.

Readme

@onivoro/server-sendgrid

A simple SendGrid integration module for NestJS applications, providing basic email sending functionality.

Installation

npm install @onivoro/server-sendgrid @sendgrid/mail

Overview

This library provides:

  • NestJS module for SendGrid integration
  • Basic email sending with attachments
  • Simple configuration management
  • Singleton SendGrid client instance

Module Setup

import { ServerSendgridModule, ServerSendgridConfig } from '@onivoro/server-sendgrid';

const config = new ServerSendgridConfig(
  process.env.SENDGRID_API_KEY,    // Your SendGrid API key
  '[email protected]'        // From email address
);

@Module({
  imports: [
    ServerSendgridModule.configure(config)
  ]
})
export class AppModule {}

Configuration

The ServerSendgridConfig class requires two parameters:

export class ServerSendgridConfig {
  constructor(
    public SENDGRID_API_KEY: string,  // SendGrid API key
    public FROM: string               // Default sender email
  ) {}
}

Usage

EmailService

The service provides a single method for sending emails:

import { Injectable } from '@nestjs/common';
import { EmailService } from '@onivoro/server-sendgrid';

@Injectable()
export class NotificationService {
  constructor(private emailService: EmailService) {}

  async sendWelcomeEmail(userEmail: string, userName: string) {
    await this.emailService.sendEmail(
      userEmail,                                    // to
      'Welcome to Our Platform!',                   // subject
      `<h1>Hello ${userName}</h1><p>Welcome!</p>`, // html
      `Hello ${userName}, Welcome!`,               // text
      []                                           // attachments (optional)
    );
  }
}

API Reference

EmailService.sendEmail

async sendEmail(
  to: string,
  subject: string,
  html: string,
  text: string,
  attachments?: Array<{
    content: string;    // Base64 encoded content
    filename: string;
    type: string;      // MIME type
  }>
): Promise<any>

Parameters:

  • to - Recipient email address
  • subject - Email subject line
  • html - HTML content of the email
  • text - Plain text content of the email
  • attachments - Optional array of attachments

Complete Example

import { Injectable } from '@nestjs/common';
import { EmailService } from '@onivoro/server-sendgrid';
import { readFileSync } from 'fs';

@Injectable()
export class InvoiceService {
  constructor(private emailService: EmailService) {}

  async sendInvoice(customerEmail: string, invoiceNumber: string, pdfPath: string) {
    const pdfContent = readFileSync(pdfPath).toString('base64');
    
    await this.emailService.sendEmail(
      customerEmail,
      `Invoice #${invoiceNumber}`,
      `
        <h2>Invoice #${invoiceNumber}</h2>
        <p>Please find your invoice attached.</p>
        <p>Thank you for your business!</p>
      `,
      `Invoice #${invoiceNumber}\n\nPlease find your invoice attached.\n\nThank you for your business!`,
      [{
        content: pdfContent,
        filename: `invoice-${invoiceNumber}.pdf`,
        type: 'application/pdf'
      }]
    );
  }

  async sendPasswordReset(userEmail: string, resetToken: string) {
    const resetUrl = `https://yourapp.com/reset-password?token=${resetToken}`;
    
    await this.emailService.sendEmail(
      userEmail,
      'Password Reset Request',
      `
        <p>You requested a password reset.</p>
        <p><a href="${resetUrl}">Click here to reset your password</a></p>
        <p>This link will expire in 1 hour.</p>
      `,
      `You requested a password reset.\n\nClick here to reset your password: ${resetUrl}\n\nThis link will expire in 1 hour.`
    );
  }
}

Important Notes

  1. This is a basic wrapper around SendGrid's mail service
  2. The SendGrid client is initialized as a singleton
  3. Only supports single recipient emails (no bulk sending)
  4. The FROM address is configured globally for all emails
  5. No template support - you must provide HTML and text content
  6. Limited error handling - errors are thrown directly from SendGrid

Limitations

  • No dynamic templates
  • No bulk email support
  • No personalization features
  • No advanced SendGrid features (tracking, scheduling, etc.)
  • Single sender address for all emails

For more advanced features, consider using the SendGrid API directly or extending this service.

License

MIT