# Product availability by sales channel

Source: https://developers.swell.is/guides/core-concepts/product-availability-by-sales-channel

A product can appear in a marketplace and be hidden from every storefront in the same store. Only active products appear in a sales channel. The Frontend API shows the Online Store catalog. On the Backend API, pass `$sales_channel` to read the catalog you want.

## Channel rules

A sales channel is a place where the merchant offers a selection of products. A slug identifies it, such as `online_store`, `tiktok` or `my_marketplace`. The slug is unique in its store environment and never changes. Live and test have separate channels.

All storefronts in a store share the built-in Online Store channel, `online_store`. It includes active products by default. The merchant sets up every other channel in the dashboard when connecting a channel app (see [Build a channel app](https://developers.swell.is/apps/build-a-channel-app-tutorial)). The channel belongs to the store, not to the app. Several apps can use one channel, and the channel keeps its products after its last app is uninstalled.

Each channel other than the Online Store has a mode, which the merchant chooses:

- `all`, "All products, including new ones": every active product is in the channel unless the merchant excludes it.
- `selected`, "Only products you add": only the products the merchant includes are in the channel.

On the product, `sales_channels.<slug>` includes (`"in"`) or excludes (`"out"`) that product. A missing or `null` value follows the channel's default, even when the product has no `sales_channels` field.

| Channel | No override | "in" | "out" |
| --- | --- | --- | --- |
| Online Store | Visible | Visible | Hidden |
| Mode all | Visible | Visible | Hidden |
| Mode selected | Hidden | Visible | Hidden |

A new channel is empty until its first mode is set. Switching modes clears that channel's overrides on all products, for every app that uses it. Switching to `selected` starts with an empty catalog. Switching to `all` includes every active product, and new ones as they are added. The merchant can also reset a channel's products: the channel keeps its mode and returns to that mode's default.

### Listing the channels

Read `GET /settings/sales_channels` on the Backend API. Scoped access needs `read_settings`. `channels` holds each channel set up in the environment. `reserved` lists the slugs Swell names itself, whether or not they are set up.

```json
{
  "channels": {
    "tiktok": { "name": "TikTok", "mode": "all" },
    "my_marketplace": { "name": "My Marketplace", "mode": "selected" }
  },
  "id": "sales_channels",
  "reserved": {
    "online_store": { "name": "Online Store" },
    "tiktok": { "name": "TikTok" },
    "meta": { "name": "Facebook & Instagram" },
    "google": { "name": "Google" },
    "amazon": { "name": "Amazon" },
    "ebay": { "name": "eBay" },
    "walmart": { "name": "Walmart" },
    "pinterest": { "name": "Pinterest" }
  }
}
```

While a mode switch or a reset runs, the channel also has an `operation` key. The channel is ready again when that key is gone. Only Swell writes this record.

## Write a product override

On a Backend API write, key `sales_channels` by channel slug. Use `online_store` or a channel set up in the environment. A write merges by key, so it leaves the product's other channels as they are. This example hides an active tote from storefronts and includes it in a marketplace in `selected` mode:

```javascript
await swell.put(`/products/${toteId}`, {
  sales_channels: {
    online_store: 'out',
    my_marketplace: 'in'
  }
});
```

The product's `sales_channels` field is then:

```json
{
  "online_store": "out",
  "my_marketplace": "in"
}
```

To change one channel, write its dotted key. To restore a channel's default, unset the key or write `null`.

```javascript
await swell.put(`/products/${mugId}`, {
  'sales_channels.my_marketplace': 'in'
});

await swell.put(`/products/${mugId}`, {
  $unset: ['sales_channels.my_marketplace']
});
```

Any caller with product write access can set an override for any channel: the dashboard, imports, API keys and apps. Scoped access needs `write_products`. The channel an app is connected to doesn't limit the channels it writes, and a change applies to every app that uses the channel. An app's hook result is the exception: Swell drops `sales_channels` from the result of a before or after hook on products.

A key that names no channel in the environment is refused with `NOT_FOUND`, and nothing is saved. A key that isn't a valid slug is refused with `INVALID`.

```json
{
  "errors": {
    "sales_channels.amazon_us": {
      "message": "Unknown sales channel 'amazon_us': use online_store or a channel configured in this environment",
      "code": "NOT_FOUND"
    }
  }
}
```

## Choose an API

- **Frontend API:** `swell-js` and the Apps SDK's `createStorefrontClient()` use a public key. `GET /products` returns active products in the Online Store.
- **Backend API:** `swell-node` uses a secret key. The Apps SDK's `SwellBackendAPI` takes an access token or a secret key, and `req.swell` in app functions uses the app's access token. With any of them, a plain `GET /products` returns every product, including inactive products and products outside every channel.

The Frontend API applies the Online Store rule to product lists, lookups by ID or slug, category products, recommendations and GraphQL product reads. An up-sell or cross-sell to a hidden product keeps its entry, with `product` set to `null`. A bundle still shows a hidden product as one of its items, since the bundle needs it. The Frontend API omits `sales_channels`, and refuses a filter on that field and the `$sales_channel` parameter.

### Adding a hidden product to a cart

A shopper who looks up the tote by ID on a storefront gets no product. The tote is still for sale: a purchase link, or a storefront cart request with its ID, can add it to a cart while it's active. To stop storefronts from adding a product to carts, make it inactive. Customers still see what they bought in past orders and subscriptions.

## Read a channel catalog

Use `$sales_channel` on Backend API product reads. It applies the active flag, the channel's default and the product's override together. A filter on the stored `sales_channels` field would miss products included by default.

Suppose the store has three active products. The mug follows both defaults. The tote has the overrides shown above. The apron is `"in"` for the marketplace and follows the Online Store default. The marketplace, `my_marketplace`, is in `selected` mode.

```javascript
// Online Store: mug and apron
await swell.get('/products', {
  $sales_channel: {
    online_store: true
  }
});

// Marketplace: tote and apron
await swell.get('/products', {
  $sales_channel: {
    channel: 'my_marketplace'
  }
});

// Both channels: apron only
await swell.get('/products', {
  $sales_channel: {
    channel: 'my_marketplace',
    online_store: true
  }
});
```

Pass a channel's slug in `channel`. `{ channel: 'online_store' }` reads the same catalog as `{ online_store: true }`. A channel with no apps reads as usual, because the catalog belongs to the channel. A slug that names no channel in the environment returns `NOT_FOUND`, and a malformed slug or selector returns `INVALID`. Other filters and pagination apply within the selected catalog.

### Rendering a storefront on the server

When you use `swell-node` for server-side rendering or a static build, use the Online Store query above. A plain `swell.get('/products')` would also publish the hidden tote and the store's inactive products.

## Good to know

- While a channel switches its mode or resets its products, a write that changes that channel's override is refused with `CHANNEL_BUSY` on `sales_channels.<slug>`, and nothing in the write is saved. This includes a new product that names the channel, and a write that would remove its value by replacing or unsetting the whole field. Leave that channel out, or retry once the switch or reset is done. Sending its current value unchanged, as in a full product round trip, is accepted.
- Channels belong to one environment. A slug set up in test can be unknown in live.
- `sales_channels` must be an object. To clear every override on a product, send `$unset: ['sales_channels']`.
- Read membership with `$sales_channel`, not from the stored overrides. During a reset the catalog already reads the default while the old overrides are removed in the background.

See [Order channels](https://developers.swell.is/guides/core-concepts/order-channels) for how carts, orders and subscriptions record the channel they came from.
