Examples of the Product Module
In this guide, you’ll find common examples of how you can use the Product Module in your application.
Create Product
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const products = await productModuleService.create([
{
title: "Medusa Shirt",
options: [
{
title: "Color",
},
],
variants: [
{
title: "Black Shirt",
options: [
{
value: "Black",
},
],
},
],
},
])
res.json({ products })
}
import { NextResponse } from "next/server"
import {
initialize as initializeProductModule,
} from "@medusajs/product"
export async function POST(request: Request) {
const productModuleService = await initializeProductModule()
const products = await productModuleService.create([
{
title: "Medusa Shirt",
options: [
{
title: "Color",
},
],
variants: [
{
title: "Black Shirt",
options: [
{
value: "Black",
},
],
},
],
},
])
return NextResponse.json({ products })
}
List Products
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const data = await productModuleService.list()
res.json({ products: data })
}
import { NextResponse } from "next/server"
import {
initialize as initializeProductModule,
} from "@medusajs/product"
export async function GET(request: Request) {
const productModuleService = await initializeProductModule()
const data = await productModuleService.list()
return NextResponse.json({ products: data })
}
Retrieve a Product by its ID
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const data = await productModuleService.list({
id: request.params.id,
})
res.json({ product: data[0] })
}
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 productModuleService = await initializeProductModule()
const data = await productModuleService.list({
id,
})
return NextResponse.json({ product: data[0] })
}
Retrieve a Product by its Handle
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const data = await productModuleService.list()
res.json({ products: data })
}
import { NextResponse } from "next/server"
import {
initialize as initializeProductModule,
} from "@medusajs/product"
export async function GET(request: Request) {
const productModuleService = await initializeProductModule()
const data = await productModuleService.list()
return NextResponse.json({ products: data })
}
Retrieve a Product by its ID
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const data = await productModuleService.list({
handle: request.params.handle,
})
res.json({ product: data[0] })
}
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 productModuleService = await initializeProductModule()
const data = await productModuleService.list({
handle,
})
return NextResponse.json({ product: data[0] })
}
Retrieve Categories
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const data = await productModuleService.listCategories()
res.json({ categories: data })
}
import { NextResponse } from "next/server"
import {
initialize as initializeProductModule,
} from "@medusajs/product"
export async function GET(request: Request) {
const productModuleService = await initializeProductModule()
const data = await productModuleService.listCategories()
return NextResponse.json({ categories: data })
}
Retrieve Category by Handle
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: IProductModuleService =
request.scope.resolve(ModuleRegistrationName.PRODUCT)
const data = await productModuleService.listCategories({
handle: request.params.handle,
})
res.json({ category: data[0] })
}
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 productModuleService = await initializeProductModule()
const data = await productModuleService.listCategories({
handle,
})
return NextResponse.json({ category: data[0] })
}
More Examples
The Product Module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?