--- slug: /references/notification-provider-module --- import { TypeList } from "docs-ui" # How to Create a Notification Provider Module In this document, you’ll learn how to create a notification provider module and the methods you must implement in it. --- ## 1. Create Module Directory Start by creating a new directory for your module. For example, `src/modules/my-notification`. --- ## 2. Create the Notification Provider Service Create the file `src/modules/my-notification/service.ts` that holds the implementation of the notification service. The Notification Provider Module's main service must extend the `AbstractNotificationProviderService` class imported from `@medusajs/framework/utils`: ```ts title="src/modules/my-notification/service.ts" import { AbstractNotificationProviderService } from "@medusajs/framework/utils" class MyNotificationProviderService extends AbstractNotificationProviderService { // TODO add methods } export default MyNotificationProviderService ``` ### constructor The constructor allows you to access resources from the module's container using the first parameter, and the module's options using the second parameter. If you're creating a client or establishing a connection with a third-party service, do it in the constructor. #### Example ```ts import { AbstractNotificationProviderService } from "@medusajs/framework/utils" import { Logger } from "@medusajs/framework/types" type InjectedDependencies = { logger: Logger } type Options = { apiKey: string } class MyNotificationProviderService extends AbstractNotificationProviderService { protected logger_: Logger protected options_: Options // assuming you're initializing a client protected client constructor ( { logger }: InjectedDependencies, options: Options ) { super() this.logger_ = logger this.options_ = options // assuming you're initializing a client this.client = new Client(options) } } export default MyNotificationProviderService ``` ### constructor ### validateOptions Override this static method in order for the loader to validate the options provided to the module provider. #### Parameters `","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} expandUrl="https://docs.medusajs.com/v2/advanced-development/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="validateOptions"/> #### Returns ### send This method is used to send a notification using the third-party provider or your custom logic. #### Example ```ts // other imports... import { ProviderSendNotificationDTO, ProviderSendNotificationResultsDTO } from "@medusajs/framework/types" class MyNotificationProviderService extends AbstractNotificationProviderService { // ... async send( notification: ProviderSendNotificationDTO ): Promise { // TODO send the notification using a third-party // provider or custom logic. // for example: return this.client.send({ email: notification.to, template: notification.template, template_data: notification.data }) } } ``` #### Parameters `","description":"The data that gets passed over to the provider for rendering the notification.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/v2/advanced-development/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="send"/> #### Returns --- ## 3. Create Module Definition File Create the file `src/modules/my-notification/index.ts` with the following content: ```ts title="src/modules/my-notification/index.ts" import MyNotificationProviderService from "./service" export default { services: [MyNotificationProviderService], } ``` This exports the module's definition, indicating that the `MyNotificationProviderService` is the module's service. --- ## 4. Use Module To use your Notification Module Provider, add it to the `providers` array of the Notification Module: The Notification Module accepts one provider per channel. ```js title="medusa-config.js" const { Modules } = require("@medusajs/framework/utils") // ... module.exports = defineConfig({ // ... modules: { [Modules.NOTIFICATION]: { resolve: "@medusajs/framework/notification", options: { providers: [ { resolve: "./modules/my-notification", id: "my-notification", options: { channels: ["email"], // provider options... }, }, ], }, }, } }) ``` Make sure to specify the correct channels for your provider in the `channels` option. --- ## 5. Test it Out ### Create Subscriber To test out the provider, create a subscriber at `src/subscribers/user-created.ts` with the following content: ```ts title="src/subscribers/user-created.ts" import { Modules } from "@medusajs/framework/utils" import { SubscriberArgs, type SubscriberConfig, } from "@medusajs/medusa" export default async function userCreatedHandler({ event: { data }, container, }: SubscriberArgs<{ id: string }>) { const notificationModuleService = container.resolve( Modules.NOTIFICATION ) const userModule = container.resolve( Modules.USER ) const user = await userModule.retrieveUser(data.id) await notificationModuleService.createNotifications({ to: user.email, channel: "email", template: "new-user" }) } export const config: SubscriberConfig = { event: "user.created", } ``` In the subscriber, you resolve the Notification and User modules. Then, you use the User Module's main service to retrieve the user's details. Finally, you use the Notification Module's main service to send a notification to the user's email through the `email` channel (assuming that's your provider's channel). Make sure to replace the value of `template` to the ID of the template in your provider. ### Create User Use the following command to create a user: ```bash npx medusa user -e admin@test.com -p supersecret ``` After the user is created, the subscriber is executed, sending the notification using your provider.