Raw
1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Customer Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Customer Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the [Medusa Admin User Guide](!user-guide!/customers) to learn how to manage customers and groups using the dashboard.
18
19 </Note>
20
21 Medusa has customer related features available out-of-the-box through the Customer Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Customer Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## Customer Features
30
31 - [Customer Management](./customer-accounts/page.mdx): Store and manage guest and registered customers in your store.
32 - [Customer Organization](/references/customer/models): Organize customers into groups. This has a lot of benefits and supports many use cases, such as provide discounts for specific customer groups using the [Promotion Module](../promotion/page.mdx).
33
34 ---
35
36 ## How to Use the Customer Module
37
38 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
39
40 You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package.
41
42 For example:
43
44 export const highlights = [
45 ["12", "Modules.CUSTOMER", "Resolve the module in a step."]
46 ]
47
48 ```ts title="src/workflows/create-customer.ts" highlights={highlights}
49 import {
50 createWorkflow,
51 WorkflowResponse,
52 createStep,
53 StepResponse,
54 } from "@medusajs/framework/workflows-sdk"
55 import { Modules } from "@medusajs/framework/utils"
56
57 const createCustomerStep = createStep(
58 "create-customer",
59 async ({}, { container }) => {
60 const customerModuleService = container.resolve(Modules.CUSTOMER)
61
62 const customer = await customerModuleService.createCustomers({
63 first_name: "Peter",
64 last_name: "Hayes",
65 email: "peter.hayes@example.com",
66 })
67
68 return new StepResponse({ customer }, customer.id)
69 },
70 async (customerId, { container }) => {
71 if (!customerId) {
72 return
73 }
74 const customerModuleService = container.resolve(Modules.CUSTOMER)
75
76 await customerModuleService.deleteCustomers([customerId])
77 }
78 )
79
80 export const createCustomerWorkflow = createWorkflow(
81 "create-customer",
82 () => {
83 const { customer } = createCustomerStep()
84
85 return new WorkflowResponse({
86 customer,
87 })
88 }
89 )
90 ```
91
92 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
93
94 <CodeTabs group="resource-types">
95 <CodeTab label="API Route" value="api-route">
96
97 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
98 import type {
99 MedusaRequest,
100 MedusaResponse,
101 } from "@medusajs/framework/http"
102 import { createCustomerWorkflow } from "../../workflows/create-customer"
103
104 export async function GET(
105 req: MedusaRequest,
106 res: MedusaResponse
107 ) {
108 const { result } = await createCustomerWorkflow(req.scope)
109 .run()
110
111 res.send(result)
112 }
113 ```
114
115 </CodeTab>
116 <CodeTab label="Subscriber" value="subscriber">
117
118 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
119 import {
120 type SubscriberConfig,
121 type SubscriberArgs,
122 } from "@medusajs/framework"
123 import { createCustomerWorkflow } from "../workflows/create-customer"
124
125 export default async function handleUserCreated({
126 event: { data },
127 container,
128 }: SubscriberArgs<{ id: string }>) {
129 const { result } = await createCustomerWorkflow(container)
130 .run()
131
132 console.log(result)
133 }
134
135 export const config: SubscriberConfig = {
136 event: "user.created",
137 }
138 ```
139
140 </CodeTab>
141 <CodeTab label="Scheduled Job" value="scheduled-job">
142
143 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
144 import { MedusaContainer } from "@medusajs/framework/types"
145 import { createCustomerWorkflow } from "../workflows/create-customer"
146
147 export default async function myCustomJob(
148 container: MedusaContainer
149 ) {
150 const { result } = await createCustomerWorkflow(container)
151 .run()
152
153 console.log(result)
154 }
155
156 export const config = {
157 name: "run-once-a-day",
158 schedule: `0 0 * * *`,
159 }
160 ```
161
162 </CodeTab>
163 </CodeTabs>
164
165 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
166
167 ---
168
169 <CommerceModuleSections />