Skip to main content
Skip to main content

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

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 })
}

List Products

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 })
}

Retrieve a Product by its ID

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] })
}

Retrieve a Product by its Handle

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] })
}

Retrieve Categories

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 })
}

Retrieve Category by Handle

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] })
}

More Examples

The module interface reference provides a reference to all the methods available for use with examples for each.

Was this section helpful?