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 })
}
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 })
}
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 })
}
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 })
}
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] })
}
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] })
}
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] })
}
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] })
}
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 })
}
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 })
}
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] })
}
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] })
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?