1 import { CodeTabs, CodeTab, H1 } from "docs-ui"
2 import { Feedback } from "@/components/Feedback"
3 import SectionContainer from "@/components/Section/Container"
4 import DividedMarkdownLayout from "@/layouts/DividedMarkdown"
5 import {
6 DividedMarkdownContent,
7 DividedMarkdownCode
8 } from "@/layouts/DividedMarkdown/Sections"
9 import Section from "@/components/Section"
10 import ViewAsMarkdown from "@/components/ViewAsMarkdown"
11
12 import ClientLibraries from "./client-libraries.mdx"
13
14 <Section checkActiveOnScroll>
15
16 <SectionContainer noTopPadding={true}>
17
18 <DividedMarkdownLayout>
19
20 <DividedMarkdownContent>
21
22 <H1 className={"scroll-m-[184px] lg:scroll-m-[264px] !mb-0"} id="introduction">Medusa V2 Admin API Reference</H1>
23
24 <ViewAsMarkdown path="/admin" className="mb-1" />
25
26 This API reference includes Medusa v2's Admin APIs, which are REST APIs exposed by the Medusa application. They are used to perform admin functionalities or create an admin dashboard to access and manipulate your commerce store's data.
27
28 All API Routes are prefixed with `/admin`. So, during development, the API Routes will be available under the path `http://localhost:9000/admin`. For production, replace `http://localhost:9000` with your Medusa application URL.
29
30 <Feedback
31 extraData={{
32 section: "introduction"
33 }}
34 question="Was this section helpful?"
35 />
36
37 </DividedMarkdownContent>
38
39 <DividedMarkdownCode>
40
41 <ClientLibraries />
42
43 </DividedMarkdownCode>
44
45 </DividedMarkdownLayout>
46
47 </SectionContainer>
48
49 <SectionContainer noTopPadding={true}>
50
51 <DividedMarkdownLayout>
52
53 <DividedMarkdownContent>
54
55 ## Authentication
56
57 There are three ways to send authenticated requests to the Medusa server: Using a JWT token in a bearer authorization header, using an admin user's API token, or using a cookie session ID.
58
59 ### 1. Bearer Authorization with JWT Tokens
60
61 Use a JWT token in a request's bearer authorization header to send authenticated requests. Authentication state is managed by the client, which is ideal for Jamstack applications and mobile applications.
62
63 </DividedMarkdownContent>
64
65 </DividedMarkdownLayout>
66
67 <DividedMarkdownLayout addYSpacing>
68
69 <DividedMarkdownContent>
70
71 #### How to Obtain the JWT Token
72
73 To obtain a JWT token, send a request to the [authentication route](https://docs.medusajs.com/api/admin/auth/authenticate-user) passing it the user's email and password in the request body.
74
75 </DividedMarkdownContent>
76
77 <DividedMarkdownCode>
78
79 <CodeTabs group="request-examples">
80
81 <CodeTab label="JS SDK" value="js-sdk">
82
83 ```js title="Obtain JWT token"
84 token = await sdk.auth.login("user", "emailpass", {
85 email,
86 password
87 })
88 ```
89
90 </CodeTab>
91
92 <CodeTab label="cURL" value="curl">
93
94 ```bash title="Obtain JWT token"
95 curl -X POST '{backend_url}/auth/user/emailpass' \
96 -H 'Content-Type: application/json' \
97 --data-raw '{
98 "email": "user@example.com",
99 "password": "supersecret"
100 }'
101 ```
102
103 </CodeTab>
104
105 </CodeTabs>
106
107 </DividedMarkdownCode>
108
109 </DividedMarkdownLayout>
110
111 <DividedMarkdownLayout addYSpacing>
112
113 <DividedMarkdownContent>
114
115 If authenticated successfully, an object is returned in the response with the property `token` being the JWT token.
116
117 </DividedMarkdownContent>
118
119 <DividedMarkdownCode>
120
121 ```json title="Example response"
122 {
123 "token": "123..."
124 }
125 ```
126
127 </DividedMarkdownCode>
128
129 </DividedMarkdownLayout>
130
131 <DividedMarkdownLayout addYSpacing>
132
133 <DividedMarkdownContent>
134
135 #### How to Use the JWT Token
136
137 To use the JWT token, pass it in the authorization bearer header.
138
139 If you're using the JS SDK, the `login` method automatically sets the token for you and passes it to subsequent requests. You can also set the token manually using the `client.setToken` method.
140
141 <Note>
142
143 Make sure you've set the `auth.type` configuration of the JS SDK to `jwt` to use the JWT token. Learn more in the [JS SDK configurations](!resources!/js-sdk#js-sdk-configurations).
144
145 </Note>
146
147 <Note title="Migrating from v1?" type="warning">
148
149 The `x-medusa-access-token` header is no longer used in Medusa v2. Pass the JWT token in the `Authorization: Bearer` header instead, as explained here.
150
151 </Note>
152
153 </DividedMarkdownContent>
154
155 <DividedMarkdownCode>
156
157 ```bash title="Use JWT token"
158 Authorization: Bearer {jwt_token}
159 ```
160
161 </DividedMarkdownCode>
162
163 </DividedMarkdownLayout>
164
165 <DividedMarkdownLayout addYSpacing>
166
167 <DividedMarkdownContent>
168
169 ### 2. API Token
170
171 Use a user's secret API Token to send authenticated requests.
172
173 </DividedMarkdownContent>
174
175 </DividedMarkdownLayout>
176
177 <DividedMarkdownLayout addYSpacing>
178
179 <DividedMarkdownContent>
180
181 #### How to Create an API Token for a User
182
183 Create the API key token either from the [Medusa Admin](!user-guide!/settings/developer/secret-api-keys) or using the [Create API Key API Route](https://docs.medusajs.com/api/admin/api-keys/create-api-key).
184
185 <Note>
186
187 You must be an authenticated user to create an API token.
188
189 </Note>
190
191 An `api_key` object is returned in the response. You need its `token` property.
192
193 </DividedMarkdownContent>
194
195 <DividedMarkdownCode>
196
197 <CodeTabs group="request-examples">
198
199 <CodeTab label="JS SDK" value="js-sdk">
200
201 ```js title="Create API token"
202 const { api_key } = await sdk.admin.apiKey.create({
203 title: "My Token",
204 type: "secret"
205 })
206
207 console.log(api_key.token)
208 ```
209
210 </CodeTab>
211
212 <CodeTab label="cURL" value="curl">
213
214 ```bash title="Create API token"
215 curl -X POST 'localhost:9000/admin/api-keys' \
216 -H 'Content-Type: application/json' \
217 -H 'Authorization: Bearer {jwt_token}' \
218 --data '{
219 "title": "my token",
220 "type": "secret"
221 }'
222 ```
223
224 </CodeTab>
225
226 </CodeTabs>
227
228 </DividedMarkdownCode>
229
230 </DividedMarkdownLayout>
231
232 <DividedMarkdownLayout addYSpacing>
233
234 <DividedMarkdownContent>
235
236 #### How to Use the API Token
237
238 You pass the API Key token in the Authorization Basic header. For example:
239
240 <Note>
241
242 Previous versions of Medusa required passing the API key token as base64. This is still supported for backwards compatibility, but you can pass the API token without encoding it as well.
243
244 </Note>
245
246 </DividedMarkdownContent>
247
248 <DividedMarkdownCode>
249
250 <CodeTabs group="app-type">
251
252 <CodeTab label="JS SDK" value="js-sdk">
253
254 ```js
255 import Medusa from "@medusajs/js-sdk"
256
257 export const sdk = new Medusa({
258 // other options...
259 apiKey: "{api_key_token}",
260 })
261 ```
262
263 </CodeTab>
264 <CodeTab label="cURL" value="curl">
265
266 ```bash title="Use API token"
267 curl '{backend_url}/admin/products' \
268 -H 'Authorization: Basic {api_key_token}'
269 ```
270
271 </CodeTab>
272 </CodeTabs>
273
274 </DividedMarkdownCode>
275
276 </DividedMarkdownLayout>
277
278 <DividedMarkdownLayout addYSpacing>
279
280 <DividedMarkdownContent>
281
282 ### 3. Cookie Session ID
283
284 When you authenticate a user and create a cookie session ID for them, the cookie session ID is passed automatically when sending the request from the browser, or with tools like Postman.
285
286 ### How to Obtain the Cookie Session
287
288 To obtain a cookie session ID, you must have a [JWT token for bearer authentication](#1-bearer-authorization-with-jwt-tokens).
289
290 </DividedMarkdownContent>
291
292 </DividedMarkdownLayout>
293
294 <DividedMarkdownLayout addYSpacing>
295
296 <DividedMarkdownContent>
297
298 Then, if you're using the JS SDK, make sure the `auth.type` configuration is set to `session`, as explained in the [JS SDK configurations](!resources!/js-sdk#js-sdk-configurations) guide. The `auth.login` method will handle setting the session cookie and passing it in subsequent requests.
299
300 If you're not using the JS SDK, send a request to the [session authentication API route](https://docs.medusajs.com/api/admin/auth/set-authentication-session). To view the cookie session ID, pass the `-v` option to the `curl` command.
301
302 </DividedMarkdownContent>
303
304 <DividedMarkdownCode>
305
306 ```bash title="Obtain cookie session"
307 curl -v -X POST '{backend_url}/auth/session' \
308 --header 'Authorization: Bearer {jwt_token}'
309 ```
310
311 </DividedMarkdownCode>
312
313 </DividedMarkdownLayout>
314
315 <DividedMarkdownLayout addYSpacing>
316
317 <DividedMarkdownContent>
318
319 If you send the `cURL` request, the headers will be logged in the terminal as well as the response. You
320 should find in the headers a Cookie header.
321
322 </DividedMarkdownContent>
323
324 <DividedMarkdownCode>
325
326 ```bash title="Logged cookie session"
327 Set-Cookie: connect.sid=s%3A2Bu8B...;
328 ```
329
330 </DividedMarkdownCode>
331
332 </DividedMarkdownLayout>
333
334 <DividedMarkdownLayout addYSpacing>
335
336 <DividedMarkdownContent>
337
338 #### How to Use the Cookie Session ID in cURL
339
340 If you're using the JS SDK, it will pass the cookie session with every request automatically after you use the `auth.login` method.
341
342 If you're not using the JS SDK, copy the value after `connect.sid` (without the `;` at the end) and pass
343 it as a cookie in subsequent requests.
344
345 </DividedMarkdownContent>
346
347 <DividedMarkdownCode>
348
349 ```bash title="Use cookie session"
350 curl '{backend_url}/admin/products' \
351 -H 'Cookie: connect.sid={sid}'
352 ```
353
354 Where `{sid}` is the value of `connect.sid` that you copied.
355
356 </DividedMarkdownCode>
357
358 </DividedMarkdownLayout>
359
360 <DividedMarkdownLayout addYSpacing>
361
362 <DividedMarkdownContent>
363
364 If you're sending requests using JavaScript's Fetch API, you must pass the `credentials` option
365 with the value `include` to all the requests you're sending.
366
367 </DividedMarkdownContent>
368
369 <DividedMarkdownCode>
370
371 ```js title="Include credentials in fetch"
372 fetch(`<BACKEND_URL>/admin/products`, {
373 credentials: "include",
374 })
375 ```
376
377 </DividedMarkdownCode>
378
379 </DividedMarkdownLayout>
380
381 <DividedMarkdownLayout addYSpacing>
382
383 <Feedback
384 extraData={{
385 section: "authentication-cookie"
386 }}
387 question="Was this section helpful?"
388 />
389
390 </DividedMarkdownLayout>
391
392 </SectionContainer>
393
394 <SectionContainer noTopPadding={true}>
395
396 <DividedMarkdownLayout>
397
398 <DividedMarkdownContent>
399
400 ## HTTP Compression
401
402 </DividedMarkdownContent>
403
404 </DividedMarkdownLayout>
405
406 <DividedMarkdownLayout addYSpacing>
407
408 <DividedMarkdownContent>
409
410 If you've enabled HTTP Compression in your Medusa configurations, and you
411 want to disable it for some requests, you can pass the `x-no-compression`
412 header in your requests.
413
414 If you're using the JS SDK, every method accepts a `headers` parameter as the last parameter. You can pass in it custom headers, including the `x-no-compression` header.
415
416 </DividedMarkdownContent>
417
418 <DividedMarkdownCode>
419
420 <CodeTabs group="request-examples">
421
422 <CodeTab label="JS SDK" value="js-sdk">
423
424 ```js title="Disable HTTP compression"
425 sdk.store.product.list({
426 limit,
427 offset,
428 }, {
429 "x-no-compression": "false",
430 })
431 ```
432
433 </CodeTab>
434
435 <CodeTab label="cURL" value="curl">
436
437 ```bash title="Disable HTTP compression"
438 x-no-compression: false
439 ```
440
441 </CodeTab>
442
443 </CodeTabs>
444
445 </DividedMarkdownCode>
446
447 </DividedMarkdownLayout>
448
449 <DividedMarkdownLayout addYSpacing>
450
451 <Feedback
452 extraData={{
453 section: "http-compression"
454 }}
455 question="Was this section helpful?"
456 />
457
458 </DividedMarkdownLayout>
459
460 </SectionContainer>
461
462 <SectionContainer noTopPadding={true}>
463
464 <DividedMarkdownLayout>
465
466 <DividedMarkdownContent>
467
468 ## Manage Metadata
469
470 Many data models in Medusa, such as products and carts, have a `metadata` field that allows you to store custom information in key-value pairs.
471
472 When setting or updating the `metadata` field using the relevant API routes, Medusa will merge the new metadata with the existing metadata.
473
474 <Note>
475
476 The instructions in this section apply to any [JSON property in a data model](!docs!/learn/fundamentals/data-models/json-properties).
477
478 </Note>
479
480 </DividedMarkdownContent>
481
482 </DividedMarkdownLayout>
483
484 <DividedMarkdownLayout addYSpacing>
485
486 <DividedMarkdownContent>
487
488 ### Accepted Values in Metadata
489
490 The `metadata` is an object of key-value pairs, where the keys are strings and the values can be one of the following types:
491
492 - String
493 - An empty string deletes the property from the metadata.
494 - Number
495 - Boolean
496 - Date
497 - Object
498 - Arrays of any of the above types
499
500 The `metadata` is not validated, so you can store any custom data in it.
501
502 </DividedMarkdownContent>
503
504 <DividedMarkdownCode>
505
506 ```ts title="Metadata Example"
507 {
508 "metadata": {
509 "category": "electronics",
510 "views": 1500,
511 "is_featured": true,
512 "tags": ["new", "sale"],
513 "details": {
514 "warranty": "2 years",
515 "origin": "USA"
516 }
517 }
518 }
519 ```
520
521 </DividedMarkdownCode>
522
523 </DividedMarkdownLayout>
524
525 <DividedMarkdownLayout addYSpacing>
526
527 <DividedMarkdownContent>
528
529 ### Add or Update New Property in Metadata
530
531 To add or update a property in the `metadata`, pass the property in the request body as a key-value pair. This won't affect existing properties in the metadata.
532
533 </DividedMarkdownContent>
534
535 <DividedMarkdownCode>
536
537 <CodeTabs group="request-with-result">
538
539 <CodeTab label="JS SDK" value="js-sdk">
540
541 ```js title="Add new metadata property"
542 sdk.admin.product.update("prod_123", {
543 metadata: {
544 new_property: "value"
545 }
546 })
547 ```
548
549 </CodeTab>
550
551 <CodeTab label="Result" value="result">
552
553 ```json title="Result"
554 {
555 "id": "prod_123",
556 "metadata": {
557 "new_property": "value",
558 "old_property": "value"
559 }
560 }
561 ```
562
563 </CodeTab>
564
565 </CodeTabs>
566
567 </DividedMarkdownCode>
568
569 </DividedMarkdownLayout>
570
571 <DividedMarkdownLayout addYSpacing>
572
573 <DividedMarkdownContent>
574
575 ### Update Nested Objects in Metadata
576
577 When updating a nested object in the `metadata`, you must pass the entire object in the request body.
578
579 Medusa doesn't merge nested objects, so if you pass a partial object, the existing properties in the nested object will be removed.
580
581 </DividedMarkdownContent>
582
583 <DividedMarkdownCode>
584
585 <CodeTabs group="request-with-result">
586
587 <CodeTab label="JS SDK" value="js-sdk">
588
589 ```js title="Update nested object in metadata"
590 sdk.admin.product.update("prod_123", {
591 metadata: {
592 nested_object: {
593 property1: "value1",
594 property2: "value2"
595 }
596 }
597 })
598 ```
599
600 </CodeTab>
601
602 <CodeTab label="Result" value="result">
603
604 ```json title="Result"
605 {
606 "id": "prod_123",
607 "metadata": {
608 "nested_object": {
609 "property1": "value1",
610 "property2": "value2"
611 }
612 }
613 }
614 ```
615
616 </CodeTab>
617
618 </CodeTabs>
619
620 </DividedMarkdownCode>
621
622 </DividedMarkdownLayout>
623
624 <DividedMarkdownLayout addYSpacing>
625
626 <DividedMarkdownContent>
627
628 ### Remove Property from Metadata
629
630 To remove a property from the `metadata`, pass the property in the request body with an empty string value. This will remove the property from the `metadata` without affecting other properties.
631
632 <Feedback
633 extraData={{
634 section: "metadata"
635 }}
636 question="Was this section helpful?"
637 />
638
639 </DividedMarkdownContent>
640
641 <DividedMarkdownCode>
642
643 <CodeTabs group="request-with-result">
644
645 <CodeTab label="JS SDK" value="js-sdk">
646
647 ```js title="Remove metadata property"
648 sdk.admin.product.update("prod_123", {
649 metadata: {
650 property_to_remove: ""
651 }
652 })
653 ```
654
655 </CodeTab>
656
657 <CodeTab label="Result" value="result">
658
659 ```json title="Result"
660 {
661 "id": "prod_123",
662 "metadata": {
663 "other_property": "value"
664 }
665 }
666 ```
667
668 </CodeTab>
669
670 </CodeTabs>
671
672 </DividedMarkdownCode>
673
674 </DividedMarkdownLayout>
675
676 </SectionContainer>
677
678 <SectionContainer noTopPadding={true}>
679
680 <DividedMarkdownLayout>
681
682 <DividedMarkdownContent>
683
684 ## Select Fields and Relations
685
686 </DividedMarkdownContent>
687
688 </DividedMarkdownLayout>
689
690 <DividedMarkdownLayout addYSpacing>
691
692 <DividedMarkdownContent>
693
694 Many API Routes accept a `fields` query that allows you to select which fields and relations should be returned in a record.
695 Fields and relations are separated by a comma `,`.
696
697 </DividedMarkdownContent>
698
699 <DividedMarkdownCode>
700
701 <CodeTabs group="request-examples">
702
703 <CodeTab label="JS SDK" value="js-sdk">
704
705 ```js title="Select fields"
706 sdk.admin.product.list({
707 fields: "title,handle"
708 })
709 ```
710
711 </CodeTab>
712
713 <CodeTab label="cURL" value="curl">
714
715 ```bash title="Select fields"
716 curl 'localhost:9000/admin/products?fields=title,handle' \
717 -H 'Authorization: Bearer {jwt_token}'
718 ```
719
720 </CodeTab>
721
722 </CodeTabs>
723
724 This returns only the `title` and `handle` fields of a product.
725
726 </DividedMarkdownCode>
727
728 </DividedMarkdownLayout>
729
730 <DividedMarkdownLayout>
731
732 <DividedMarkdownContent>
733
734 ### Allowed and Disallowed Fields
735
736 An API route can restrict the fields and relations that you can retrieve:
737
738 - Allowed fields and relations are the only ones you can retrieve in the API route. If an API route has no allowed fields, you can retrieve any field or relation.
739 - Disallowed fields and relations are the ones you can never retrieve in the API route, even if you pass them in the `fields` query parameter.
740
741 If an API route has any of these restrictions, they're listed in its documentation in the Allowed and Disallowed collapsibles.
742
743 To learn more about these restrictions and how to override them, refer to the [Allowed Fields](!docs!/learn/fundamentals/api-routes/allowed-fields) and [Disallowed Fields](!docs!/learn/fundamentals/api-routes/disallowed-fields) documentation.
744
745 </DividedMarkdownContent>
746
747 </DividedMarkdownLayout>
748
749 <DividedMarkdownLayout addYSpacing>
750
751 <DividedMarkdownContent>
752
753 ### Fields Operator
754
755 By default, only the selected fields and relations are returned in the response.
756
757 Before every field or relation, you can pass one of the following operators to change the default behavior:
758
759 - `+`: Add the field to the fields returned by default. For example, `+title` returns the `title` field along with the fields returned by default.
760 - `-`: Remove the field from the fields returned by default. For example, `-handle` removes the `handle` field from the fields returned by default.
761
762 </DividedMarkdownContent>
763
764 <DividedMarkdownCode>
765
766 <CodeTabs group="request-examples">
767
768 <CodeTab label="JS SDK" value="js-sdk">
769
770 ```js title="Select relations"
771 sdk.admin.product.list({
772 fields: "+title,-handle"
773 })
774 ```
775
776 </CodeTab>
777
778 <CodeTab label="cURL" value="curl">
779
780 ```bash title="Select relations"
781 curl 'localhost:9000/admin/products?fields=+title,-handle'
782 ```
783
784 </CodeTab>
785
786 </CodeTabs>
787
788 This returns the products, each of them having their `title` field, but without the `handle` field.
789
790 </DividedMarkdownCode>
791
792 </DividedMarkdownLayout>
793
794 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
795
796 <DividedMarkdownContent>
797
798 ### Select Relations
799
800 To select a relation, pass to `fields` the relation name prefixed by `*`.
801
802 </DividedMarkdownContent>
803
804 <DividedMarkdownCode>
805
806 <CodeTabs group="request-examples">
807
808 <CodeTab label="JS SDK" value="js-sdk">
809
810 ```js title="Select relations"
811 sdk.admin.product.list({
812 fields: "*variants"
813 })
814 ```
815
816 </CodeTab>
817
818 <CodeTab label="cURL" value="curl">
819
820 ```bash title="Select relations"
821 curl 'localhost:9000/admin/products?fields=*variants' \
822 -H 'Authorization: Bearer {jwt_token}'
823 ```
824
825 </CodeTab>
826 </CodeTabs>
827
828 This returns the variants of each product.
829
830 </DividedMarkdownCode>
831
832 </DividedMarkdownLayout>
833
834 <DividedMarkdownLayout addYSpacing>
835
836 <DividedMarkdownContent>
837
838 To select multiple relations, pass each of the relations with the `*` prefix, separated by a comma.
839
840 </DividedMarkdownContent>
841
842 <DividedMarkdownCode>
843
844 <CodeTabs group="request-examples">
845
846 <CodeTab label="JS SDK" value="js-sdk">
847
848 ```js title="Select relation's fields"
849 sdk.admin.product.list({
850 fields: "*variants,*options"
851 })
852 ```
853
854 </CodeTab>
855
856 <CodeTab label="cURL" value="curl">
857
858 ```bash title="Select relation's fields"
859 curl 'localhost:9000/admin/products?fields=*variants,*options'
860 ```
861
862 </CodeTab>
863
864 </CodeTabs>
865
866 This returns the variants and options of each product.
867
868 </DividedMarkdownCode>
869
870 </DividedMarkdownLayout>
871
872 <DividedMarkdownLayout addYSpacing>
873
874 <DividedMarkdownContent>
875
876 ### Select Fields in a Relation
877
878 The `*` prefix selects all fields of the relation's data model.
879
880 </DividedMarkdownContent>
881
882 </DividedMarkdownLayout>
883
884 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
885
886 <DividedMarkdownContent>
887
888 To select a specific field, pass a `.<field>` suffix instead of the `*` prefix. For example, `variants.title`.
889
890 To specify multiple fields, pass each of the fields with the `<relation>.<field>` format, separated by a comma. You can do the same for multiple relations.
891
892 </DividedMarkdownContent>
893
894 <DividedMarkdownCode>
895
896 <CodeTabs group="request-examples">
897
898 <CodeTab label="JS SDK" value="js-sdk">
899
900 ```js title="Select relation's fields"
901 sdk.admin.product.list({
902 fields: "variants.title,variants.sku,options.title"
903 })
904 ```
905
906 </CodeTab>
907
908 <CodeTab label="cURL" value="curl">
909
910 ```bash title="Select relation's fields"
911 curl 'localhost:9000/admin/products?fields=variants.title,variants.sku,options.title' \
912 -H 'Authorization: Bearer {jwt_token}'
913 ```
914
915 </CodeTab>
916
917 </CodeTabs>
918
919 This returns the variants and options of each product, but the variants only have their `id`, `title`, and `sku` fields, and the options only have their `id` and `title` fields. The `id` is always included.
920
921 </DividedMarkdownCode>
922
923 </DividedMarkdownLayout>
924
925 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
926
927 <DividedMarkdownContent>
928
929 ### Select Nested Relations
930
931 A relation can have relations of its own. To select a nested relation, chain the relation names with a dot (`.`), prefixing the deepest relation with `*` to select all its fields. For example, `*variants.prices`.
932
933 </DividedMarkdownContent>
934
935 </DividedMarkdownLayout>
936
937 <DividedMarkdownLayout addYSpacing>
938
939 <DividedMarkdownContent>
940
941 Prefixing a relation with `*` only selects that relation's own fields, not its nested relations. So, to retrieve a nested relation, you must reference it explicitly through its parent, as shown in the example.
942
943 You can chain more relations to select deeper levels, such as `*items.variant.product`.
944
945 </DividedMarkdownContent>
946
947 <DividedMarkdownCode>
948
949 <CodeTabs group="request-examples">
950
951 <CodeTab label="JS SDK" value="js-sdk">
952
953 ```js title="Select nested relations"
954 sdk.admin.product.list({
955 fields: "*variants.prices"
956 })
957 ```
958
959 </CodeTab>
960
961 <CodeTab label="cURL" value="curl">
962
963 ```bash title="Select nested relations"
964 curl 'localhost:9000/admin/products?fields=*variants.prices' \
965 -H 'Authorization: Bearer {jwt_token}'
966 ```
967
968 </CodeTab>
969
970 </CodeTabs>
971
972 This returns the variants of each product, each with its `prices` relation populated.
973
974 </DividedMarkdownCode>
975
976 </DividedMarkdownLayout>
977
978 <DividedMarkdownLayout>
979
980 <DividedMarkdownContent>
981
982 ### Select Custom Linked Data Models
983
984 Most of the API routes that accept a `fields` query parameter allow you to specify custom linked data models. For example, if you linked a `Brand` to a `Product`, you can pass `brand` in the `fields` query parameter to retrieve the brand of each product.
985
986 However, some API routes restrict the fields and relations you can retrieve. To learn about those API routes and how to override the allowed fields and relations, refer to the [Retrieve Custom Linked Data Models from Medusa's API routes](!docs!/learn/fundamentals/api-routes/retrieve-custom-links) documentation.
987
988 </DividedMarkdownContent>
989
990 </DividedMarkdownLayout>
991
992 <DividedMarkdownLayout>
993
994 <Feedback
995 extraData={{
996 section: "select-fields"
997 }}
998 question="Was this section helpful?"
999 />
1000
1001 </DividedMarkdownLayout>
1002
1003 </SectionContainer>
1004
1005 <SectionContainer noTopPadding={true}>
1006
1007 <DividedMarkdownLayout>
1008
1009 <DividedMarkdownContent>
1010
1011 ## Query Parameter Types
1012
1013
1014 This section covers how to pass some common data types as query parameters.
1015
1016 This is useful if you're sending requests to the API Routes using cURL or Postman.
1017
1018 </DividedMarkdownContent>
1019
1020 </DividedMarkdownLayout>
1021
1022 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1023
1024 <DividedMarkdownContent>
1025
1026 ### Strings
1027
1028
1029 You can pass a string value in the form of `<parameter_name>=<value>`.
1030
1031 </DividedMarkdownContent>
1032
1033 <DividedMarkdownCode>
1034
1035 <CodeTabs group="request-examples">
1036
1037 <CodeTab label="JS SDK" value="js-sdk">
1038
1039 ```js title="String filter"
1040 sdk.admin.product.list({
1041 title: "Shirt"
1042 })
1043 ```
1044
1045 </CodeTab>
1046
1047 <CodeTab label="cURL" value="curl">
1048
1049 ```bash title="String filter"
1050 curl "http://localhost:9000/admin/products?title=Shirt" \
1051 -H 'Authorization: Bearer {jwt_token}'
1052 ```
1053
1054 </CodeTab>
1055
1056 </CodeTabs>
1057
1058 </DividedMarkdownCode>
1059
1060 </DividedMarkdownLayout>
1061
1062 <DividedMarkdownLayout addYSpacing>
1063
1064 <DividedMarkdownContent>
1065
1066 If the string has any characters other than letters and numbers, you must
1067 encode them. For example, if the string has spaces, you can encode the space with `+` or
1068 `%20`.
1069
1070 When using the JS SDK, you can pass the string directly to the query parameter. The JS SDK will encode the string for you.
1071
1072 You can use tools like [this one](https://www.urlencoder.org/) to learn how
1073 a value can be encoded.
1074
1075 </DividedMarkdownContent>
1076
1077 <DividedMarkdownCode>
1078
1079 <CodeTabs group="request-examples">
1080
1081 <CodeTab label="JS SDK" value="js-sdk">
1082
1083 ```js title="Encoded string filter"
1084 sdk.admin.product.list({
1085 title: "Blue Shirt"
1086 })
1087 ```
1088
1089 </CodeTab>
1090
1091 <CodeTab label="cURL" value="curl">
1092
1093 ```bash title="Encoded string filter"
1094 curl "http://localhost:9000/admin/products?title=Blue%20Shirt" \
1095 -H 'Authorization: Bearer {jwt_token}'
1096 ```
1097
1098 </CodeTab>
1099
1100 </CodeTabs>
1101
1102 </DividedMarkdownCode>
1103
1104 </DividedMarkdownLayout>
1105
1106 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1107
1108 <DividedMarkdownContent>
1109
1110 ### Integers
1111
1112 You can pass an integer value in the form of `<parameter_name>=<value>`.
1113
1114 </DividedMarkdownContent>
1115
1116 <DividedMarkdownCode>
1117
1118 <CodeTabs group="request-examples">
1119
1120 <CodeTab label="JS SDK" value="js-sdk">
1121
1122 ```js title="Integer filter"
1123 sdk.admin.product.list({
1124 offset: 1
1125 })
1126 ```
1127
1128 </CodeTab>
1129
1130 <CodeTab label="cURL" value="curl">
1131
1132 ```bash title="Integer filter"
1133 curl "http://localhost:9000/admin/products?offset=1" \
1134 -H 'Authorization: Bearer {jwt_token}'
1135 ```
1136
1137 </CodeTab>
1138
1139 </CodeTabs>
1140
1141 </DividedMarkdownCode>
1142
1143 </DividedMarkdownLayout>
1144
1145 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1146
1147 <DividedMarkdownContent>
1148
1149 ### Boolean
1150
1151
1152 You can pass a boolean value in the form of `<parameter_name>=<value>`.
1153
1154 </DividedMarkdownContent>
1155
1156 <DividedMarkdownCode>
1157
1158 <CodeTabs group="request-examples">
1159
1160 <CodeTab label="JS SDK" value="js-sdk">
1161
1162 ```js title="Boolean filter"
1163 sdk.admin.product.list({
1164 is_giftcard: true
1165 })
1166 ```
1167
1168 </CodeTab>
1169
1170 <CodeTab label="cURL" value="curl">
1171
1172 ```bash title="Boolean filter"
1173 curl "http://localhost:9000/admin/products?is_giftcard=true" \
1174 -H 'Authorization: Bearer {jwt_token}'
1175 ```
1176
1177 </CodeTab>
1178 </CodeTabs>
1179
1180 </DividedMarkdownCode>
1181
1182 </DividedMarkdownLayout>
1183
1184 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1185
1186 <DividedMarkdownContent>
1187
1188 ### Date and DateTime
1189
1190
1191 You can pass a date value in the form `<parameter_name>=<value>`. The date
1192 must be in the format `YYYY-MM-DD`.
1193
1194 </DividedMarkdownContent>
1195
1196 <DividedMarkdownCode>
1197
1198 <CodeTabs group="request-examples">
1199
1200 <CodeTab label="JS SDK" value="js-sdk">
1201
1202 ```js title="Date filter"
1203 sdk.admin.product.list({
1204 created_at: { $lt: "2023-02-17" }
1205 })
1206 ```
1207
1208 </CodeTab>
1209
1210 <CodeTab label="cURL" value="curl">
1211
1212 ```bash title="Date filter"
1213 curl -g "http://localhost:9000/admin/products?created_at[$lt]=2023-02-17" \
1214 -H 'Authorization: Bearer {jwt_token}'
1215 ```
1216
1217 </CodeTab>
1218
1219 </CodeTabs>
1220
1221 </DividedMarkdownCode>
1222
1223 </DividedMarkdownLayout>
1224
1225 <DividedMarkdownLayout addYSpacing>
1226
1227 <DividedMarkdownContent>
1228
1229 You can also pass the time using the format `YYYY-MM-DDTHH:MM:SSZ`. Please
1230 note that the `T` and `Z` here are fixed.
1231
1232 </DividedMarkdownContent>
1233
1234 <DividedMarkdownCode>
1235
1236 <CodeTabs group="request-examples">
1237
1238 <CodeTab label="JS SDK" value="js-sdk">
1239
1240 ```js title="Date and time filter"
1241 sdk.admin.product.list({
1242 created_at: { $lt: "2023-02-17T07:22:30Z" }
1243 })
1244 ```
1245
1246 </CodeTab>
1247
1248 <CodeTab label="cURL" value="curl">
1249
1250 ```bash title="Date and time filter"
1251 curl -g "http://localhost:9000/admin/products?created_at[$lt]=2023-02-17T07:22:30Z" \
1252 -H 'Authorization: Bearer {jwt_token}'
1253 ```
1254
1255 </CodeTab>
1256
1257 </CodeTabs>
1258
1259 </DividedMarkdownCode>
1260
1261 </DividedMarkdownLayout>
1262
1263 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1264
1265 <DividedMarkdownContent>
1266
1267 ### Array
1268
1269 Array filters can be passed either as:
1270
1271 - `<parameter_name>[]=<value1>,<value2>`, separating the values by a comma.
1272 - `<parameter_name>[]=<value1>&<parameter_name>[]=<value2>`, passing each value as a separate query parameter. You can also specify the index of each
1273 parameter in the brackets `<parameter_name>[0]=<value>`.
1274
1275 When using the JS SDK, you can pass the array directly to the query parameter. The JS SDK will handle the rest.
1276
1277 </DividedMarkdownContent>
1278
1279 <DividedMarkdownCode>
1280
1281 <CodeTabs group="array-filter">
1282 <CodeTab label="JS SDK" value="js-sdk">
1283
1284 ```js title="Array filter"
1285 sdk.admin.product.list({
1286 sales_channel_id: ["sc_123", "sc_456"]
1287 })
1288 ```
1289
1290 </CodeTab>
1291 <CodeTab label="Comma-separated" value="comma-separated">
1292
1293 ```bash
1294 curl -g "http://localhost:9000/admin/products?sales_channel_id[]=sc_123,sc_456" \
1295 -H 'Authorization: Bearer {jwt_token}'
1296 ```
1297
1298 </CodeTab>
1299 <CodeTab label="Separate parameters" value="separate-query-parameters">
1300
1301 ```bash
1302 curl -g "http://localhost:9000/admin/products?sales_channel_id[]=sc_123&sales_channel_id[]=sc_456" \
1303 -H 'Authorization: Bearer {jwt_token}'
1304 ```
1305
1306 </CodeTab>
1307 </CodeTabs>
1308
1309 Note that the `-g` parameter passed to `curl` disables errors being thrown
1310 for using the brackets. Read more
1311 [here](https://curl.se/docs/manpage.html#-g).
1312
1313 </DividedMarkdownCode>
1314
1315 </DividedMarkdownLayout>
1316
1317 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1318
1319 <DividedMarkdownContent>
1320
1321 ### Object
1322
1323
1324 Object parameters must be passed as separate query parameters in the form
1325 `<parameter_name>[<key>]=<value>`.
1326
1327 When using the JS SDK, you can pass the object directly to the query parameter. The JS SDK will handle the rest.
1328
1329 </DividedMarkdownContent>
1330
1331 <DividedMarkdownCode>
1332
1333 <CodeTabs group="request-examples">
1334
1335 <CodeTab label="JS SDK" value="js-sdk">
1336
1337 ```js title="Object filter"
1338 sdk.admin.product.list({
1339 created_at: { $lt: "2023-02-17", $gt: "2022-09-17" }
1340 })
1341 ```
1342
1343 </CodeTab>
1344
1345 <CodeTab label="cURL" value="curl">
1346
1347 ```bash title="Object filter"
1348 curl -g "http://localhost:9000/admin/products?created_at[$lt]=2023-02-17&created_at[$gt]=2022-09-17" \
1349 -H 'Authorization: Bearer {jwt_token}'
1350 ```
1351
1352 </CodeTab>
1353
1354 </CodeTabs>
1355
1356
1357 </DividedMarkdownCode>
1358
1359 </DividedMarkdownLayout>
1360
1361 <DividedMarkdownLayout>
1362
1363 <Feedback
1364 extraData={{
1365 section: "query-parameters"
1366 }}
1367 question="Was this section helpful?"
1368 />
1369
1370 </DividedMarkdownLayout>
1371
1372 </SectionContainer>
1373
1374 <SectionContainer noTopPadding={true}>
1375
1376 <DividedMarkdownLayout>
1377
1378 <DividedMarkdownContent>
1379
1380 ## Pagination
1381
1382 ### Query Parameters
1383
1384
1385 In listing API Routes, such as list customers or list products, you can control the pagination using the query parameters `limit` and `offset`.
1386
1387 </DividedMarkdownContent>
1388
1389 </DividedMarkdownLayout>
1390
1391 <DividedMarkdownLayout addYSpacing>
1392
1393 <DividedMarkdownContent>
1394
1395 `limit` is used to specify the maximum number of items to be returned in the response. `offset` is used to specify how many items to skip before returning the resulting records.
1396
1397
1398 Use the `offset` query parameter to change between pages. For example, if the limit is `50`, at page `1` the offset should be `0`; at page `2` the offset should be `50`, and so on.
1399
1400 </DividedMarkdownContent>
1401
1402 <DividedMarkdownCode>
1403
1404 <CodeTabs group="request-examples">
1405
1406 <CodeTab label="JS SDK" value="js-sdk">
1407
1408 ```js title="Pagination query parameters"
1409 sdk.admin.product.list({
1410 limit: 5,
1411 offset: 0
1412 })
1413 ```
1414
1415 </CodeTab>
1416
1417 <CodeTab label="cURL" value="curl">
1418
1419 ```bash title="Pagination query parameters"
1420 curl "http://localhost:9000/admin/products?limit=5&offset=0" \
1421 -H 'Authorization: Bearer {jwt_token}'
1422 ```
1423
1424 </CodeTab>
1425
1426 </CodeTabs>
1427
1428 </DividedMarkdownCode>
1429
1430 </DividedMarkdownLayout>
1431
1432 <DividedMarkdownLayout addYSpacing>
1433
1434 <DividedMarkdownContent>
1435
1436 ### Response Fields
1437
1438
1439 In the response of listing API Routes, aside from the records retrieved,
1440 there are three pagination-related fields returned:
1441
1442
1443 - `limit`: the maximum number of items that can be returned in the response.
1444 - `offset`: the number of items that were skipped before the records in the result.
1445 - `count`: the total number of available items of this data model. It can be used to determine how many pages are there.
1446
1447
1448 For example, if the `count` is `100` and the `limit` is `50`, divide the
1449 `count` by the `limit` to get the number of pages: `100/50 = 2 pages`.
1450
1451 </DividedMarkdownContent>
1452
1453 </DividedMarkdownLayout>
1454
1455 <DividedMarkdownLayout addYSpacing codeContentClassName="mt-3">
1456
1457 <DividedMarkdownContent>
1458
1459 ### Sort Order
1460
1461
1462 The `order` field (available on API Routes that support pagination) allows you to
1463 sort the retrieved items by a field of that item.
1464
1465 </DividedMarkdownContent>
1466
1467 <DividedMarkdownCode>
1468
1469 <CodeTabs group="request-examples">
1470
1471 <CodeTab label="JS SDK" value="js-sdk">
1472
1473 ```js title="Ascending sort by a field"
1474 sdk.admin.product.list({
1475 order: "created_at"
1476 })
1477 ```
1478
1479 </CodeTab>
1480
1481 <CodeTab label="cURL" value="curl">
1482
1483 ```bash title="Ascending sort by a field"
1484 curl "http://localhost:9000/admin/products?order=created_at" \
1485 -H 'Authorization: Bearer {jwt_token}'
1486 ```
1487
1488 </CodeTab>
1489
1490 </CodeTabs>
1491
1492 This sorts the products by their `created_at` field in the ascending order.
1493
1494 </DividedMarkdownCode>
1495
1496 </DividedMarkdownLayout>
1497
1498 <DividedMarkdownLayout addYSpacing>
1499
1500 <DividedMarkdownContent>
1501
1502 By default, the sort direction is ascending. To change it to
1503 descending, pass a dash (`-`) before the field name.
1504
1505 </DividedMarkdownContent>
1506
1507 <DividedMarkdownCode>
1508
1509 <CodeTabs group="request-examples">
1510
1511 <CodeTab label="JS SDK" value="js-sdk">
1512
1513 ```js title="Descending sort by a field"
1514 sdk.admin.product.list({
1515 order: "-created_at"
1516 })
1517 ```
1518
1519 </CodeTab>
1520
1521 <CodeTab label="cURL" value="curl">
1522
1523 ```bash title="Descending sort by a field"
1524 curl "http://localhost:9000/admin/products?order=-created_at" \
1525 -H 'Authorization: Bearer {jwt_token}'
1526 ```
1527
1528 </CodeTab>
1529
1530 </CodeTabs>
1531
1532 This sorts the products by their `created_at` field in the descending order.
1533
1534 </DividedMarkdownCode>
1535
1536 </DividedMarkdownLayout>
1537
1538 <DividedMarkdownLayout>
1539
1540 <Feedback
1541 extraData={{
1542 section: "pagination"
1543 }}
1544 question="Was this section helpful?"
1545 />
1546
1547 </DividedMarkdownLayout>
1548
1549 </SectionContainer>
1550
1551 <SectionContainer noTopPadding>
1552
1553 <DividedMarkdownLayout>
1554
1555 <DividedMarkdownContent>
1556
1557 ## Workflows
1558
1559 While browsing this reference, you'll find some API routes mention what workflow is used in them.
1560
1561 If you click on the workflow, you'll view a reference of that workflow, including its hooks.
1562
1563 This is useful if you want to extend an API route and pass additional data or perform custom actions.
1564
1565 Refer to [this guide](!docs!/learn/customization/extend-features/extend-create-product) to find an example of extending an API route.
1566
1567 <Feedback
1568 extraData={{
1569 section: "workflows"
1570 }}
1571 question="Was this section helpful?"
1572 />
1573
1574 </DividedMarkdownContent>
1575
1576 </DividedMarkdownLayout>
1577
1578 </SectionContainer>
1579
1580 </Section>