You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/apps/api-reference/markdown/admin.mdx
+220Lines changed: 220 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -483,6 +483,226 @@ x-no-compression: false
483
483
484
484
<DividedMarkdownContent>
485
485
486
+
## Manage Metadata
487
+
488
+
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.
489
+
490
+
When setting or updating the `metadata` field using the relevant API routes, Medusa will merge the new metadata with the existing metadata.
491
+
492
+
<Note>
493
+
494
+
The instructions in this section apply to any [JSON property in a data model](!docs!/learn/fundamentals/data-models/json-properties).
495
+
496
+
</Note>
497
+
498
+
</DividedMarkdownContent>
499
+
500
+
</DividedMarkdownLayout>
501
+
502
+
<DividedMarkdownLayoutaddYSpacing>
503
+
504
+
<DividedMarkdownContent>
505
+
506
+
### Accepted Values in Metadata
507
+
508
+
The `metadata` is an object of key-value pairs, where the keys are strings and the values can be one of the following types:
509
+
510
+
- String
511
+
- An empty string deletes the property from the metadata.
512
+
- Number
513
+
- Boolean
514
+
- Date
515
+
- Object
516
+
- Arrays of any of the above types
517
+
518
+
The `metadata` is not validated, so you can store any custom data in it.
519
+
520
+
</DividedMarkdownContent>
521
+
522
+
<DividedMarkdownCode>
523
+
524
+
```ts title="Metadata Example"
525
+
{
526
+
"metadata": {
527
+
"category": "electronics",
528
+
"views": 1500,
529
+
"is_featured": true,
530
+
"tags": ["new", "sale"],
531
+
"details": {
532
+
"warranty": "2 years",
533
+
"origin": "USA"
534
+
}
535
+
}
536
+
}
537
+
```
538
+
539
+
</DividedMarkdownCode>
540
+
541
+
</DividedMarkdownLayout>
542
+
543
+
<DividedMarkdownLayoutaddYSpacing>
544
+
545
+
<DividedMarkdownContent>
546
+
547
+
### Add or Update New Property in Metadata
548
+
549
+
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.
550
+
551
+
</DividedMarkdownContent>
552
+
553
+
<DividedMarkdownCode>
554
+
555
+
<CodeTabsgroup="request-with-result">
556
+
557
+
<CodeTablabel="JS SDK"value="js-sdk">
558
+
559
+
```js title="Add new metadata property"
560
+
sdk.admin.product.update("prod_123", {
561
+
metadata: {
562
+
new_property:"value"
563
+
}
564
+
})
565
+
```
566
+
567
+
</CodeTab>
568
+
569
+
<CodeTablabel="Result"value="result">
570
+
571
+
```json title="Result"
572
+
{
573
+
"id": "prod_123",
574
+
"metadata": {
575
+
"new_property": "value",
576
+
"old_property": "value"
577
+
}
578
+
}
579
+
```
580
+
581
+
</CodeTab>
582
+
583
+
</CodeTabs>
584
+
585
+
</DividedMarkdownCode>
586
+
587
+
</DividedMarkdownLayout>
588
+
589
+
<DividedMarkdownLayoutaddYSpacing>
590
+
591
+
<DividedMarkdownContent>
592
+
593
+
### Update Nested Objects in Metadata
594
+
595
+
When updating a nested object in the `metadata`, you must pass the entire object in the request body.
596
+
597
+
Medusa doesn't merge nested objects, so if you pass a partial object, the existing properties in the nested object will be removed.
598
+
599
+
</DividedMarkdownContent>
600
+
601
+
<DividedMarkdownCode>
602
+
603
+
<CodeTabsgroup="request-with-result">
604
+
605
+
<CodeTablabel="JS SDK"value="js-sdk">
606
+
607
+
```js title="Update nested object in metadata"
608
+
sdk.admin.product.update("prod_123", {
609
+
metadata: {
610
+
nested_object: {
611
+
property1:"value1",
612
+
property2:"value2"
613
+
}
614
+
}
615
+
})
616
+
```
617
+
618
+
</CodeTab>
619
+
620
+
<CodeTablabel="Result"value="result">
621
+
622
+
```json title="Result"
623
+
{
624
+
"id": "prod_123",
625
+
"metadata": {
626
+
"nested_object": {
627
+
"property1": "value1",
628
+
"property2": "value2"
629
+
}
630
+
}
631
+
}
632
+
```
633
+
634
+
</CodeTab>
635
+
636
+
</CodeTabs>
637
+
638
+
</DividedMarkdownCode>
639
+
640
+
</DividedMarkdownLayout>
641
+
642
+
<DividedMarkdownLayoutaddYSpacing>
643
+
644
+
<DividedMarkdownContent>
645
+
646
+
### Remove Property from Metadata
647
+
648
+
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.
0 commit comments