Development Resources

SendGrid Notification Module Provider

The SendGrid Notification Module Provider integrates SendGrid to send emails to users and customers.


Install the SendGrid Notification Module#

To install the SendGrid Notification Module Provider, run the following command in the directory of your Medusa application:

NoteMake sure that the version added in package.json is preview to avoid errors with installation and updates in the future.

Next, add the module into the providers array of the Notification Module:

NoteOnly one provider can be defined for a channel.
medusa-config.js
1const { Modules } = require("@medusajs/utils")2
3// ...4
5module.exports = defineConfig({6  // ...7  modules: {8    [Modules.NOTIFICATION]: {9      resolve: "@medusajs/notification",10      options: {11        providers: [12          // ...13          {14            resolve: "@medusajs/notification-sendgrid",15            id: "sendgrid",16            options: {17              channels: ["email"],18              api_key: process.env.SENDGRID_API_KEY,19              from: process.env.SENDGRID_FROM,20            },21          },22        ],23      },24    },25  },26})

Environment Variables#

Make sure to add the following environment variables:

Terminal
SENDGRID_API_KEY=<YOUR_SENDGRID_API_KEY>SENDGRID_FROM=<YOUR_SENDGRID_FROM>

SendGrid Notification Module Options#

OptionDescription
channels

The channels this notification module is used to send notifications for. Only one provider can be defined for a channel.

api_keyThe SendGrid API key.
fromThe SendGrid from email.

SendGrid Templates#

When you send a notification, you must specify the ID of the template to use in SendGrid.

Refer to this SendGrid documentation guide on how to create templates for your different email types.


Test out the Module#

To test the module out, create a simple subscriber at src/subscribers/product-created.ts with the following content:

src/subscribers/product-created.ts
6import { INotificationModuleService } from "@medusajs/types"7
8export default async function productCreateHandler({9  event: { data },10  container,11}: SubscriberArgs<{ id: string }>) {12  const notificationModuleService: INotificationModuleService =13    container.resolve(Modules.NOTIFICATION)14
15  await notificationModuleService.createNotifications({16    to: "test@gmail.com",17    from: "test@medusajs.com", // Optional var, verified sender required18    channel: "email",19    template: "product-created",20    data,21    attachments: [ // optional var22      {23        content: base64,24        content_type: "image/png", // mime type25        filename: filename.ext,26        disposition: "attachment or inline attachment",27        id: "id", // only needed for inline attachment28      },29    ],30  })31}32
33export const config: SubscriberConfig = {34  event: "product.created",35}

In this subscriber:

  • Resolve the Notification Module's main service.
  • Use the create method of the main service to create a notification to be sent to the specified email.
  • By specifying the email channel, the SendGrid Notification Module Provider is used to send the notification.
  • The template property of the create method's parameter specifies the ID of the template defined in SendGrid.
  • The data property allows you to pass data to the template in SendGrid.
  • The attachments optional property allows you to pass attachments to the template in SendGrid.
  • The from optional property allows you to pass a single sender-verified email. If not provided, the value of the from configuration of the module is used.

Then, start the Medusa application:

And create a product either using the API route or the Medusa Admin. This runs the subscriber and sends an email using SendGrid.

Was this page helpful?
Edit this page