Skip to main content
Skip to main content

AdminCustomerGroupsResource

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

All methods in this class require user authentication.

Customer Groups can be used to organize customers that share similar data or attributes into dedicated groups. This can be useful for different purposes such as setting a different price for a specific customer group.

Related Guide: How to manage customer groups.

Methods

create

Create a customer group.

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.customerGroups.create({
name: "VIP"
})
.then(({ customer_group }) => {
console.log(customer_group.id);
})

Parameters

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

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCustomerGroupsRes>Required
Resolves to the customer group's details.

retrieve

Retrieve a customer group by its ID. You can expand the customer group's relations or select the fields that should be returned.

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.customerGroups.retrieve(customerGroupId)
.then(({ customer_group }) => {
console.log(customer_group.id);
})

Parameters

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

Default: {}

Configurations to apply on the retrieved customer group.

Returns

ResponsePromiseResponsePromise<AdminCustomerGroupsRes>Required
Resolves to the customer group's details.

update

Update a customer group's details.

Parameters

idstringRequired
The ID of the customer group.
The attributes to update in the customer group.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCustomerGroupsRes>Required
Resolves to the customer group's details.

delete

Delete a customer group. This doesn't delete the customers associated with the customer group.

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

Parameters

idstringRequired
The ID of the customer group.
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 customer groups. The customer groups can be filtered by fields such as name or id. The customer groups can also be sorted or paginated.

Example

To list customer groups:

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

To specify relations that should be retrieved within the customer groups:

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

Parameters

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

Default: {}

Filters and pagination configurations to apply on the retrieved customer groups.

Returns

ResponsePromiseResponsePromise<AdminCustomerGroupsListRes>Required
Resolves to the list of customer groups with pagination fields.

addCustomers

Add a list of customers to a customer group.

Parameters

idstringRequired
The ID of the customer group.
The customers to add to the customer group.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCustomerGroupsRes>Required
Resolves to the customer group's details.

removeCustomers

Remove a list of customers from a customer group. This doesn't delete the customer, only the association between the customer and the customer group.

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.customerGroups.removeCustomers(customerGroupId, {
customer_ids: [
{
id: customerId
}
]
})
.then(({ customer_group }) => {
console.log(customer_group.id);
})

Parameters

idstringRequired
The ID of the customer group.
The customers to remove from the customer group.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminCustomerGroupsRes>Required
Resolves to the customer group's details.

listCustomers

Retrieve a list of customers in a customer group. The customers can be filtered by the q field. The customers can also be paginated.

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.customerGroups.listCustomers(customerGroupId)
.then(({ customers }) => {
console.log(customers.length);
})

Parameters

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

Default: {}

Filters and pagination configurations to apply on the retrieved customers.

Returns

ResponsePromiseResponsePromise<AdminCustomersListRes>Required
Resolves to the list of customers with pagination fields.
Was this section helpful?