Skip to main content
Skip to main content

AdminCustomersResource

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

All methods in this class require user authentication.

Customers can either be created when they register through the CustomersResource.create method, or created by the admin using the create method.

Related Guide: How to manage customers.

Methods

create

Create a customer as an admin.

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.customers.create({
email: "user@example.com",
first_name: "Caterina",
last_name: "Yost",
password: "supersecret"
})
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

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

Default: {}

Returns

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

update

Update a customer'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.customers.update(customerId, {
first_name: "Dolly"
})
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

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

Default: {}

Returns

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

retrieve

Retrieve the details of a customer.

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.customers.retrieve(customerId)
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

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

Default: {}

Returns

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

list

Retrieve a list of Customers. The customers can be filtered by fields such as q or groups. The customers can also be paginated.

Example

To list customers:

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

To specify relations that should be retrieved within the customers:

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

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

Parameters

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?