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 })
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
export async function POST(request: Request) {
const inventoryModuleService =
await initializeInventoryModule({})
const body = await request.json()
const inventoryItem =
await inventoryModuleService.create({
sku: body.sku,
title: body.title,
requires_shipping: body.requires_shipping,
})
return NextResponse.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 })
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
export async function GET(request: Request) {
const inventoryModuleService =
await initializeInventoryModule({})
const inventoryItems =
await inventoryModuleService.list({})
return NextResponse.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 })
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
type ContextType = {
params: {
id: string
}
}
export async function GET(
request: Request,
{ params }: ContextType
) {
const inventoryModuleService =
await initializeInventoryModule({})
const inventoryItem =
await inventoryModuleService.retrieve(
params.id
)
return NextResponse.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 })
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
export async function POST(request: Request) {
const inventoryModuleService =
await initializeInventoryModule({})
const body = await request.json()
const inventoryLevel =
await inventoryModuleService.createInventoryLevels({
inventory_item_id: body.inventory_item_id,
location_id: body.location_id,
stocked_quantity: body.quantity,
})
return NextResponse.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 })
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
export async function POST(request: Request) {
const inventoryModuleService =
await initializeInventoryModule({})
const body = await request.json()
const inventoryLevel =
await inventoryModuleService.adjustInventory(
body.inventory_item_id,
body.location_id,
body.new_quantity
)
return NextResponse.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
),
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
export async function POST(request: Request) {
const inventoryModuleService =
await initializeInventoryModule({})
const body = await request.json()
return NextResponse.json({
is_available: await inventoryModuleService.confirmInventory(
body.inventory_item_id,
body.location_id,
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 })
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
export async function POST(request: Request) {
const inventoryModuleService =
await initializeInventoryModule({})
const body = await request.json()
const reservationItem =
await inventoryModuleService.createReservationItems({
inventory_item_id: body.inventory_item_id,
location_id: body.location_id,
quantity: body.reserved_quantity,
})
return NextResponse.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,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
type ContextType = {
params: {
inventory_item_id: string
location_id: string
}
}
export async function POST(
request: Request,
{ params }: ContextType
) {
const inventoryModuleService =
await initializeInventoryModule({})
const reservedQuantity =
await inventoryModuleService.retrieveReservedQuantity(
params.inventory_item_id,
[params.location_id]
)
const stockedQuantity =
await inventoryModuleService.retrieveStockedQuantity(
params.inventory_item_id,
[params.location_id]
)
const availableQuantity =
await inventoryModuleService.retrieveAvailableQuantity(
params.inventory_item_id,
[params.location_id]
)
return NextResponse.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)
}
import {
initialize as initializeInventoryModule,
} from "@medusajs/inventory-next"
type ContextType = {
params: {
reservation_id: string
}
}
export async function DELETE(
request: Request,
{ params }: ContextType
) {
const inventoryModuleService =
await initializeInventoryModule({})
await inventoryModuleService.deleteReservationItems([
params.reservation_id,
])
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?