Skip to main content
Skip to main content

Examples of the Stock Location Module

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

Create a Stock Location

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

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)

const stockLocation = await stockLocationModuleService.create(
{
name: request.body.name,
}
)

res.json({
stock_location: stockLocation,
})
}

List Stock Locations

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

export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)

res.json({
stock_locations: await stockLocationModuleService.list({}),
})
}

Add Address to Stock Location

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

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)

const stockLocation = await stockLocationModuleService.update(
{
id: request.params.id,
address: {
country_code: request.body.country_code,
city: request.body.city,
address_1: request.body.address_1,
postal_code: request.body.postal_code,
},
}
)

res.json({
stock_location: stockLocation,
})
}

Delete a Stock Location

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

export async function DELETE(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const stockLocationModuleService: IStockLocationService =
request.scope.resolve(ModuleRegistrationName.STOCK_LOCATION)

await stockLocationModuleService.delete(request.params.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?