import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Examples of Product Module

In this document, you’ll find common examples of how you can use the Product module in your application.

## Create Product

<Tabs groupId="app-type" isCodeTabs={true}>
  <TabItem value="nextjs" label="Next.js App Router" default>

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

    import { 
      initialize as initializeProductModule,
    } from "@medusajs/product"

    export async function POST(request: Request) {
      const productService = await initializeProductModule()

      const products = await productService.create([
        {
          title: "Medusa Shirt",
          options: [
            {
              title: "Color",
            },
          ],
          variants: [
            {
              title: "Black Shirt",
              options: [
                {
                  value: "Black",
                },
              ],
            },
          ],
        },
      ])

      return NextResponse.json({ products })
    }
    ```

  </TabItem>
  <TabItem value="medusa" label="Medusa API Router">

    ```ts
    import { 
      MedusaRequest, 
      MedusaResponse,
      PriceListType
    } from "@medusajs/medusa";
    import { 
      ProductModuleService
    } from "@medusajs/product"

    export async function POST(
      request: MedusaRequest,
      response: MedusaResponse
    ): Promise<void> {
      const productModuleService: ProductModuleService = 
        request.scope.resolve(
          "productModuleService"
        )

      const products = await productModuleService.create([
        {
          title: "Medusa Shirt",
          options: [
            {
              title: "Color",
            },
          ],
          variants: [
            {
              title: "Black Shirt",
              options: [
                {
                  value: "Black",
                },
              ],
            },
          ],
        },
      ])

      response.json({ products })
    }
    ```

  </TabItem>
</Tabs>

## List Products

<Tabs groupId="app-type" isCodeTabs={true}>
  <TabItem value="nextjs" label="Next.js App Router" default>

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

    import { 
      initialize as initializeProductModule,
    } from "@medusajs/product"

    export async function GET(request: Request) {
      const productService = await initializeProductModule()

      const data = await productService.list()

      return NextResponse.json({ products: data })
    }
    ```

  </TabItem>
  <TabItem value="medusa" label="Medusa API Router">

    ```ts
    import { 
      MedusaRequest, 
      MedusaResponse,
      PriceListType
    } from "@medusajs/medusa";
    import { 
      ProductModuleService
    } from "@medusajs/product"

    export async function GET(
      request: MedusaRequest,
      response: MedusaResponse
    ): Promise<void> {
      const productModuleService: ProductModuleService = 
        request.scope.resolve(
          "productModuleService"
        )

      const data = await productModuleService.list()

      response.json({ products: data })
    }
    ```

  </TabItem>
</Tabs>

## Retrieve a Product by its ID

<Tabs groupId="app-type" isCodeTabs={true}>
  <TabItem value="nextjs" label="Next.js App Router" default>

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

    import { 
      initialize as initializeProductModule,
    } from "@medusajs/product"

    export async function GET(
      request: Request, 
      { params }: { params: Record<string, any> }) {
      
      const { id } = params
      const productService = await initializeProductModule()

      const data = await productService.list({
        id,
      })

      return NextResponse.json({ product: data[0] })
    }
    ```

  </TabItem>
  <TabItem value="medusa" label="Medusa API Router">

    ```ts
    import { 
      MedusaRequest, 
      MedusaResponse,
      PriceListType
    } from "@medusajs/medusa";
    import { 
      ProductModuleService
    } from "@medusajs/product"

    export async function GET(
      request: MedusaRequest,
      response: MedusaResponse
    ): Promise<void> {
      const productModuleService: ProductModuleService = 
        request.scope.resolve(
          "productModuleService"
        )

      const data = await productModuleService.list({
        id: request.params.id,
      })

      response.json({ product: data[0] })
    }
    ```

  </TabItem>
</Tabs>

## Retrieve a Product by its Handle

<Tabs groupId="app-type" isCodeTabs={true}>
  <TabItem value="nextjs" label="Next.js App Router" default>

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

    import { 
      initialize as initializeProductModule,
    } from "@medusajs/product"

    export async function GET(
      request: Request, 
      { params }: { params: Record<string, any> }) {
      
      const { handle } = params
      const productService = await initializeProductModule()

      const data = await productService.list({
        handle,
      })

      return NextResponse.json({ product: data[0] })
    }
    ```

  </TabItem>
  <TabItem value="medusa" label="Medusa API Router">

    ```ts
    import { 
      MedusaRequest, 
      MedusaResponse,
      PriceListType
    } from "@medusajs/medusa";
    import { 
      ProductModuleService
    } from "@medusajs/product"

    export async function GET(
      request: MedusaRequest,
      response: MedusaResponse
    ): Promise<void> {
      const productModuleService: ProductModuleService = 
        request.scope.resolve(
          "productModuleService"
        )

      const data = await productModuleService.list({
        handle: request.params.handle,
      })

      response.json({ product: data[0] })
    }
    ```

  </TabItem>
</Tabs>

## Retrieve Categories

<Tabs groupId="app-type" isCodeTabs={true}>
  <TabItem value="nextjs" label="Next.js App Router" default>

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

    import { 
      initialize as initializeProductModule,
    } from "@medusajs/product"

    export async function GET(request: Request) {
      const productService = await initializeProductModule()

      const data = await productService.listCategories()

      return NextResponse.json({ categories: data })
    }
    ```

  </TabItem>
  <TabItem value="medusa" label="Medusa API Router">

    ```ts
    import { 
      MedusaRequest, 
      MedusaResponse,
      PriceListType
    } from "@medusajs/medusa";
    import { 
      ProductModuleService
    } from "@medusajs/product"

    export async function POST(
      request: MedusaRequest,
      response: MedusaResponse
    ): Promise<void> {
      const productModuleService: ProductModuleService = 
        request.scope.resolve(
          "productModuleService"
        )

      const data = await productModuleService.listCategories()

      response.json({ categories: data })
    }
    ```

  </TabItem>
</Tabs>

## Retrieve Category by Handle

<Tabs groupId="app-type" isCodeTabs={true}>
  <TabItem value="nextjs" label="Next.js App Router" default>

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

    import { 
      initialize as initializeProductModule,
    } from "@medusajs/product"

    export async function GET(
      request: Request, 
      { params }: { params: Record<string, any> }) {
      
      const { handle } = params
      const productService = await initializeProductModule()

      const data = await productService.listCategories({
        handle,
      })

      return NextResponse.json({ category: data[0] })
    }
    ```

  </TabItem>
  <TabItem value="medusa" label="Medusa API Router">

    ```ts
    import { 
      MedusaRequest, 
      MedusaResponse,
      PriceListType
    } from "@medusajs/medusa";
    import { 
      ProductModuleService
    } from "@medusajs/product"

    export async function POST(
      request: MedusaRequest,
      response: MedusaResponse
    ): Promise<void> {
      const productModuleService: ProductModuleService = 
        request.scope.resolve(
          "productModuleService"
        )

      const data = await productModuleService.listCategories({
        handle: request.params.handle,
      })

      response.json({ category: data[0] })
    }
    ```

  </TabItem>
</Tabs>

---

## More Examples

The [module interface reference](../../references/product/interfaces/product.IProductModuleService.mdx) provides a reference to all the methods available for use with examples for each.
