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