1 ---
2 sidebar_label: "Sales Channels"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Sales Channel Hooks - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 These hooks are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The sales channel hooks retrieve sales channels from the Medusa backend and manage the products linked to a sales channel.
20
21 ```tsx
22 import {
23 salesChannelsQueryKeys,
24 useSalesChannel,
25 useSalesChannelAddProducts,
26 useSalesChannelRemoveProducts,
27 useSalesChannels,
28 } from "@medusajs/dashboard/hooks"
29 ```
30
31 These hooks are built on [TanStack Query](https://tanstack.com/query/latest). Use them in your [widgets](!docs!/learn/fundamentals/admin/widgets) and [UI routes](!docs!/learn/fundamentals/admin/ui-routes).
32
33 ---
34
35 ## useSalesChannel
36
37 Retrieve a single sales channel by its ID. The hook returns the sales channel along with the TanStack Query result properties.
38
39 ```tsx
40 const { sales_channel, isPending } = useSalesChannel(
41 "sc_123"
42 )
43 ```
44
45 ### Parameters
46
47 <TypeList
48 types={[
49 {
50 name: "id",
51 type: "`string`",
52 optional: false,
53 description: "The sales channel's ID.",
54 },
55 {
56 name: "options",
57 type: "`object`",
58 optional: true,
59 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
60 },
61 ]}
62 />
63
64 ### Returns
65
66 Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result.
67
68 <TypeList
69 types={[
70 {
71 name: "sales_channel",
72 type: "`AdminSalesChannel`",
73 description: "The retrieved sales channel. It's `undefined` until the request resolves.",
74 },
75 ]}
76 />
77
78 ---
79
80 ## useSalesChannels
81
82 Retrieve a paginated list of sales channels. Pass query parameters to filter or paginate the returned sales channels.
83
84 ```tsx
85 const { sales_channels, count } = useSalesChannels({
86 limit: 20,
87 })
88 ```
89
90 ### Parameters
91
92 <TypeList
93 types={[
94 {
95 name: "query",
96 type: "`AdminSalesChannelListParams`",
97 optional: true,
98 description: "Optional filters and pagination parameters, such as `limit`, `offset`, and `q`.",
99 },
100 {
101 name: "options",
102 type: "`object`",
103 optional: true,
104 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
105 },
106 ]}
107 />
108
109 ### Returns
110
111 Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result.
112
113 <TypeList
114 types={[
115 {
116 name: "sales_channels",
117 type: "`AdminSalesChannel[]`",
118 description: "The list of retrieved sales channels.",
119 },
120 {
121 name: "count",
122 type: "`number`",
123 description: "The total number of sales channels matching the query.",
124 },
125 {
126 name: "limit",
127 type: "`number`",
128 description: "The maximum number of sales channels returned.",
129 },
130 {
131 name: "offset",
132 type: "`number`",
133 description: "The number of sales channels skipped before the returned ones.",
134 },
135 ]}
136 />
137
138 ---
139
140 ## useSalesChannelAddProducts
141
142 Add products to a sales channel. Pass the sales channel's ID to the hook, then pass an array of product IDs to `mutate` or `mutateAsync`.
143
144 ```tsx
145 const { mutate } = useSalesChannelAddProducts("sc_123")
146
147 const handleAdd = () => {
148 mutate(["prod_123"])
149 }
150 ```
151
152 ### Parameters
153
154 <TypeList
155 types={[
156 {
157 name: "id",
158 type: "`string`",
159 optional: false,
160 description: "The ID of the sales channel to add the products to.",
161 },
162 {
163 name: "options",
164 type: "`UseMutationOptions`",
165 optional: true,
166 description: "Optional TanStack mutation options, such as `onSuccess` and `onError`.",
167 },
168 ]}
169 />
170
171 ### Returns
172
173 Along with the following, the hook returns the rest of TanStack Query's [`useMutation`](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation) result.
174
175 <TypeList
176 types={[
177 {
178 name: "mutate",
179 type: "`(productIds: string[]) => void`",
180 description: "Add the products with the specified IDs to the sales channel.",
181 },
182 {
183 name: "mutateAsync",
184 type: "`(productIds: string[]) => Promise`",
185 description: "The promise-based version of `mutate`. It resolves to `{ sales_channel }`.",
186 },
187 ]}
188 />
189
190 ---
191
192 ## useSalesChannelRemoveProducts
193
194 Remove products from a sales channel. It accepts the same parameters as `useSalesChannelAddProducts`, and the array passed to `mutate` holds the IDs of the products to remove.
195
196 ```tsx
197 const { mutate } = useSalesChannelRemoveProducts(
198 "sc_123"
199 )
200
201 const handleRemove = () => {
202 mutate(["prod_123"])
203 }
204 ```
205
206 ### Parameters
207
208 <TypeList
209 types={[
210 {
211 name: "id",
212 type: "`string`",
213 optional: false,
214 description: "The ID of the sales channel to remove the products from.",
215 },
216 {
217 name: "options",
218 type: "`UseMutationOptions`",
219 optional: true,
220 description: "Optional TanStack mutation options, such as `onSuccess` and `onError`.",
221 },
222 ]}
223 />
224
225 ### Returns
226
227 Along with the following, the hook returns the rest of TanStack Query's [`useMutation`](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation) result.
228
229 <TypeList
230 types={[
231 {
232 name: "mutate",
233 type: "`(productIds: string[]) => void`",
234 description: "Remove the products with the specified IDs from the sales channel.",
235 },
236 {
237 name: "mutateAsync",
238 type: "`(productIds: string[]) => Promise`",
239 description: "The promise-based version of `mutate`. It resolves to `{ sales_channel }`.",
240 },
241 ]}
242 />
243
244 ---
245
246 ## salesChannelsQueryKeys
247
248 `salesChannelsQueryKeys` is the query key factory for sales channel queries. Use `all`, `lists()`, `list(query)`, `details()`, and `detail(id)` to build the keys that the hooks above register.
249
250 Use it to invalidate cached sales channel data after a custom mutation:
251
252 ```tsx
253 import { useQueryClient } from "@tanstack/react-query"
254 import {
255 salesChannelsQueryKeys,
256 } from "@medusajs/dashboard/hooks"
257
258 const queryClient = useQueryClient()
259
260 queryClient.invalidateQueries({
261 queryKey: salesChannelsQueryKeys.detail("sc_123"),
262 })
263 ```