Skip to main content
Skip to main content

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

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

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

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

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

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?