import { CodeTabs, CodeTab } from "docs-ui"

export const metadata = {
  title: `Examples of the Customer Module`,
}

# {metadata.title}

In this guide, you’ll find common examples of how you can use the Customer Module in your application.

<Note>

You should only use the Customer Module's main service when implementing complex customizations. For common cases, check out [available workflows instead](../../../medusa-workflows-reference/page.mdx).

</Note>

## Create a Customer

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"

export async function POST(request: MedusaRequest, res: MedusaResponse) {
  const customerModuleService = request.scope.resolve(
    Modules.CUSTOMER
  )

  const customer = await customerModuleService.createCustomers({
    first_name: "Peter",
    last_name: "Hayes",
    email: "peter.hayes@example.com",
  })

  res.json({
    customer,
  })
}
```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

```ts
import { NextResponse } from "next/server"

import { initialize as initializeCustomerModule } from "@medusajs/medusa/customer"

export async function POST(request: Request) {
  const customerModuleService = await initializeCustomerModule()

  const customer = await customerModuleService.createCustomers({
    first_name: "Peter",
    last_name: "Hayes",
    email: "peter.hayes@example.com",
  })

  return NextResponse.json({
    customer,
  })
}
```

  </CodeTab>
</CodeTabs>

---

## Create a Customer Group

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"

export async function POST(request: MedusaRequest, res: MedusaResponse) {
  const customerModuleService = request.scope.resolve(
    Modules.CUSTOMER
  )

  const customerGroup = await customerModuleService.createCustomerGroups({
    name: "VIP",
  })

  res.json({
    customer_group: customerGroup,
  })
}
```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

```ts
import { NextResponse } from "next/server"

import { initialize as initializeCustomerModule } from "@medusajs/medusa/customer"

export async function POST(request: Request) {
  const customerModuleService = await initializeCustomerModule()

  const customerGroup = await customerModuleService.createCustomerGroups({
    name: "VIP",
  })

  return NextResponse.json({
    customer_group: customerGroup,
  })
}
```

  </CodeTab>
</CodeTabs>

---

## Add a Customer to a Group

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"

export async function POST(request: MedusaRequest, res: MedusaResponse) {
  const customerModuleService = request.scope.resolve(
    Modules.CUSTOMER
  )

  await customerModuleService.addCustomerToGroup({
    customer_id: "cus_123",
    customer_group_id: "cusgroup_123",
  })

  res.status(200)
}
```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

```ts
import { NextResponse } from "next/server"

import { initialize as initializeCustomerModule } from "@medusajs/medusa/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 })
}
```

  </CodeTab>
</CodeTabs>

---

## Remove a Customer from a Group

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"

export async function POST(request: MedusaRequest, res: MedusaResponse) {
  const customerModuleService = request.scope.resolve(
    Modules.CUSTOMER
  )

  await customerModuleService.removeCustomerFromGroup({
    customer_id: "cus_123",
    customer_group_id: "cusgroup_123",
  })

  res.status(200)
}
```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

```ts
import { NextResponse } from "next/server"

// eslint-disable-next-line prettier/prettier
import { initialize as initializeCustomerModule } from "@medusajs/medusa/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 })
}
```

  </CodeTab>
</CodeTabs>

---

## More Examples

The [Customer Module's main service reference](/references/customer) provides a reference to all the methods available for use with examples for each.
