Notification Module
In this document, you'll learn about the Notification Module and its providers.
What is the Notification Module?#
The Notification Module exposes the functionalities to send a notification to a customer or user. For example, sending an order confirmation email. Medusa uses the Notification Module in its core commerce features for notification operations, and you an use it in your custom features as well.
How to Use the Notification Module?#
You can use the Notification Module as part of the workflows you build for your custom features. A workflow is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
In a step of your workflow, you can resolve the Notification Module's service and use its methods to send notifications.
For example:
1import { Modules } from "@medusajs/framework/utils"2import { 3 createStep,4 createWorkflow,5} from "@medusajs/framework/workflows-sdk"6 7const step1 = createStep(8 "step-1",9 async ({}, { container }) => {10 const notificationModuleService = container.resolve(11 Modules.NOTIFICATION12 )13 14 await notificationModuleService.createNotifications({15 to: "customer@gmail.com",16 channel: "email",17 template: "product-created",18 data,19 })20 } 21)22 23export const workflow = createWorkflow(24 "workflow-1",25 () => {26 step1()27 }28)
In the example above, you create a workflow that has a step. In the step, you resolve the service of the Notification Module from the Medusa container.
Then, you use the createNotifications
method of the Notification Module to send an email notification.
Find a full example of sending a notification in the Send Notification guide.
What is a Notification Module Provider?#
A Notification Module Provider implements the underlying logic of sending notification. It either integrates a third-party service or uses custom logic to send the notification.
By default, Medusa uses the Local Notification Module which only simulates sending the notification by logging a message in the terminal.
Medusa provides other Notification Modules that actually send notifications, such as the SendGrid Notification Module Provider. You can also Create a Notification Module Provider.
Notification Module Provider Channels#
When you send a notification, you specify the channel to send it through, such as email
or sms
.
You register providers of the Notification Module in medusa-config.ts
. For each provider, you pass a channels
option specifying which channels the provider can be used in. Only one provider can be setup for each channel.
For example:
1import { Modules } from "@medusajs/framework/utils"2 3// ...4 5module.exports = {6 // ...7 modules: [8 // ...9 {10 resolve: "@medusajs/medusa/notification",11 options: {12 providers: [13 // ...14 {15 resolve: "@medusajs/medusa/notification-local",16 id: "notification",17 options: {18 channels: ["email"],19 },20 },21 ],22 },23 },24 ],25}
The channels
option is an array of strings indicating the channels this provider is used for.