Raw
1 ---
2 products:
3 - sales channel
4 ---
5
6 import { CodeTabs, CodeTab, Table } from "docs-ui"
7
8 export const metadata = {
9 title: `Links between API Key Module and Other Modules`,
10 }
11
12 # {metadata.title}
13
14 This document showcases the module links defined between the API Key Module and other Commerce Modules.
15
16 ## Summary
17
18 The API Key Module has the following links to other modules:
19
20 <Table>
21 <Table.Header>
22 <Table.Row>
23 <Table.HeaderCell>
24 First Data Model
25 </Table.HeaderCell>
26 <Table.HeaderCell>
27 Second Data Model
28 </Table.HeaderCell>
29 <Table.HeaderCell>
30 Type
31 </Table.HeaderCell>
32 <Table.HeaderCell>
33 Description
34 </Table.HeaderCell>
35 </Table.Row>
36 </Table.Header>
37 <Table.Body>
38 <Table.Row>
39 <Table.Cell>
40 [ApiKey](/references/api-key/models/ApiKey)
41 </Table.Cell>
42 <Table.Cell>
43 [SalesChannel](/references/sales-channel/models/SalesChannel) in [Sales Channel Module](../../sales-channel/page.mdx)
44 </Table.Cell>
45 <Table.Cell>
46 Stored - many-to-many
47 </Table.Cell>
48 <Table.Cell>
49 [Learn more](#sales-channel-module)
50 </Table.Cell>
51 </Table.Row>
52 </Table.Body>
53 </Table>
54
55 ---
56
57 ## Sales Channel Module
58
59 You can create a publishable API key and associate it with a sales channel. Medusa defines a link between the `ApiKey` and the `SalesChannel` data models.
60
61 ![A diagram showcasing an example of how data models from the API Key and Sales Channel modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1709812064/Medusa%20Resources/sales-channel-api-key_zmqi2l.jpg)
62
63 This is useful to avoid passing the sales channel's ID as a parameter of every request, and instead pass the publishable API key in the header of any request to the Store API route.
64
65 Learn more about this in the [Sales Channel Module's documentation](../../sales-channel/publishable-api-keys/page.mdx).
66
67 ### Retrieve with Query
68
69 To retrieve the sales channels of an API key with [Query](!docs!/learn/fundamentals/query), pass `sales_channels.*` in `fields`:
70
71 <CodeTabs group="relation-query">
72 <CodeTab label="query.graph" value="method">
73
74 ```ts
75 const { data: apiKeys } = await query.graph({
76 entity: "api_key",
77 fields: [
78 "sales_channels.*",
79 ],
80 })
81
82 // apiKeys[0].sales_channels
83 ```
84
85 </CodeTab>
86 <CodeTab label="useQueryGraphStep" value="step">
87
88 ```ts
89 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
90
91 // ...
92
93 const { data: apiKeys } = useQueryGraphStep({
94 entity: "api_key",
95 fields: [
96 "sales_channels.*",
97 ],
98 })
99
100 // apiKeys[0].sales_channels
101 ```
102
103 </CodeTab>
104 </CodeTabs>
105
106 ### Manage with Link
107
108 To manage the sales channels of an API key, use [Link](!docs!/learn/fundamentals/module-links/link):
109
110 <CodeTabs group="relation-link">
111 <CodeTab label="link.create" value="method">
112
113 ```ts
114 import { Modules } from "@medusajs/framework/utils"
115
116 // ...
117
118 await link.create({
119 [Modules.API_KEY]: {
120 publishable_key_id: "apk_123",
121 },
122 [Modules.SALES_CHANNEL]: {
123 sales_channel_id: "sc_123",
124 },
125 })
126 ```
127
128 </CodeTab>
129 <CodeTab label="createRemoteLinkStep" value="step">
130
131 ```ts
132 import { Modules } from "@medusajs/framework/utils"
133 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
134
135 // ...
136
137 createRemoteLinkStep({
138 [Modules.API_KEY]: {
139 publishable_key_id: "apk_123",
140 },
141 [Modules.SALES_CHANNEL]: {
142 sales_channel_id: "sc_123",
143 },
144 })
145 ```
146
147 </CodeTab>
148 </CodeTabs>