Send Invite User Email Notification
In this guide, you'll learn how to handle the invite.created and invite.resent events to send an invite email (or other notification type) to the user.
User Invite Flow Overview#

Admin users can add new users to their store by sending them an invite. The flow for inviting users is as follows:
- An admin user invites a user either through the Medusa Admin or using the Create Invite API route.
- The invite is created and the
invite.createdevent is emitted.- At this point, you can handle the event to send an email or notification to the user.
- The invited user receives the invite and can accept it, which creates a new user.
- The invited user can accept the invite either through the Medusa Admin or using the Accept Invite API route.
The admin user can also resend the invite if the invited user doesn't receive the invite or doesn't accept it before expiry, which emits the invite.resent event.
In this guide, you'll implement a subscriber that handles the invite.created and invite.resent events to send an email to the user.
After adding the subscriber, you will have a complete user invite flow that you can utilize either through the Medusa Admin or using the Admin APIs.
Prerequisites: Email Sending#
To send an email to the user, you need a Notification Module Provider registered for the email channel.
Cloud projects send notifications of the email channel with Medusa Emails, Cloud's built-in email service. Medusa Emails is enabled by default, so you don't need to configure a Notification Module Provider or add API keys.
If you're not using Cloud, you can use another Notification Module Provider for the email channel, or create your own custom provider.
Testing with the Local Notification Module Provider#
Medusa Emails isn't available when you run the Medusa application locally. So, register the Local Notification Module Provider for the email channel during development by adding this to your medusa-config.ts:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/notification",
options: {
providers: [
// ...
{
resolve: "@medusajs/medusa/notification-local",
id: "local",
options: {
channels: [
process.env.NODE_ENV === "development"
? "email"
: "feed",
],
},
},
],
},
},
],
})
The Local provider logs email details to your terminal instead of sending actual emails, which is useful for development and testing. It handles the email channel only in development, so Medusa Emails sends the emails in your Cloud environments.
Create the Invite User Subscriber#
To create a subscriber that handles the invite.created and invite.resent events, create the file src/subscribers/user-invited.ts with the following content:
import { SubscriberArgs, type SubscriberConfig } from "@medusajs/framework"
import {
Modules,
ContainerRegistrationKeys,
} from "@medusajs/framework/utils"
export default async function inviteCreatedHandler({
event: { data },
container,
}: SubscriberArgs<{
id: string
}>) {
const query = container.resolve(
ContainerRegistrationKeys.QUERY
)
const notificationModuleService = container.resolve(
Modules.NOTIFICATION
)
const config = container.resolve(
ContainerRegistrationKeys.CONFIG_MODULE
)
const { data: [invite] } = await query.graph({
entity: "invite",
fields: [
"email",
"token",
],
filters: {
id: data.id,
},
})
const backend_url = config.admin.backendUrl !== "/" ? config.admin.backendUrl :
"http://localhost:9000"
const adminPath = config.admin.path
const inviteUrl = `${backend_url}${adminPath}` +
`/invite?token=${invite.token}`
await notificationModuleService.createNotifications({
to: invite.email,
channel: "email",
content: {
subject: "You've been invited",
html: `<p>Click <a href="${inviteUrl}">here</a> to accept the invite.</p>`,
},
})
}
export const config: SubscriberConfig = {
event: [
"invite.created",
"invite.resent",
],
}
The subscriber receives the ID of the invite in the event payload. You resolve Query to fetch the invite details, including the email and token.
Invite URL#
You then use the Notification Module's service to send an email to the user with the invite link. The invite link is constructed using:
- The backend URL of the Medusa application, since the Medusa Admin is hosted on the same URL.
- The admin path, which is the path to the Medusa Admin (for example,
/app).
Note that the Medusa Admin has an invite form at /invite?token=, which accepts the invite token as a query parameter.
Notification Configurations#
For the notification, you can configure the following fields:
channel: The channel to send the notification through. In this case, it's set toemail.content: The email's subject and HTML content, which includes the invite link. On Cloud, Medusa Emails sends this content to the user.- Refer to the Use a React Email Template section below to build a richer email template.
Test It Out#
After you set up the Local Notification Module Provider and create the subscriber, you can test the full invite flow.
Start the Medusa application with the following command:
Then, open the Medusa Admin (locally at http://localhost:9000/app) and go to Settings → Users. Create an invite as explained in the Manage Invites Merchant Guide.
Once you create the invite, you should see that the invite.created event is emitted in the server's logs:
The Local provider logs the email details in your terminal. After you deploy the subscriber to Cloud, Medusa Emails sends the invite email to the recipient's inbox. You can track its delivery on the Email Activity page of your Cloud project.
Use a React Email Template#
The subscriber sends a basic HTML email. To build a richer email template, use React Email to create the template as a React component, then render it to an HTML string in the subscriber.
The Set Up React Email Templates guide explains how to install React Email in your Medusa project, and walks you through creating an invite user email template and a subscriber that sends it.