Raw
1 ---
2 sidebar_label: "Shipping Options"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Shipping Option 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 shipping option hooks retrieve shipping options from the Medusa backend, either a single option by ID or a paginated list.
20
21 ```tsx
22 import {
23 shippingOptionsQueryKeys,
24 useShippingOption,
25 useShippingOptions,
26 } from "@medusajs/dashboard/hooks"
27 ```
28
29 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).
30
31 ---
32
33 ## useShippingOption
34
35 Retrieve a single shipping option by its ID. Pass a query object as the second parameter to select the fields and relations to retrieve.
36
37 ```tsx
38 const { shipping_option, isPending } = useShippingOption(
39 "so_123"
40 )
41 ```
42
43 ### Parameters
44
45 <TypeList
46 types={[
47 {
48 name: "id",
49 type: "`string`",
50 optional: false,
51 description: "The shipping option's ID.",
52 },
53 {
54 name: "query",
55 type: "`Record<string, any>`",
56 optional: true,
57 description: "Optional query parameters passed to the request, such as `fields`.",
58 },
59 {
60 name: "options",
61 type: "`object`",
62 optional: true,
63 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
64 },
65 ]}
66 />
67
68 <Note>
69
70 The query key of this hook only includes the shipping option's ID. If you use different `query` values for the same ID, set a custom `queryKey` through a separate query instead.
71
72 </Note>
73
74 ### Returns
75
76 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.
77
78 <TypeList
79 types={[
80 {
81 name: "shipping_option",
82 type: "`AdminShippingOption`",
83 description: "The retrieved shipping option. It's `undefined` until the request resolves.",
84 },
85 ]}
86 />
87
88 ---
89
90 ## useShippingOptions
91
92 Retrieve a paginated list of shipping options. Pass query parameters to filter the shipping options, for example by stock location or shipping profile.
93
94 ```tsx
95 const { shipping_options, count } = useShippingOptions({
96 limit: 20,
97 })
98 ```
99
100 ### Parameters
101
102 <TypeList
103 types={[
104 {
105 name: "query",
106 type: "`AdminShippingOptionListParams`",
107 optional: true,
108 description: "Optional filters and pagination parameters, such as `limit`, `offset`, and `stock_location_id`.",
109 },
110 {
111 name: "options",
112 type: "`object`",
113 optional: true,
114 description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.",
115 },
116 ]}
117 />
118
119 ### Returns
120
121 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.
122
123 <TypeList
124 types={[
125 {
126 name: "shipping_options",
127 type: "`AdminShippingOption[]`",
128 description: "The list of retrieved shipping options.",
129 },
130 {
131 name: "count",
132 type: "`number`",
133 description: "The total number of shipping options matching the query.",
134 },
135 {
136 name: "limit",
137 type: "`number`",
138 description: "The maximum number of shipping options returned.",
139 },
140 {
141 name: "offset",
142 type: "`number`",
143 description: "The number of shipping options skipped before the returned ones.",
144 },
145 ]}
146 />
147
148 ---
149
150 ## shippingOptionsQueryKeys
151
152 `shippingOptionsQueryKeys` is the query key factory for shipping option queries. Use `all`, `lists()`, `list(query)`, `details()`, and `detail(id)` to build the keys that the hooks above register.
153
154 Use it to invalidate cached shipping option data after a custom mutation:
155
156 ```tsx
157 import { useQueryClient } from "@tanstack/react-query"
158 import {
159 shippingOptionsQueryKeys,
160 } from "@medusajs/dashboard/hooks"
161
162 const queryClient = useQueryClient()
163
164 queryClient.invalidateQueries({
165 queryKey: shippingOptionsQueryKeys.all,
166 })
167 ```