Skip to main content
Skip to main content

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,
})
}

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(),
})
}

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,
})
}

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,
})
}

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)
}

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?