3.2. Medusa container

In this chapter, you’ll learn about Medusa’s Medusa container and how to use it.

What is the Medusa container?#

The Medusa container holds all resources registered in the Medusa application. You have access to it in your customizations.

You use the Medusa container to resolve resources, such as services.

For example, in a custom API route you can resolve any service registered in the Medusa application using the scope.resolve method of the MedusaRequest parameter:

1import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"2import { IProductModuleService } from "@medusajs/types"3import { ModuleRegistrationName } from "@medusajs/utils"4
5export const GET = async (req: MedusaRequest, res: MedusaResponse) => {6  const productModuleService: IProductModuleService = req.scope.resolve(7    ModuleRegistrationName.PRODUCT8  )9
10  const [, count] = await productModuleService11    .listAndCountProducts()12
13  res.json({14    count,15  })16}
Was this chapter helpful?