Examples of the Store Module
In this guide, you’ll find common examples of how you can use the Store Module in your application.
Create a Store
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStoreModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const storeModuleService: IStoreModuleService =
request.scope.resolve(ModuleRegistrationName.STORE)
const store = await storeModuleService.create({
name: "My Store",
supported_currency_codes: ["usd"],
})
res.json({
store,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStoreModule,
} from "@medusajs/store"
export async function POST(request: Request) {
const storeModuleService = await initializeStoreModule()
const store = await storeModuleService.create({
name: "My Store",
supported_currency_codes: ["usd"],
})
res.json({
store,
})
}
List Stores
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStoreModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const storeModuleService: IStoreModuleService =
request.scope.resolve(ModuleRegistrationName.STORE)
res.json({
stores: await storeModuleService.list(),
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStoreModule,
} from "@medusajs/store"
export async function GET(request: Request) {
const storeModuleService = await initializeStoreModule()
const salesChannels = await storeModuleService.list()
return NextResponse.json({
stores: await storeModuleService.list(),
})
}
Retrieve a Store by its ID
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStoreModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const storeModuleService: IStoreModuleService =
request.scope.resolve(ModuleRegistrationName.STORE)
const store = await storeModuleService.retrieve(
"store_123"
)
res.json({
store,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStoreModule,
} from "@medusajs/store"
export async function GET(
request: Request,
{ params }: ContextType
) {
const storeModuleService = await initializeStoreModule()
const store = await storeModuleService.retrieve(
"store_123"
)
return NextResponse.json({ store })
}
Update a Store
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStoreModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const storeModuleService: IStoreModuleService =
request.scope.resolve(ModuleRegistrationName.STORE)
const store = await storeModuleService.update("store_123", {
name: "Change Store",
})
res.json({
store,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeStoreModule,
} from "@medusajs/store"
export async function POST(
request: Request,
{ params }: ContextType
) {
const storeModuleService = await initializeStoreModule()
const store = await storeModuleService.update("store_123", {
name: "Change Store",
})
return NextResponse.json({ store })
}
Delete a Store
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IStoreModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const storeModuleService: IStoreModuleService =
request.scope.resolve(ModuleRegistrationName.STORE)
await storeModuleService.delete(request.params.id)
res.status(200)
}
import { NextResponse } from "next/server"
import {
initialize as initializeStoreModule,
} from "@medusajs/store"
type ContextType = {
params: {
id: string
}
}
export async function DELETE(
request: Request,
{ params }: ContextType
) {
const storeModuleService = await initializeStoreModule()
await storeModuleService.delete(params.id)
}
More Examples
The Store Module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?