Examples of the Sales Channel Module
In this guide, you’ll find common examples of how you can use the Sales Channel Module in your application.
Create a Sales Channel
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ISalesChannelModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const salesChannelModuleService: ISalesChannelModuleService =
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
const salesChannel = await salesChannelModuleService.create({
name: request.body.name,
})
res.json({
sales_channel: salesChannel,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeSalesChannelModule,
} from "@medusajs/sales-channel"
export async function POST(request: Request) {
const salesChannelModuleService =
await initializeSalesChannelModule()
const body = await request.json()
const salesChannel = await salesChannelModuleService.create({
name: body.name,
})
return NextResponse.json({ sales_channel: salesChannel })
}
List Sales Channels
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ISalesChannelModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const salesChannelModuleService: ISalesChannelModuleService =
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
res.json({
sales_channels: salesChannelModuleService.list(),
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeSalesChannelModule,
} from "@medusajs/sales-channel"
export async function GET(request: Request) {
const salesChannelModuleService =
await initializeSalesChannelModule()
const salesChannels = await salesChannelModuleService.list()
return NextResponse.json({ sales_channels: salesChannels })
}
Retrieve a Sales Channel by its ID
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ISalesChannelModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const salesChannelModuleService: ISalesChannelModuleService =
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
const salesChannel = await salesChannelModuleService.retrieve(
request.params.id
)
res.json({
sales_channel: salesChannel,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeSalesChannelModule,
} from "@medusajs/sales-channel"
type ContextType = {
params: {
id: string
}
}
export async function GET(
request: Request,
{ params }: ContextType
) {
const salesChannelModuleService =
await initializeSalesChannelModule()
const body = await request.json()
const salesChannel = await salesChannelModuleService.retrieve(
params.id
)
return NextResponse.json({ sales_channel: salesChannel })
}
Update a Sales Channel
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ISalesChannelModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const salesChannelModuleService: ISalesChannelModuleService =
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
const salesChannel = await salesChannelModuleService.update({
id: request.params.id,
description: request.body.description,
})
res.json({
sales_channel: salesChannel,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeSalesChannelModule,
} from "@medusajs/sales-channel"
type ContextType = {
params: {
id: string
}
}
export async function POST(
request: Request,
{ params }: ContextType
) {
const salesChannelModuleService =
await initializeSalesChannelModule()
const body = await request.json()
const salesChannel = await salesChannelModuleService.update({
id: params.id,
description: body.description,
})
return NextResponse.json({ sales_channel: salesChannel })
}
Delete a Sales Channel
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ISalesChannelModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const salesChannelModuleService: ISalesChannelModuleService =
request.scope.resolve(ModuleRegistrationName.SALES_CHANNEL)
await salesChannelModuleService.delete(request.params.id)
res.status(200)
}
import { NextResponse } from "next/server"
import {
initialize as initializeSalesChannelModule,
} from "@medusajs/sales-channel"
type ContextType = {
params: {
id: string
}
}
export async function DELETE(
request: Request,
{ params }: ContextType
) {
const salesChannelModuleService =
await initializeSalesChannelModule()
await salesChannelModuleService.delete(params.id)
}
More Examples
The Sales Channel Module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?