Skip to main content
Skip to main content

Sales Channels

Queries and Mutations listed here are used to send requests to the Admin Sales Channel API Routes.

All hooks listed require authentication.

A sales channel indicates a channel where products can be sold in. For example, a webshop or a mobile app. Admins can manage sales channels and the products available in them.

Related Guide: How to manage sales channels.

Mutations

useAdminCreateSalesChannel

This hook creates a sales channel.

Example

import React from "react"
import { useAdminCreateSalesChannel } from "medusa-react"

const CreateSalesChannel = () => {
const createSalesChannel = useAdminCreateSalesChannel()
// ...

const handleCreate = (name: string, description: string) => {
createSalesChannel.mutate({
name,
description,
}, {
onSuccess: ({ sales_channel }) => {
console.log(sales_channel.id)
}
})
}

// ...
}

export default CreateSalesChannel

Mutation Function Parameters

AdminPostSalesChannelsReqAdminPostSalesChannelsReqRequired
The details of the sales channel to create.

Mutation Function Returned Data

AdminSalesChannelsResAdminSalesChannelsResRequired
The sales channel's details.

useAdminUpdateSalesChannel

This hook updates a sales channel's details.

Example

import React from "react"
import { useAdminUpdateSalesChannel } from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const updateSalesChannel = useAdminUpdateSalesChannel(
salesChannelId
)
// ...

const handleUpdate = (
is_disabled: boolean
) => {
updateSalesChannel.mutate({
is_disabled,
}, {
onSuccess: ({ sales_channel }) => {
console.log(sales_channel.is_disabled)
}
})
}

// ...
}

export default SalesChannel

Hook Parameters

idstringRequired
The sales channel's ID.

Mutation Function Parameters

AdminPostSalesChannelsSalesChannelReqAdminPostSalesChannelsSalesChannelReqRequired
The details to update of the sales channel.

Mutation Function Returned Data

AdminSalesChannelsResAdminSalesChannelsResRequired
The sales channel's details.

useAdminDeleteSalesChannel

This hook deletes a sales channel. Associated products, stock locations, and other resources are not deleted.

Example

import React from "react"
import { useAdminDeleteSalesChannel } from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const deleteSalesChannel = useAdminDeleteSalesChannel(
salesChannelId
)
// ...

const handleDelete = () => {
deleteSalesChannel.mutate(void 0, {
onSuccess: ({ id, object, deleted }) => {
console.log(id)
}
})
}

// ...
}

export default SalesChannel

Hook Parameters

idstringRequired
The sales channel's ID.

Mutation Function Returned Data

DeleteResponseDeleteResponseRequired
The response returned for a DELETE request.

useAdminDeleteProductsFromSalesChannel

This hook removes a list of products from a sales channel. This doesn't delete the product. It only removes the association between the product and the sales channel.

Example

import React from "react"
import {
useAdminDeleteProductsFromSalesChannel,
} from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const deleteProducts = useAdminDeleteProductsFromSalesChannel(
salesChannelId
)
// ...

const handleDeleteProducts = (productId: string) => {
deleteProducts.mutate({
product_ids: [
{
id: productId,
},
],
}, {
onSuccess: ({ sales_channel }) => {
console.log(sales_channel.id)
}
})
}

// ...
}

export default SalesChannel

Hook Parameters

idstringRequired
The sales channel's ID.

Mutation Function Parameters

AdminDeleteSalesChannelsChannelProductsBatchReqAdminDeleteSalesChannelsChannelProductsBatchReqRequired
The details of the products to delete from the sales channel.

Mutation Function Returned Data

AdminSalesChannelsResAdminSalesChannelsResRequired
The sales channel's details.

useAdminAddProductsToSalesChannel

This hook adds a list of products to a sales channel.

Example

import React from "react"
import { useAdminAddProductsToSalesChannel } from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const addProducts = useAdminAddProductsToSalesChannel(
salesChannelId
)
// ...

const handleAddProducts = (productId: string) => {
addProducts.mutate({
product_ids: [
{
id: productId,
},
],
}, {
onSuccess: ({ sales_channel }) => {
console.log(sales_channel.id)
}
})
}

// ...
}

export default SalesChannel

Hook Parameters

idstringRequired
The sales channel's ID.

Mutation Function Parameters

AdminPostSalesChannelsChannelProductsBatchReqAdminPostSalesChannelsChannelProductsBatchReqRequired
The details of the products to add to the sales channel.

Mutation Function Returned Data

AdminSalesChannelsResAdminSalesChannelsResRequired
The sales channel's details.

useAdminAddLocationToSalesChannel

This hook associates a stock location with a sales channel. It requires the @medusajs/stock-location module to be installed in your Medusa backend.

Example

import React from "react"
import {
useAdminAddLocationToSalesChannel
} from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const addLocation = useAdminAddLocationToSalesChannel()
// ...

const handleAddLocation = (locationId: string) => {
addLocation.mutate({
sales_channel_id: salesChannelId,
location_id: locationId
}, {
onSuccess: ({ sales_channel }) => {
console.log(sales_channel.locations)
}
})
}

// ...
}

export default SalesChannel

Mutation Function Parameters

sales_channel_idstringRequired
The sales channel's ID.
location_idstringRequired
The location's ID.

Mutation Function Returned Data

AdminSalesChannelsResAdminSalesChannelsResRequired
The sales channel's details.

useAdminRemoveLocationFromSalesChannel

This hook removes a stock location from a sales channel. This only removes the association between the stock location and the sales channel. It does not delete the stock location. This hook requires the @medusajs/stock-location module to be installed in your Medusa backend.

Example

import React from "react"
import {
useAdminRemoveLocationFromSalesChannel
} from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const removeLocation = useAdminRemoveLocationFromSalesChannel()
// ...

const handleRemoveLocation = (locationId: string) => {
removeLocation.mutate({
sales_channel_id: salesChannelId,
location_id: locationId
}, {
onSuccess: ({ sales_channel }) => {
console.log(sales_channel.locations)
}
})
}

// ...
}

export default SalesChannel

Mutation Function Parameters

sales_channel_idstringRequired
The sales channel's ID.
location_idstringRequired
The location's ID.

Mutation Function Returned Data

AdminSalesChannelsResAdminSalesChannelsResRequired
The sales channel's details.

Queries

useAdminSalesChannel

This hook retrieves a sales channel's details.

Example

import React from "react"
import { useAdminSalesChannel } from "medusa-react"

type Props = {
salesChannelId: string
}

const SalesChannel = ({ salesChannelId }: Props) => {
const {
sales_channel,
isLoading,
} = useAdminSalesChannel(salesChannelId)

return (
<div>
{isLoading && <span>Loading...</span>}
{sales_channel && <span>{sales_channel.name}</span>}
</div>
)
}

export default SalesChannel

Hook Parameters

idstringRequired
The sales channel's ID.

Query Returned Data

sales_channelSalesChannelRequired
Sales Channel's details.

useAdminSalesChannels

This hook retrieves a list of sales channels. The sales channels can be filtered by fields such as q or name passed in the query parameter. The sales channels can also be sorted or paginated.

Example

To list sales channels:

import React from "react"
import { useAdminSalesChannels } from "medusa-react"

const SalesChannels = () => {
const { sales_channels, isLoading } = useAdminSalesChannels()

return (
<div>
{isLoading && <span>Loading...</span>}
{sales_channels && !sales_channels.length && (
<span>No Sales Channels</span>
)}
{sales_channels && sales_channels.length > 0 && (
<ul>
{sales_channels.map((salesChannel) => (
<li key={salesChannel.id}>{salesChannel.name}</li>
))}
</ul>
)}
</div>
)
}

export default SalesChannels

To specify relations that should be retrieved within the sales channels:

import React from "react"
import { useAdminSalesChannels } from "medusa-react"

const SalesChannels = () => {
const {
sales_channels,
isLoading
} = useAdminSalesChannels({
expand: "locations"
})

return (
<div>
{isLoading && <span>Loading...</span>}
{sales_channels && !sales_channels.length && (
<span>No Sales Channels</span>
)}
{sales_channels && sales_channels.length > 0 && (
<ul>
{sales_channels.map((salesChannel) => (
<li key={salesChannel.id}>{salesChannel.name}</li>
))}
</ul>
)}
</div>
)
}

export default SalesChannels

By default, only the first 20 records are retrieved. You can control pagination by specifying the limit and offset properties:

import React from "react"
import { useAdminSalesChannels } from "medusa-react"

const SalesChannels = () => {
const {
sales_channels,
limit,
offset,
isLoading
} = useAdminSalesChannels({
expand: "locations",
limit: 10,
offset: 0
})

return (
<div>
{isLoading && <span>Loading...</span>}
{sales_channels && !sales_channels.length && (
<span>No Sales Channels</span>
)}
{sales_channels && sales_channels.length > 0 && (
<ul>
{sales_channels.map((salesChannel) => (
<li key={salesChannel.id}>{salesChannel.name}</li>
))}
</ul>
)}
</div>
)
}

export default SalesChannels

Hook Parameters

Filters and pagination configurations applied on the retrieved sales channels.

Query Returned Data

limitnumberRequired
The maximum number of items that can be returned in the list.
offsetnumberRequired
The number of items skipped before the returned items in the list.
countnumberRequired
The total number of items available.
sales_channelsSalesChannel[]Required
An array of sales channels details.
Was this section helpful?