Skip to main content
Skip to main content

Examples of the Inventory Module

In this document, you’ll find common examples of how you can use the Inventory Module in your application.

Create an Inventory Item

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const inventoryItem =
await inventoryModuleService.create({
sku: request.body.sku,
title: request.body.title,
requires_shipping: request.body.requires_shipping,
})

res.json({ inventory_item: inventoryItem })
}

List Inventory Items

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const inventoryItems =
await inventoryModuleService.list({})

res.json({ inventory_items: inventoryItems })
}

Retrieve an Inventory Item by its ID

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const inventoryItem =
await inventoryModuleService.retrieve(
request.params.id
)

res.json({ inventory_item: inventoryItem })
}

Create an Inventory Level

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const inventoryLevel =
await inventoryModuleService.createInventoryLevels({
inventory_item_id: request.body.inventory_item_id,
location_id: request.body.location_id,
stocked_quantity: request.body.quantity,
})

res.json({ inventory_level: inventoryLevel })
}

Adjust Inventory in Location

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const inventoryLevel =
await inventoryModuleService.adjustInventory(
request.body.inventory_item_id,
request.body.location_id,
request.body.new_quantity
)

res.json({ inventory_level: inventoryLevel })
}

Confirm Inventory Item Availability

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

res.json({
is_available: await inventoryModuleService.confirmInventory(
request.body.inventory_item_id,
request.body.location_id,
request.body.required_quantity
),
})
}

Create Reservation Item

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const reservationItem =
await inventoryModuleService.createReservationItems({
inventory_item_id: request.body.inventory_item_id,
location_id: request.body.location_id,
quantity: request.body.reserved_quantity,
})

res.json({ reservation_item: reservationItem })
}

Retrieve Quantity Details

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

const reservedQuantity =
await inventoryModuleService.retrieveReservedQuantity(
request.params.inventory_item_id,
[request.params.location_id]
)
const stockedQuantity =
await inventoryModuleService.retrieveStockedQuantity(
request.params.inventory_item_id,
[request.params.location_id]
)
const availableQuantity =
await inventoryModuleService.retrieveAvailableQuantity(
request.params.inventory_item_id,
[request.params.location_id]
)

res.json({
reserved_quantity: reservedQuantity,
stocked_quantity: stockedQuantity,
available_quantity: availableQuantity,
})
}

Delete a Reservation

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IInventoryService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function DELETE(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const inventoryModuleService: IInventoryService =
request.scope.resolve(ModuleRegistrationName.INVENTORY)

await inventoryModuleService.deleteReservationItems([
request.params.reservation_id,
])

res.status(200)
}

More Examples

The module interface reference provides a reference to all the methods available for use with examples for each.

Was this section helpful?