Skip to main content
Skip to main content

CustomersResource

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

A customer can register and manage their information such as addresses, orders, payment methods, and more.

Related Guide: How to implement customer profiles in your storefront.

Properties

paymentMethodsPaymentMethodsResourceRequired
An instance of PaymentMethodsResource used to send requests to payment-related routes part of the Store Customer API Routes.
addressesAddressesResourceRequired
An instance of AddressesResource used to send requests to address-related routes part of the Store Customer API Routes.

Methods

create

Register a new customer. This will also automatically authenticate the customer and set their login session in the response Cookie header. Subsequent requests sent with the JS client are sent with the Cookie session automatically.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
medusa.customers.create({
first_name: "Alec",
last_name: "Reynolds",
email: "user@example.com",
password: "supersecret"
})
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

payloadStorePostCustomersReqRequired
The details of the customer to be created.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

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

retrieve

Retrieve the logged-in customer's details. This method requires customer authentication.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged
medusa.customers.retrieve()
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

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

Default: {}

Returns

ResponsePromiseResponsePromise<StoreCustomersRes>Required
Resolves to the logged-in customer's details.

update

Update the logged-in customer's details. This method requires customer authentication.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged
medusa.customers.update({
first_name: "Laury"
})
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

The attributes to update in the logged-in customer.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<StoreCustomersRes>Required
Resolves to the logged-in customer's details.

listOrders

Retrieve a list of the logged-in customer's orders. The orders can be filtered by fields such as status or fulfillment_status. The orders can also be paginated. This method requires customer authentication.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged
medusa.customers.listOrders()
.then(({ orders, limit, offset, count }) => {
console.log(orders);
})

Parameters

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

Default: {}

Filters and pagination configurations to apply on the retrieved orders.

Returns

ResponsePromiseResponsePromise<StoreCustomersListOrdersRes>Required
Resolves to the list of orders with pagination fields.

resetPassword

Reset a customer's password using a password token created by a previous request using the generatePasswordToken method. If the password token expired, you must create a new one.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
medusa.customers.resetPassword({
email: "user@example.com",
password: "supersecret",
token: "supersecrettoken"
})
.then(({ customer }) => {
console.log(customer.id);
})

Parameters

The necessary details to reset the password.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

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

generatePasswordToken

Create a reset password token to be used when sending a request with the resetPassword method. This emits the event customer.password_reset. If a notification provider is installed in the Medusa backend and is configured to handle this event, a notification to the customer, such as an email, may be sent with reset instructions.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
medusa.customers.generatePasswordToken({
email: "user@example.com"
})
.then(() => {
// successful
})
.catch(() => {
// failed
})

Parameters

The necessary details to create the reset password token.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromiseRequired
Resolves when reset password token is created successfully.
Was this section helpful?