Raw
1 ---
2 sidebar_label: "Send Notification"
3 tags:
4 - notification
5 - how to
6 - server
7 ---
8
9 import { TypeList } from "docs-ui"
10
11 export const metadata = {
12 title: `Send Notification with the Notification Module`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn about the different ways to send notifications using the Notification Module.
18
19 ## Using the Create Method
20
21 In your resource, such as a [subscriber](!docs!/learn/fundamentals/events-and-subscribers), resolve the Notification Module's main service and use its `create` method:
22
23 export const highlights = [
24 ["12", "notificationModuleService", "Resolve the Notification Module."],
25 ["15", "create", "Create the notification to be sent."],
26 [
27 "17",
28 '"email"',
29 "Use the module provider defined for the `email` channel to send an email.",
30 ],
31 [
32 "18",
33 '"product-created"',
34 "The ID of the template defined in the third-party service, such as SendGrid.",
35 ],
36 [
37 "19",
38 "data",
39 "The data to pass to the template defined in the third-party service.",
40 ],
41 ]
42
43 ```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
44 import type {
45 SubscriberArgs,
46 SubscriberConfig,
47 } from "@medusajs/framework"
48 import { Modules } from "@medusajs/framework/utils"
49 import { INotificationModuleService } from "@medusajs/framework/types"
50
51 export default async function productCreateHandler({
52 event: { data },
53 container,
54 }: SubscriberArgs<{ id: string }>) {
55 const notificationModuleService: INotificationModuleService =
56 container.resolve(Modules.NOTIFICATION)
57
58 await notificationModuleService.createNotifications({
59 to: "user@gmail.com",
60 channel: "email",
61 template: "product-created",
62 data,
63 })
64 }
65
66 export const config: SubscriberConfig = {
67 event: "product.created",
68 }
69 ```
70
71 The `create` method accepts an object or an array of objects having the following properties:
72
73 <TypeList
74 types={[
75 {
76 name: "to",
77 type: "`string`",
78 description:
79 "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.",
80 optional: false,
81 },
82 {
83 name: "channel",
84 type: "`string`",
85 description:
86 "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.",
87 optional: false,
88 },
89 {
90 name: "template",
91 type: "`string`",
92 description:
93 "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.",
94 optional: false,
95 },
96 {
97 name: "data",
98 type: "`Record<string, unknown>`",
99 description: "The data to pass along to the template, if necessary.",
100 },
101 ]}
102 sectionTitle="Use the Create Method"
103 />
104
105 For a full list of properties accepted, refer to [this guide](/references/notification-provider-module#create).
106
107 ---
108
109 ## Using the sendNotificationsStep
110
111 If you want to send a notification as part of a workflow, You can use the [sendNotificationsStep](/references/medusa-workflows/steps/sendNotificationsStep) in your workflow.
112
113 For example:
114
115 ```ts title="src/workflows/send-email.ts"
116 import {
117 createWorkflow,
118 transform,
119 } from "@medusajs/framework/workflows-sdk"
120 import {
121 sendNotificationsStep,
122 useQueryGraphStep,
123 } from "@medusajs/medusa/core-flows"
124
125 type WorkflowInput = {
126 id: string
127 }
128
129 export const sendEmailWorkflow = createWorkflow(
130 "send-email-workflow",
131 ({ id }: WorkflowInput) => {
132 const { data: products } = useQueryGraphStep({
133 entity: "product",
134 fields: [
135 "*",
136 "variants.*",
137 ],
138 filters: {
139 id,
140 },
141 })
142
143 const notificationData = transform(
144 { products },
145 ({ products }) => ({
146 product_title: products[0].title,
147 product_image: products[0].images[0]?.url,
148 })
149 )
150
151 sendNotificationsStep({
152 to: "user@gmail.com",
153 channel: "email",
154 template: "product-created",
155 data: notificationData,
156 })
157 }
158 )
159 ```
160
161 For a full list of input properties accepted, refer to the [sendNotificationsStep](/references/medusa-workflows/steps/sendNotificationsStep) reference.
162
163 You can then execute this workflow in a subscriber, API route, or scheduled job.
164
165 For example, you can execute it when a product is created:
166
167 ```ts title="src/subscribers/product-created.ts"
168 import type {
169 SubscriberArgs,
170 SubscriberConfig,
171 } from "@medusajs/framework"
172 import { sendEmailWorkflow } from "../workflows/send-email"
173
174 export default async function productCreateHandler({
175 event: { data },
176 container,
177 }: SubscriberArgs<{ id: string }>) {
178 await sendEmailWorkflow(container).run({
179 input: {
180 id: data.id,
181 },
182 })
183 }
184
185 export const config: SubscriberConfig = {
186 event: "product.created",
187 }
188 ```