1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Sales Channel Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Sales Channel 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!/settings/sales-channels) to learn how to manage sales channels using the dashboard.
18
19 </Note>
20
21 Medusa has sales channel related features available out-of-the-box through the Sales Channel 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 Sales Channel 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 ## What's a Sales Channel?
30
31 A sales channel indicates an online or offline channel that you sell products on.
32
33 Some use case examples for using a sales channel:
34
35 - Implement a B2B Ecommerce Store.
36 - Specify different products for each channel you sell in.
37 - Support omnichannel in your ecommerce store.
38
39 ---
40
41 ## Sales Channel Features
42
43 - [Sales Channel Management](/references/sales-channel/models/SalesChannel): Manage sales channels in your store. Each sales channel has different meta information such as name or description, allowing you to easily differentiate between sales channels.
44 - [Product Availability](./links-to-other-modules/page.mdx): Medusa uses the Product and Sales Channel modules to allow merchants to specify a product's availability per sales channel.
45 - [Cart and Order Scoping](./links-to-other-modules/page.mdx): Carts, available through the Cart Module, are scoped to a sales channel. Paired with the product availability feature, you can show customers only the products available in their sales channel, and associate the created order with that sales channel.
46 - [Inventory Availability Per Sales Channel](./links-to-other-modules/page.mdx): Medusa links sales channels to stock locations, allowing you to retrieve available inventory of products based on the specified sales channel.
47
48 ---
49
50 ## How to Use Sales Channel Module's Service
51
52 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.
53
54 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.
55
56 For example:
57
58 export const highlights = [
59 ["12", "Modules.SALES_CHANNEL", "Resolve the module in a step."]
60 ]
61
62 ```ts title="src/workflows/create-sales-channel.ts" highlights={highlights}
63 import {
64 createWorkflow,
65 WorkflowResponse,
66 createStep,
67 StepResponse,
68 } from "@medusajs/framework/workflows-sdk"
69 import { Modules } from "@medusajs/framework/utils"
70
71 const createSalesChannelStep = createStep(
72 "create-sales-channel",
73 async ({}, { container }) => {
74 const salesChannelModuleService = container.resolve(Modules.SALES_CHANNEL)
75
76 const salesChannels = await salesChannelModuleService.createSalesChannels([
77 {
78 name: "B2B",
79 },
80 {
81 name: "Mobile App",
82 },
83 ])
84
85 return new StepResponse({ salesChannels }, salesChannels.map((sc) => sc.id))
86 },
87 async (salesChannelIds, { container }) => {
88 if (!salesChannelIds) {
89 return
90 }
91 const salesChannelModuleService = container.resolve(Modules.SALES_CHANNEL)
92
93 await salesChannelModuleService.deleteSalesChannels(
94 salesChannelIds
95 )
96 }
97 )
98
99 export const createSalesChannelWorkflow = createWorkflow(
100 "create-sales-channel",
101 () => {
102 const { salesChannels } = createSalesChannelStep()
103
104 return new WorkflowResponse({
105 salesChannels,
106 })
107 }
108 )
109 ```
110
111 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
112
113 <CodeTabs group="resource-types">
114 <CodeTab label="API Route" value="api-route">
115
116 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
117 import type {
118 MedusaRequest,
119 MedusaResponse,
120 } from "@medusajs/framework/http"
121 import { createSalesChannelWorkflow } from "../../workflows/create-sales-channel"
122
123 export async function GET(
124 req: MedusaRequest,
125 res: MedusaResponse
126 ) {
127 const { result } = await createSalesChannelWorkflow(req.scope)
128 .run()
129
130 res.send(result)
131 }
132 ```
133
134 </CodeTab>
135 <CodeTab label="Subscriber" value="subscriber">
136
137 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
138 import {
139 type SubscriberConfig,
140 type SubscriberArgs,
141 } from "@medusajs/framework"
142 import { createSalesChannelWorkflow } from "../workflows/create-sales-channel"
143
144 export default async function handleUserCreated({
145 event: { data },
146 container,
147 }: SubscriberArgs<{ id: string }>) {
148 const { result } = await createSalesChannelWorkflow(container)
149 .run()
150
151 console.log(result)
152 }
153
154 export const config: SubscriberConfig = {
155 event: "user.created",
156 }
157 ```
158
159 </CodeTab>
160 <CodeTab label="Scheduled Job" value="scheduled-job">
161
162 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
163 import { MedusaContainer } from "@medusajs/framework/types"
164 import { createSalesChannelWorkflow } from "../workflows/create-sales-channel"
165
166 export default async function myCustomJob(
167 container: MedusaContainer
168 ) {
169 const { result } = await createSalesChannelWorkflow(container)
170 .run()
171
172 console.log(result)
173 }
174
175 export const config = {
176 name: "run-once-a-day",
177 schedule: `0 0 * * *`,
178 }
179 ```
180
181 </CodeTab>
182 </CodeTabs>
183
184 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
185
186 ---
187
188 <CommerceModuleSections />