Send Notification with the Notification Module

In this guide, you'll learn about the different ways to send notifications using the Notification Module.

Using the Create Method#

In your resource, such as a subscriber, resolve the Notification Module's main service and use its create method:

src/subscribers/product-created.ts
6import { INotificationModuleService } from "@medusajs/framework/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: "user@gmail.com",17    channel: "email",18    template: "product-created",19    data,20  })21}22
23export const config: SubscriberConfig = {24  event: "product.created",25}

The create method accepts an object or an array of objects having the following properties:

tostring
The destination to send the notification to. When sending an email, it'll be the email address. When sending an SMS, it'll be the phone number.
channelstring
The channel to send the notification through. For example, email or sms. The module provider defined for that channel will be used to send the notification.
templatestring
The ID of the template used for the notification. This is useful for providers like SendGrid, where you define templates within SendGrid and use their IDs here.
dataRecord<string, unknown>
The data to pass along to the template, if necessary.

For a full list of properties accepted, refer to this guide.


Using the sendNotifcationStep#

If you want to send a notification as part of a workflow, You can use the sendNotifcationStep in your workflow.

For example:

src/workflows/send-email.ts
1import { createWorkflow } from "@medusajs/framework/workflows-sdk"2import { 3  sendNotificationsStep, 4  useQueryGraphStep,5} from "@medusajs/medusa/core-flows"6
7type WorkflowInput = {8  id: string9}10
11export const sendEmailWorkflow = createWorkflow(12  "send-email-workflow",13  ({ id }: WorkflowInput) => {14    const { data: products } = useQueryGraphStep({15      entity: "product",16      fields: [17        "*",18        "variants.*",19      ],20      filters: {21        id,22      },23    })24
25    sendNotificationsStep({26      to: "user@gmail.com",27      channel: "email",28      template: "product-created",29      data: {30        product_title: product[0].title,31        product_image: product[0].images[0]?.url,32      },33    })34  }35)

For a full list of input properties accepted, refer to the sendNotifcationStep reference.

You can then execute this workflow in a subscriber, API route, or scheduled job.

For example, you can execute it when a product is created:

src/subscribers/product-created.ts
1import type {2  SubscriberArgs,3  SubscriberConfig,4} from "@medusajs/framework"5import { sendEmailWorkflow } from "../workflows/send-email"6
7export default async function productCreateHandler({8  event: { data },9  container,10}: SubscriberArgs<{ id: string }>) {11  await sendEmailWorkflow(container).run({12    input: {13      id: data.id,14    },15  })16}17
18export const config: SubscriberConfig = {19  event: "product.created",20}
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break