Skip to main content
Skip to main content

AdminCollectionsResource

This class is used to send requests to Admin Product Collection API Routes. All its method are available in the JS Client under the medusa.admin.collections property.

All methods in this class require user authentication.

A product collection is used to organize products for different purposes such as marketing or discount purposes. For example, you can create a Summer Collection.

Methods

addProducts

Add products to collection.

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.collections.addProducts(collectionId, {
product_ids: [
productId1,
productId2
]
})
.then(({ collection }) => {
console.log(collection.products)
})

Parameters

idstringRequired
The ID of the product collection.
The products to add.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCollectionsRes>Required
Resolves to the product collection's details.

create

Create a product collection.

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.collections.create({
title: "New Collection"
})
.then(({ collection }) => {
console.log(collection.id);
})

Parameters

The data of the product collection to create.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCollectionsRes>Required
Resolves to the created product collection's details.

delete

Delete a product collection. This does not delete associated products.

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.collections.delete(collectionId)
.then(({ id, object, deleted }) => {
console.log(id);
})

Parameters

idstringRequired
The ID of the product collection.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<DeleteResponse>Required
Resolves to the deletion operation details.

list

Retrieve a list of product collections. The product collections can be filtered by fields such as handle or title. The collections can also be sorted or paginated.

Example

To list product collections:

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.collections.list()
.then(({ collections, limit, offset, count }) => {
console.log(collections.length);
})

By default, only the first 10 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.collections.list({
limit,
offset
})
.then(({ collections, limit, offset, count }) => {
console.log(collections.length);
})

Parameters

Filters and pagination configurations to apply on the retrieved product collections.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCollectionsListRes>Required
Resolves to the list of product collections with pagination fields.

removeProducts

Remove a list of products from a collection. This would not delete the product, only the association between the product and the collection.

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.collections.removeProducts(collectionId, {
product_ids: [
productId1,
productId2
]
})
.then(({ id, object, removed_products }) => {
console.log(removed_products)
})

Parameters

idstringRequired
the ID of the product collection
The products to remove from the collection.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDeleteProductsFromCollectionRes>Required
Resolves to the deletion operation details.

retrieve

Retrieve a product collection by its ID. The products associated with it are expanded and returned as well.

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.collections.retrieve(collectionId)
.then(({ collection }) => {
console.log(collection.id);
})

Parameters

idstringRequired
The ID of the product collection.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCollectionsRes>Required
Resolves to the product collection's details.

update

Update a product collection'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.collections.update(collectionId, {
title: "New Collection"
})
.then(({ collection }) => {
console.log(collection.id);
})

Parameters

idstringRequired
The ID of the product collection.
The data to update in the product collection.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCollectionsRes>Required
Resolves to the product collection's details.
Was this section helpful?