# Order channels

Source: https://developers.swell.is/guides/core-concepts/order-channels

Every cart, order and subscription records where it came from in `channel`. `channel.type` is `channel_app` for a sale through a channel app, with `channel.app_id` naming the app, and `online_store` for every other sale. `channel.source` says where within that type it came from: an order entered in the dashboard, for example, has type `online_store` and source `admin`.

## From cart to renewal

Swell sets `channel` when it creates a cart, order or subscription, from the caller's credentials, the storefront's request or the record it comes from. Later edits and invoice payments leave it as it is. A shopper who signs up for a monthly coffee delivery on a storefront goes through these steps:

- **Cart:** the storefront creates the cart, with type `online_store`, source `storefront` and the storefront's `storefront_id`.
- **Order:** at checkout the order takes the cart's whole channel, even when another caller submits the cart.
- **Subscription:** once the order is paid, or right away for a trial, Swell creates the subscription and copies the order's channel. Its source is still `storefront`.
- **Renewal:** next month's order keeps type `online_store`, with source `subscription` and a `subscription_id` that points to the subscription.

A subscription created directly passes its channel to its first order. Only renewal orders get source `subscription`.

## A channel app's orders

A marketplace app of type `channel` creates its orders with `POST /orders` on the Backend API, with its own key or token. The order's channel names the app:

```json
{
  "channel": {
    "type": "channel_app",
    "source": "app",
    "app_id": "6af6eca9c273cd0bf466791b"
  }
}
```

Its renewals keep the type and the app:

```json
{
  "channel": {
    "type": "channel_app",
    "source": "subscription",
    "app_id": "6af6eca9c273cd0bf466791b",
    "subscription_id": "6b082cd971cc44001f36abc2"
  }
}
```

To count all of the app's orders, filter on `channel.type` and `channel.app_id` together. Filtering on source `app` alone misses its renewals.

```javascript
const { swell } = require('swell-node');

const orders = await swell.get('/orders', {
  where: {
    'channel.type': 'channel_app',
    'channel.app_id': '6af6eca9c273cd0bf466791b',
  },
});
```

## Sources

| Source | Created by | References on channel |
| --- | --- | --- |
| storefront | A storefront or its checkout | storefront_id for the storefront, app_id for its app, and key_id when it used a key of its own |
| purchase_link | A purchase link | purchase_link_id for the link that created the cart |
| admin | The Swell dashboard | None |
| app | An app's key or token | app_id for the app. An app of type channel gets type channel_app; other apps get online_store. |
| api | A secret key or a built-in integration | key_id for the key, or integration for the integration, such as zapier |
| subscription | A subscription renewal | subscription_id for the subscription, and app_id when the subscription's channel has one |

## Reading the channel

Read `channel` on carts, orders and subscriptions with the Backend API, such as `GET /orders/{id}?fields=channel`, or on the record in its events. Swell sets it: a write to `channel` is ignored, and a write to one of its parts, such as `PUT /orders/{id}/channel/type`, is refused. The Frontend API leaves it out.

A record can have no channel: one created before Swell tracked channels, or one Swell creates without a caller it can identify. An order created from a cart with no channel gets `channel: null`.

See [Order lifecycle](https://developers.swell.is/guides/core-concepts/order-lifecycle) for how an order moves from checkout to delivery.
