1 export const metadata = {
2 title: `${pageNumber} Medusa Container`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you’ll learn about the Medusa container and how to use it.
8
9 ## What is the Medusa Container?
10
11 The Medusa container is a registry of Framework and commerce tools that's accessible across your application. Medusa automatically registers these tools in the container, including custom ones that you've built, so that you can use them in your customizations.
12
13 In other platforms, if you have a resource A (for example, a class) that depends on a resource B, you have to manually add resource B to the container or specify it beforehand as A's dependency, which is often done in a file separate from A's code. This becomes difficult to manage as you maintain larger applications with many changing dependencies.
14
15 Medusa simplifies this process by giving you access to the container, with the tools or resources already registered, at all times in your customizations. When you reach a point in your code where you need a tool, you resolve it from the container and use it.
16
17 For example, consider you're creating an API route that retrieves products based on filters using [Query](../../fundamentals/query/page.mdx), a tool that fetches data across the application. In the API route's function, you can resolve Query from the container passed to the API route and use it:
18
19 export const highlights = [
20 ["8", "resolve", "A method that resolves resources from the container."],
21 [
22 "8",
23 `query`,
24 "Query's registration key in the container.",
25 ],
26 ]
27
28 ```ts highlights={highlights}
29 import { MedusaRequest, MedusaResponse } from "@medusajs/framework"
30 import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
31
32 export async function GET(
33 req: MedusaRequest,
34 res: MedusaResponse
35 ) {
36 const query = req.scope.resolve(
37 ContainerRegistrationKeys.QUERY
38 )
39
40 const { data: products } = await query.graph({
41 entity: "product",
42 fields: ["*"],
43 filters: {
44 id: "prod_123",
45 },
46 })
47
48 res.json({
49 products,
50 })
51 }
52 ```
53
54 The API route accepts as a first parameter a request object that has a `scope` property, which is the Medusa container. It has a `resolve` method that resolves a resource from the container by the key it's registered with.
55
56 <Note>
57
58 You can learn more about how Query works in [this chapter](../../fundamentals/query/page.mdx).
59
60 </Note>
61
62 ---
63
64 ## List of Resources Registered in the Medusa Container
65
66 Find a full list of the registered resources and their registration key in [this reference](!resources!/medusa-container-resources)
67
68 ---
69
70 ## How to Resolve From the Medusa Container
71
72 This section gives quick examples of how to resolve resources from the Medusa container in customizations other than an API route, which is covered in the section above.
73
74 ### Subscriber
75
76 A [subscriber](../../fundamentals/events-and-subscribers/page.mdx) function, which is executed when an event is emitted, accepts as a parameter an object with a `container` property, whose value is the Medusa container. Use its `resolve` method to resolve a resource by its registration key:
77
78 export const subscriberHighlights = [
79 ["6", "container", "Receive the Medusa container as a parameter"],
80 ["8", "resolve", "Resolve a resource from the container by its key"]
81 ]
82
83 ```ts highlights={subscriberHighlights}
84 import { SubscriberArgs, type SubscriberConfig } from "@medusajs/framework"
85 import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
86
87 export default async function productCreateHandler({
88 event: { data },
89 container,
90 }: SubscriberArgs<{ id: string }>) {
91 const query = container.resolve(ContainerRegistrationKeys.QUERY)
92
93 const { data: products } = await query.graph({
94 entity: "product",
95 fields: ["*"],
96 filters: {
97 id: data.id,
98 },
99 })
100
101 console.log(`You created a product with the title ${products[0].title}`)
102 }
103
104 export const config: SubscriberConfig = {
105 event: `product.created`,
106 }
107 ```
108
109 ### Scheduled Job
110
111 A [scheduled job](../../fundamentals/scheduled-jobs/page.mdx) function, which is executed at a specified interval, accepts the Medusa container as a parameter. Use its `resolve` method to resolve a resource by its registration key:
112
113 export const scheduledJobHighlights = [
114 ["5", "container", "Receive the Medusa container as a parameter"],
115 ["7", "resolve", "Resolve a resource from the container by its key"]
116 ]
117
118 ```ts highlights={scheduledJobHighlights}
119 import { MedusaContainer } from "@medusajs/framework/types"
120 import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
121
122 export default async function myCustomJob(
123 container: MedusaContainer
124 ) {
125 const query = container.resolve(ContainerRegistrationKeys.QUERY)
126
127 const { data: products } = await query.graph({
128 entity: "product",
129 fields: ["*"],
130 filters: {
131 id: "prod_123",
132 },
133 })
134
135 console.log(
136 `You have ${products.length} matching your filters.`
137 )
138 }
139
140 export const config = {
141 name: "every-minute-message",
142 // execute every minute
143 schedule: "* * * * *",
144 }
145 ```
146
147 ### Workflow Step
148
149 A [step in a workflow](../../fundamentals/workflows/page.mdx), which is a special function where you build durable execution logic across multiple modules, accepts in its second parameter a `container` property, whose value is the Medusa container. Use its `resolve` method to resolve a resource by its registration key:
150
151 export const workflowStepsHighlight = [
152 ["7", "container", "Receive the Medusa container as a parameter"],
153 ["8", "resolve", "Resolve a resource from the container by its key"]
154 ]
155
156 ```ts highlights={workflowStepsHighlight}
157 import {
158 createStep,
159 StepResponse,
160 } from "@medusajs/framework/workflows-sdk"
161 import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
162
163 const step1 = createStep("step-1", async (_, { container }) => {
164 const query = container.resolve(ContainerRegistrationKeys.QUERY)
165
166 const { data: products } = await query.graph({
167 entity: "product",
168 fields: ["*"],
169 filters: {
170 id: "prod_123",
171 },
172 })
173
174 return new StepResponse(products)
175 })
176 ```
177
178 ### Module Services and Loaders
179
180 A [module](../../fundamentals/modules/page.mdx), which is a package of functionalities for a single feature or domain, has its own container, so it can't resolve resources from the Medusa container.
181
182 Learn more about the module's container in [this chapter](../../fundamentals/modules/container/page.mdx).
183
184 ---
185
186 ## Container Registration Keys Auto-Completion
187
188 When you resolve a resource from the Medusa container, you can use auto-completion to find the registration key of the resource.
189
190 This is possible due to Medusa's auto-generated types, which are generated in the `.medusa` directory when you run the `dev` command.
191
192 Learn more in the [Auto-Generated Types](../generated-types/page.mdx) chapter.