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

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

# {metadata.title}

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

## Create a User

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

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

export async function POST(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  const user = await userModuleService.createUsers({
    email: "user@example.com",
    first_name: "John",
    last_name: "Smith",
  })

  res.json({ user })
}
```

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function POST(request: Request) {
  const userModuleService = await initializeUserModule()

  const user = await userModuleService.createUsers({
    email: "user@example.com",
    first_name: "John",
    last_name: "Smith",
  })

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

  </CodeTab>
</CodeTabs>

---

## List Users

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

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

export async function GET(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  res.json({
    users: await userModuleService.listUsers(),
  })
}
```

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function GET(request: Request) {
  const userModuleService = await initializeUserModule()

  return NextResponse.json({
    users: await userModuleService.listUsers(),
  })
}
```

  </CodeTab>
</CodeTabs>

---

## Update a User

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

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

export async function POST(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  const user = await userModuleService.updateUsers({
    id: "user_123",
    last_name: "Smith",
  })

  res.json({ user })
}
```

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function POST(request: Request) {
  const userModuleService = await initializeUserModule()

  const user = await userModuleService.updateUsers({
    id: "user_123",
    last_name: "Smith",
  })

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

  </CodeTab>
</CodeTabs>

---

## Delete a User

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

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

export async function DELETE(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  await userModuleService.deleteUsers(["user_123"])
}
```

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function DELETE(request: Request) {
  const userModuleService = await initializeUserModule()

  await userModuleService.deleteUsers(["user_123"])
}
```

  </CodeTab>
</CodeTabs>

---

## Invite User

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

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

export async function POST(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  const invite = await userModuleService.createInvites({
    email: "user2@example.com",
  })

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

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function POST(request: Request, { params }: ContextType) {
  const userModuleService = await initializeUserModule()

  const invite = await userModuleService.createInvites({
    email: "user2@example.com",
  })

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

  </CodeTab>
</CodeTabs>

---

## Accept Invite

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

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

export async function POST(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  const invite = await userModuleService.validateInviteToken("secret_123")

  const user = await userModuleService.createUsers({
    email: invite.email,
    metadata: invite.metadata,
  })

  await userModuleService.updateInvites({
    id: invite.id,
    accepted: true,
  })

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

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function POST(request: Request, { params }: ContextType) {
  const userModuleService = await initializeUserModule()

  const invite = await userModuleService.validateInviteToken("secret_123")

  const user = await userModuleService.createUsers({
    email: invite.email,
    metadata: invite.metadata,
  })

  await userModuleService.updateInvites({
    id: invite.id,
    accepted: true,
  })

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

  </CodeTab>
</CodeTabs>

---

## Refresh Invite

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

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

export async function POST(
  req: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const userModuleService: IUserModuleService = req.scope.resolve(
    Modules.USER
  )

  const invites = await userModuleService.refreshInviteTokens(["invite_123"])

  res.json({
    invite: invites[0],
  })
}
```

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

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

import { initialize as initializeUserModule } from "@medusajs/medusa/user"

export async function POST(request: Request, { params }: ContextType) {
  const userModuleService = await initializeUserModule()

  const invites = await userModuleService.refreshInviteTokens(["invite_123"])

  return NextResponse.json({
    invite: invites[0],
  })
}
```

  </CodeTab>
</CodeTabs>

---

## More Examples

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