Skip to main content
Skip to main content
You're viewing the documentation for v1, which isn't the latest Medusa version.Latest documentation

AdminVariantsResource

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

All methods in this class require user authentication.

Product variants are the actual salable item in your store. Each variant is a combination of the different option values available on the product. Product variants can be managed through AdminProductsResource.

Related Guide: How to manage product variants.

Methods​

list​

Retrieve a list of product variants. The product variant can be filtered by fields such as id or title passed in the query parameter. The product variant can also be paginated.

Example​

To list product variants:

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

To specify relations that should be retrieved within the product variants:

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

By default, only the first 100 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.variants.list({
expand: "options",
limit,
offset
})
.then(({ variants, limit, offset, count }) => {
console.log(variants.length);
})

Parameters​

customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Filters and pagination configurations to apply on the retrieved product variants.

Returns​

ResponsePromiseResponsePromise<AdminVariantsListRes>Required
Resolves to the list of product variants with pagination fields.

retrieve​

Retrieve a product variant's details.

Example​

A simple example that retrieves a product variant by its ID:

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.variants.retrieve(variantId)
.then(({ variant }) => {
console.log(variant.id);
})

To specify relations that should be retrieved:

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.variants.retrieve(variantId, {
expand: "options"
})
.then(({ variant }) => {
console.log(variant.id);
})

Parameters​

idstringRequired
The product variant's ID.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Configurations to apply on the retrieved product variant.

Returns​

ResponsePromiseResponsePromise<AdminVariantsRes>Required
Resolves to the product variant's details.

getInventory​

Retrieve the available inventory of 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.variants.getInventory(variantId)
.then(({ variant }) => {
console.log(variant.inventory, variant.sales_channel_availability)
})

Parameters​

variantIdstringRequired
The product variant's ID.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns​

ResponsePromiseResponsePromise<AdminGetVariantsVariantInventoryRes>Required
Resolves to the inventory details of the product variant.
Was this section helpful?