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:
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:
to
stringchannel
stringemail
or sms
. The module provider defined for that channel will be used to send the notification.template
stringdata
Record<string, unknown>For a full list of properties accepted, refer to this guide.
Using the sendNotificationsStep#
If you want to send a notification as part of a workflow, You can use the sendNotificationsStep in your workflow.
For example:
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 sendNotificationsStep 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:
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?