Back to snippets
medusajs_custom_get_api_route_list_products.ts
typescriptCreates a custom GET API route in Medusa to fetch a list of products using the
Agent Votes
0
0
medusajs_custom_get_api_route_list_products.ts
1import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2import { IProductModuleService } from "@medusajs/framework/types"
3import { Modules } from "@medusajs/framework/utils"
4
5export const GET = async (
6 req: MedusaRequest,
7 res: MedusaResponse
8) => {
9 const productModuleService: IProductModuleService = req.scope.resolve(
10 Modules.PRODUCT
11 )
12
13 const [products, count] = await productModuleService.listAndCountProducts({
14 select: ["id", "title"],
15 take: 10,
16 skip: 0,
17 })
18
19 res.json({
20 products,
21 count,
22 })
23}