Examples of the Customer Module
In this guide, you’ll find common examples of how you can use the Customer Module in your application.
Create a Customer
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
const customer = await customerModuleService.create({
first_name: "Peter",
last_name: "Hayes",
email: "peter.hayes@example.com",
})
res.json({
customer,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
const customer = await customerModuleService.create({
first_name: "Peter",
last_name: "Hayes",
email: "peter.hayes@example.com",
})
return NextResponse.json({
customer,
})
}
Create a Customer Group
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
const customerGroup =
await customerModuleService.createCustomerGroup({
name: "VIP",
})
res.json({
customer_group: customerGroup,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
const customerGroup =
await customerModuleService.createCustomerGroup({
name: "VIP",
})
return NextResponse.json({
customer_group: customerGroup,
})
}
Add a Customer to a Group
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
await customerModuleService.addCustomerToGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
res.status(200)
}
import { NextResponse } from "next/server"
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
await customerModuleService.addCustomerToGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
return NextResponse.json({}, { status: 200 })
}
Remove a Customer from a Group
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
await customerModuleService.removeCustomerFromGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
res.status(200)
}
import { NextResponse } from "next/server"
// eslint-disable-next-line prettier/prettier
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
await customerModuleService.removeCustomerFromGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
return NextResponse.json({}, { status: 200 })
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?