1 ---
2 products:
3 - api key
4 - cart
5 - order
6 - product
7 - stock location
8 ---
9
10 import { CodeTabs, CodeTab, Table } from "docs-ui"
11
12 export const metadata = {
13 title: `Links between Sales Channel Module and Other Modules`,
14 }
15
16 # {metadata.title}
17
18 This document showcases the module links defined between the Sales Channel Module and other Commerce Modules.
19
20 ## Summary
21
22 The Sales Channel Module has the following links to other modules:
23
24 <Note title="Tip">
25
26 Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database.
27
28 </Note>
29
30 <Table>
31 <Table.Header>
32 <Table.Row>
33 <Table.HeaderCell>
34 First Data Model
35 </Table.HeaderCell>
36 <Table.HeaderCell>
37 Second Data Model
38 </Table.HeaderCell>
39 <Table.HeaderCell>
40 Type
41 </Table.HeaderCell>
42 <Table.HeaderCell>
43 Description
44 </Table.HeaderCell>
45 </Table.Row>
46 </Table.Header>
47 <Table.Body>
48 <Table.Row>
49 <Table.Cell>
50 [ApiKey](/references/api-key/models/ApiKey) in [API Key Module](../../api-key/page.mdx)
51 </Table.Cell>
52 <Table.Cell>
53 [SalesChannel](/references/sales-channel/models/SalesChannel)
54 </Table.Cell>
55 <Table.Cell>
56 Stored - many-to-many
57 </Table.Cell>
58 <Table.Cell>
59 [Learn more](#api-key-module)
60 </Table.Cell>
61 </Table.Row>
62 <Table.Row>
63 <Table.Cell>
64 [Cart](/references/cart/models/Cart) in [Cart Module](../../cart/page.mdx)
65 </Table.Cell>
66 <Table.Cell>
67 [SalesChannel](/references/sales-channel/models/SalesChannel)
68 </Table.Cell>
69 <Table.Cell>
70 Read-only - has one
71 </Table.Cell>
72 <Table.Cell>
73 [Learn more](#cart-module)
74 </Table.Cell>
75 </Table.Row>
76 <Table.Row>
77 <Table.Cell>
78 [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx)
79 </Table.Cell>
80 <Table.Cell>
81 [SalesChannel](/references/sales-channel/models/SalesChannel)
82 </Table.Cell>
83 <Table.Cell>
84 Read-only - has one
85 </Table.Cell>
86 <Table.Cell>
87 [Learn more](#order-module)
88 </Table.Cell>
89 </Table.Row>
90 <Table.Row>
91 <Table.Cell>
92 [Product](/references/product/models/Product) in [Product Module](../../product/page.mdx)
93 </Table.Cell>
94 <Table.Cell>
95 [SalesChannel](/references/sales-channel/models/SalesChannel)
96 </Table.Cell>
97 <Table.Cell>
98 Stored - many-to-many
99 </Table.Cell>
100 <Table.Cell>
101 [Learn more](#product-module)
102 </Table.Cell>
103 </Table.Row>
104 <Table.Row>
105 <Table.Cell>
106 [SalesChannel](/references/sales-channel/models/SalesChannel)
107 </Table.Cell>
108 <Table.Cell>
109 [StockLocation](/references/stock-location-next/models/StockLocation) in [Stock Location Module](../../stock-location/page.mdx)
110 </Table.Cell>
111 <Table.Cell>
112 Stored - many-to-many
113 </Table.Cell>
114 <Table.Cell>
115 [Learn more](#stock-location-module)
116 </Table.Cell>
117 </Table.Row>
118 </Table.Body>
119 </Table>
120
121 ---
122
123 ## API Key Module
124
125 A publishable API key allows you to easily specify the sales channel scope in a client request.
126
127 Medusa defines a link between the `ApiKey` and the `SalesChannel` data models.
128
129 ![A diagram showcasing an example of how resources from the Sales Channel and API Key modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1709812064/Medusa%20Resources/sales-channel-api-key_zmqi2l.jpg)
130
131 ### Retrieve with Query
132
133 To retrieve the API keys associated with a sales channel with [Query](!docs!/learn/fundamentals/query), pass `publishable_api_keys.*` in `fields`:
134
135 <CodeTabs group="relation-query">
136 <CodeTab label="query.graph" value="method">
137
138 ```ts
139 const { data: salesChannels } = await query.graph({
140 entity: "sales_channel",
141 fields: [
142 "publishable_api_keys.*",
143 ],
144 })
145
146 // salesChannels[0].publishable_api_keys
147 ```
148
149 </CodeTab>
150 <CodeTab label="useQueryGraphStep" value="step">
151
152 ```ts
153 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
154
155 // ...
156
157 const { data: salesChannels } = useQueryGraphStep({
158 entity: "sales_channel",
159 fields: [
160 "publishable_api_keys.*",
161 ],
162 })
163
164 // salesChannels[0].publishable_api_keys
165 ```
166
167 </CodeTab>
168 </CodeTabs>
169
170 ### Manage with Link
171
172 To manage the sales channels of an API key, use [Link](!docs!/learn/fundamentals/module-links/link):
173
174 <CodeTabs group="relation-link">
175 <CodeTab label="link.create" value="method">
176
177 ```ts
178 import { Modules } from "@medusajs/framework/utils"
179
180 // ...
181
182 await link.create({
183 [Modules.API_KEY]: {
184 publishable_key_id: "apk_123",
185 },
186 [Modules.SALES_CHANNEL]: {
187 sales_channel_id: "sc_123",
188 },
189 })
190 ```
191
192 </CodeTab>
193 <CodeTab label="createRemoteLinkStep" value="step">
194
195 ```ts
196 import { Modules } from "@medusajs/framework/utils"
197 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
198
199 // ...
200
201 createRemoteLinkStep({
202 [Modules.API_KEY]: {
203 publishable_key_id: "apk_123",
204 },
205 [Modules.SALES_CHANNEL]: {
206 sales_channel_id: "sc_123",
207 },
208 })
209 ```
210
211 </CodeTab>
212 </CodeTabs>
213
214 ---
215
216 ## Cart Module
217
218 Medusa defines a read-only link between the [Cart Module](../../cart/page.mdx)'s `Cart` data model and the `SalesChannel` data model. Because the link is read-only from the `Cart`'s side, you can only retrieve the sales channel of a cart, and not the other way around.
219
220 ### Retrieve with Query
221
222 To retrieve the sales channel of a cart with [Query](!docs!/learn/fundamentals/query), pass `sales_channel.*` in `fields`:
223
224 <CodeTabs group="relation-query">
225 <CodeTab label="query.graph" value="method">
226
227 ```ts
228 const { data: carts } = await query.graph({
229 entity: "cart",
230 fields: [
231 "sales_channel.*",
232 ],
233 })
234
235 // carts[0].sales_channel
236 ```
237
238 </CodeTab>
239 <CodeTab label="useQueryGraphStep" value="step">
240
241 ```ts
242 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
243
244 // ...
245
246 const { data: carts } = useQueryGraphStep({
247 entity: "cart",
248 fields: [
249 "sales_channel.*",
250 ],
251 })
252
253 // carts[0].sales_channel
254 ```
255
256 </CodeTab>
257 </CodeTabs>
258
259 ---
260
261 ## Order Module
262
263 Medusa defines a read-only link between the [Order Module](../../order/page.mdx)'s `Order` data model and the `SalesChannel` data model. Because the link is read-only from the `Order`'s side, you can only retrieve the sales channel of an order, and not the other way around.
264
265 ### Retrieve with Query
266
267 To retrieve the sales channel of an order with [Query](!docs!/learn/fundamentals/query), pass `sales_channel.*` in `fields`:
268
269 <CodeTabs group="relation-query">
270 <CodeTab label="query.graph" value="method">
271
272 ```ts
273 const { data: orders } = await query.graph({
274 entity: "order",
275 fields: [
276 "sales_channel.*",
277 ],
278 })
279
280 // orders.sales_channel
281 ```
282
283 </CodeTab>
284 <CodeTab label="useQueryGraphStep" value="step">
285
286 ```ts
287 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
288
289 // ...
290
291 const { data: orders } = useQueryGraphStep({
292 entity: "order",
293 fields: [
294 "sales_channel.*",
295 ],
296 })
297
298 // orders.sales_channel
299 ```
300
301 </CodeTab>
302 </CodeTabs>
303
304 ---
305
306 ## Product Module
307
308 A product has different availability for different sales channels. Medusa defines a link between the `Product` and the `SalesChannel` data models.
309
310 ![A diagram showcasing an example of how resources from the Sales Channel and Product modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1709809833/Medusa%20Resources/product-sales-channel_t848ik.jpg)
311
312 A product can be available in more than one sales channel. You can retrieve only the products of a sales channel.
313
314 ### Retrieve with Query
315
316 To retrieve the products of a sales channel with [Query](!docs!/learn/fundamentals/query), pass `products.*` in `fields`:
317
318 <CodeTabs group="relation-query">
319 <CodeTab label="query.graph" value="method">
320
321 ```ts
322 const { data: salesChannels } = await query.graph({
323 entity: "sales_channel",
324 fields: [
325 "products.*",
326 ],
327 })
328
329 // salesChannels[0].products
330 ```
331
332 </CodeTab>
333 <CodeTab label="useQueryGraphStep" value="step">
334
335 ```ts
336 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
337
338 // ...
339
340 const { data: salesChannels } = useQueryGraphStep({
341 entity: "sales_channel",
342 fields: [
343 "products.*",
344 ],
345 })
346
347 // salesChannels[0].products
348 ```
349
350 </CodeTab>
351 </CodeTabs>
352
353 ### Manage with Link
354
355 To manage the sales channels of a product, use [Link](!docs!/learn/fundamentals/module-links/link):
356
357 <CodeTabs group="relation-link">
358 <CodeTab label="link.create" value="method">
359
360 ```ts
361 import { Modules } from "@medusajs/framework/utils"
362
363 // ...
364
365 await link.create({
366 [Modules.PRODUCT]: {
367 product_id: "prod_123",
368 },
369 [Modules.SALES_CHANNEL]: {
370 sales_channel_id: "sc_123",
371 },
372 })
373 ```
374
375 </CodeTab>
376 <CodeTab label="createRemoteLinkStep" value="step">
377
378 ```ts
379 import { Modules } from "@medusajs/framework/utils"
380 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
381
382 // ...
383
384 createRemoteLinkStep({
385 [Modules.PRODUCT]: {
386 product_id: "prod_123",
387 },
388 [Modules.SALES_CHANNEL]: {
389 sales_channel_id: "sc_123",
390 },
391 })
392 ```
393
394 </CodeTab>
395 </CodeTabs>
396
397 ---
398
399 ## Stock Location Module
400
401 A stock location is associated with a sales channel. This scopes inventory quantities associated with that stock location by the associated sales channel.
402
403 Medusa defines a link between the `SalesChannel` and `StockLocation` data models.
404
405 ![A diagram showcasing an example of how resources from the Sales Channel and Stock Location modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1716796872/Medusa%20Resources/sales-channel-location_cqrih1.jpg)
406
407 ### Retrieve with Query
408
409 To retrieve the stock locations of a sales channel with [Query](!docs!/learn/fundamentals/query), pass `stock_locations.*` in `fields`:
410
411 <CodeTabs group="relation-query">
412 <CodeTab label="query.graph" value="method">
413
414 ```ts
415 const { data: salesChannels } = await query.graph({
416 entity: "sales_channel",
417 fields: [
418 "stock_locations.*",
419 ],
420 })
421
422 // salesChannels[0].stock_locations
423 ```
424
425 </CodeTab>
426 <CodeTab label="useQueryGraphStep" value="step">
427
428 ```ts
429 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
430
431 // ...
432
433 const { data: salesChannels } = useQueryGraphStep({
434 entity: "sales_channel",
435 fields: [
436 "stock_locations.*",
437 ],
438 })
439
440 // salesChannels[0].stock_locations
441 ```
442
443 </CodeTab>
444 </CodeTabs>
445
446 ### Manage with Link
447
448 To manage the stock locations of a sales channel, use [Link](!docs!/learn/fundamentals/module-links/link):
449
450 <CodeTabs group="relation-link">
451 <CodeTab label="link.create" value="method">
452
453 ```ts
454 import { Modules } from "@medusajs/framework/utils"
455
456 // ...
457
458 await link.create({
459 [Modules.SALES_CHANNEL]: {
460 sales_channel_id: "sc_123",
461 },
462 [Modules.STOCK_LOCATION]: {
463 sales_channel_id: "sloc_123",
464 },
465 })
466 ```
467
468 </CodeTab>
469 <CodeTab label="createRemoteLinkStep" value="step">
470
471 ```ts
472 import { Modules } from "@medusajs/framework/utils"
473 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
474
475 // ...
476
477 createRemoteLinkStep({
478 [Modules.SALES_CHANNEL]: {
479 sales_channel_id: "sc_123",
480 },
481 [Modules.STOCK_LOCATION]: {
482 sales_channel_id: "sloc_123",
483 },
484 })
485 ```
486
487 </CodeTab>
488 </CodeTabs>