AdminProductsResource
This class is used to send requests to Admin Product API Routes. All its method
are available in the JS Client under the medusa.admin.products property.
All methods in this class require user authentication.
Products are saleable items in a store. This also includes saleable gift cards in a store.
Related Guide: How to manage products.
Methods
addOption
Add a product option to a product.
Example
Parameters
idstringRequiredThe option to add.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details. You can access the variant under the options property.
options property.create
Create a new Product. This API Route can also be used to create a gift card if the is_giftcard field is set to true.
Example
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.products
.create({
title: "Shirt",
is_giftcard: false,
discountable: true,
})
.then(({ product }) => {
console.log(product.id)
})
Parameters
The product to create.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details.
createVariant
Create a product variant associated with a product. Each product variant must have a unique combination of product option values.
Example
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.products
.createVariant(productId, {
title: "Color",
prices: [
{
amount: 1000,
currency_code: "eur",
},
],
options: [
{
option_id,
value: "S",
},
],
inventory_quantity: 100,
})
.then(({ product }) => {
console.log(product.id)
})
Parameters
idstringRequiredThe product variant to create.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details. You can access the variant under the variants property.
variants property.delete
Delete a product and its associated product variants and options.
Example
Parameters
idstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the deletion operation's details.
deleteOption
Delete a product option. If there are product variants that use this product option, they must be deleted before deleting the product option.
Parameters
idstringRequiredoptionIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the deletion operation's details.
deleteVariant
Delete a product variant.
Example
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.products
.deleteVariant(productId, variantId)
.then(({ variant_id, object, deleted, product }) => {
console.log(product.id)
})
Parameters
idstringRequiredvariantIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the deletion operation's details.
list
Retrieve a list of products. The products can be filtered by fields such as q or status passed in the query parameter. The products can also be sorted or paginated.
Example
To list products:
To specify relations that should be retrieved within the products:
By default, only the first 50 records are retrieved. You can control pagination by specifying the limit and offset properties:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.products
.list({
expand: "images",
limit,
offset,
})
.then(({ products, limit, offset, count }) => {
console.log(products.length)
})
Parameters
Filters and pagination configurations to apply on the retrieved products.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the list of products with pagination fields.
listTags
Retrieve a list of Product Tags with how many times each is used in products.
Example
Parameters
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the list of tags.
retrieve
Retrieve a product's details.
Example
Parameters
idstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details.
setMetadata
Set the metadata of a product. It can be any key-value pair, which allows adding custom data to a product. Learn about how you can update and delete the metadata attribute here.
Example
Parameters
idstringRequiredThe metadata details to add, update, or delete.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details.
update
Update a Product's details.
Example
Parameters
idstringRequiredThe attributes to update in a product.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details.
updateOption
Update a product option's details.
Example
Parameters
idstringRequiredoptionIdstringRequiredThe attributes to update in the product option.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details. You can access the variant under the options property.
options property.updateVariant
Update a product variant's details.
Example
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.products
.updateVariant(productId, variantId, {
title: "Color",
prices: [
{
amount: 1000,
currency_code: "eur",
},
],
options: [
{
option_id,
value: "S",
},
],
inventory_quantity: 100,
})
.then(({ product }) => {
console.log(product.id)
})
Parameters
idstringRequiredvariantIdstringRequiredThe attributes to update in the product variant.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
Resolves to the product's details. You can access the variant under the variants property.
variants property.