# Required apps

Source: https://developers.swell.is/storefronts/required-apps

A theme can require apps. Merchants get a required app when they install the theme: the install dialog installs it in the same step, or buys it if it's paid. A store can't use the theme without its required apps, and can't uninstall one while a storefront's theme requires it.

A required app is an ordinary app with its own listing in the Swell App Store, free or paid, and any theme can require it. It reaches the theme through the storefront extensions any app can declare: data sources the theme renders, actions the theme's forms post to, and embed blocks. See [Storefront extensions](https://developers.swell.is/storefronts/storefront-extensions) for the full reference, and which storefronts support each one.

## When a theme should require an app

A theme renders store data and nothing more. Features beyond display belong in an app: anything that stores records, runs logic on a server, calls an outside service or checks who a customer is. Examples are gift registries, quote requests, bookings and wholesale ordering.

Require the app when the theme's pages are built around it and don't make sense without it. A theme whose sections only add to a page when an app is there doesn't need to require it: the sections render their empty state until the merchant installs the app.

## Require an app

Name the app in `theme.apps` in the theme's `swell.json`, with `"required": true`:

**swell.json**

```json
{
  "id": "my_theme",
  "type": "theme",
  "theme": {
    "storefront": { "app": "wave" },
    "apps": [
      {
        "id": "gift_registry",
        "required": true,
        "min_version": "1.0.0",
        "why": "Lets customers create and share gift registries"
      }
    ]
  }
}
```

| Key | Description |
| --- | --- |
| id | The app's id, the id in its swell.json: 1 to 40 characters of a-z, 0-9, _ and -. It names one of your own apps, published or not, or any published app. |
| required | Set to true to require the app. Defaults to false. Only required apps are shown to merchants and enforced: an entry without it is accepted but isn't listed anywhere. |
| min_version | Optional. The lowest version of the app the theme works with, as x.y.z with digits only, such as 1.0.0. A store needs at least this version to install the theme or update to this theme version. |
| why | Optional. What the app does for the theme, 1 to 200 characters. Merchants see it next to the app. |

`theme.apps` follows these rules:

- A theme can name at most 10 apps.
- Each entry has only the keys `id`, `required`, `min_version` and `why`. Any other key is refused.
- Each app is named once.
- Every `id` must name an existing app when the theme is pushed: one of your own apps, or a published app.

`swell app push` checks the same rules before it sends the theme.

### Push order

Push the app before the theme that requires it. A theme whose `theme.apps` names an app Swell can't find is refused with:

```txt
theme.apps 'gift_registry' doesn't name an app: use the id of one of your own apps or of a published app (push the app first).
```

When a new theme version needs a new version of the app, release and publish the app first, then raise `min_version`.

### Testing on your development store

When you push a theme to your own development store and the store doesn't have the theme's required apps, `swell app push` doesn't create a test storefront, and says which apps to install. Install them, then push again.

## Connect the app to the theme

The app uses the storefront extensions every app can declare under `storefront` in its `swell.json`. They work on storefronts that support them.

- Data sources (`storefront.data`): the theme reads the app's data while the storefront renders the page, through a section field that names the app and the source, such as an `app_data` field in ui-kit themes. The section gets `{status, value}`, where `status` is `ok`, `signed_out`, `missing_app` or `error`.
- Actions (`storefront.actions`): the theme posts a form to one of the app's functions, such as with ui-kit's `AppAction`. The function answers with effects that the storefront applies: cart lines, a message, a redirect or a reload, or errors per field.
- Embeds (`storefront.embeds`): blocks that run the app's own UI in the page, which merchants place in the editor.

The app's page in the Swell App Store and the dashboard lists the data sources and actions it offers, so theme developers and merchants can see what a theme can build on.

Data and action functions are public routes. Treat every call as public: only the shopper's session says who is signed in, and the function checks that the records it returns or changes belong to that account.

Keep each setting where it's enforced. What the storefront renders, such as which pages or prices a shopper sees, is a theme setting, and it changes only what the storefront renders. Swell's Storefront API, public function routes and checkout stay open whatever the theme does, so an app whose features are only for some customers, such as approved business buyers, checks the account on its own server: in its functions and in its order hooks.

→ See [Storefront extensions](https://developers.swell.is/storefronts/storefront-extensions) for every key, limit and check.

## Example: a gift registry

This app stores gift registries in its own `registries` model. It declares one data source, the signed-in customer's registries, and one action, which creates a registry.

**swell.json (the app)**

```json
{
  "id": "gift_registry",
  "name": "Gift Registry",
  "type": "admin",
  "version": "1.0.0",
  "description": "Customers create gift registries from their account",
  "storefront": {
    "data": [
      {
        "id": "registries",
        "label": "Your registries",
        "scope": "account",
        "from": {
          "collection": "registries",
          "where": {
            "account_id": "$account.id"
          },
          "fields": [
            "name",
            "event_date"
          ],
          "sort": "event_date asc",
          "limit": 10
        },
        "sample": [
          {
            "name": "Our wedding",
            "event_date": "2027-06-12"
          }
        ]
      }
    ],
    "actions": [
      {
        "id": "create_registry",
        "label": "Create a registry",
        "function": "storefront-create-registry",
        "requires": "account",
        "fields": [
          {
            "id": "name",
            "type": "text",
            "max": 80,
            "required": true
          },
          {
            "id": "event_date",
            "type": "text",
            "max": 10
          }
        ],
        "effects": [
          "message",
          "reload"
        ]
      }
    ]
  }
}
```

The action's function:

**functions/storefront-create-registry.ts**

```typescript
export const config: SwellConfig = {
  description: 'Storefront action: create a gift registry',
  route: { methods: ['post'], public: true },
};

export async function post(req: SwellRequest) {
  // Who is signed in comes from the session, never from the body
  const accountId = (req.session as { account_id?: string } | undefined)?.account_id;
  if (!accountId) return { errors: { _form: 'Sign in to create a registry.' } };

  const input = (req.body?.input ?? {}) as { name?: unknown; event_date?: unknown };
  const name = typeof input.name === 'string' ? input.name.trim().slice(0, 80) : '';
  if (!name) return { errors: { name: 'Enter a name for the registry.' } };
  const date = typeof input.event_date === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(input.event_date) ? input.event_date : null;

  await req.swell.post('/apps/gift_registry/registries', { account_id: accountId, name, event_date: date });
  return { message: { text: 'Registry created.', tone: 'success' }, reload: true };
}
```

The theme requires the app in `theme.apps`, as in Require an app above, and a section reads the data source and posts the form:

**theme/sections/gift_registries.tsx**

```typescript
import { AppAction, Container } from 'ui-kit';
import type { SiteInfo } from 'ui-kit';

export const schema = {
  label: 'Gift registries',
  fields: [
    { id: 'heading', type: 'text', label: 'Heading', default: 'Your registries' },
    { id: 'name_label', type: 'text', label: 'Name label', default: 'Registry name' },
    { id: 'button_label', type: 'text', label: 'Button label', default: 'Create registry' },
    { id: 'registries', type: 'app_data', label: 'Registries', app: 'gift_registry', source: 'registries' },
  ],
} as const;

type Registry = { name: string; event_date?: string };
type Props = {
  site?: SiteInfo; heading?: string; name_label?: string; button_label?: string;
  registries?: { status: string; value: unknown };
};

export default function GiftRegistries({ site, heading, name_label, button_label, registries }: Props) {
  // Without the app (missing_app), or for a guest or a failed read, the section shows nothing
  if (registries?.status !== 'ok') return null;
  const list = Array.isArray(registries.value) ? (registries.value as Registry[]) : [];
  const raw = site?.app_flash;
  const flash = raw?.app === 'gift_registry' && raw.action === 'create_registry' ? raw : null;
  return (
    <section>
      <Container>
        {heading ? <h2>{heading}</h2> : null}
        {list.length ? <ul>{list.map((r, i) => <li key={i}>{r.name}</li>)}</ul> : null}
        <AppAction app="gift_registry" action="create_registry" returnTo={site?.page.path} flash={flash}>
          {({ values, errors }) => (
            <>
              <label>{name_label}<input name="name" defaultValue={values.name} /></label>
              {errors.name ? <p role="alert">{errors.name}</p> : null}
              <button type="submit">{button_label}</button>
            </>
          )}
        </AppAction>
      </Container>
    </section>
  );
}
```

When the app isn't installed, the `registries` field's status is `missing_app` and the section renders nothing, so the page has no form that can't be sent.

## How merchants get a required app

The theme's page in the Theme Store and in the dashboard shows each app the theme requires, with the theme's `why`. The install dialog shows an app the store doesn't have as **Includes <app> (<price>)**, and one it already has as **Uses <app>**.

Installing the theme installs its missing required apps in the same step, before the theme and its storefront, and updates one installed below `min_version`. A paid app is bought there under the app's own pricing, with its free trial if it has one, and is charged, or its trial starts, when the theme is installed. A paid theme is still charged at its first live publish.

Swell enforces the requirement:

- A theme's first install is refused with `required_apps` until its required apps are installed, each at `min_version` or later, for example "This theme needs Bookings. Install Bookings first." The dashboard installs them for the merchant instead of showing the error.
- A theme already in the store's library can't create a storefront, be used for a copy of one, or be given to a storefront that has no theme, until its required apps are installed.
- An app can't be uninstalled while a storefront's theme requires it. The refusal, `required_by_storefronts`, names the storefronts, for example "Bookings can’t be uninstalled: the theme of the Main storefront needs it." A theme that's only in the library doesn't block it.
- A theme update that adds a required app, or raises a `min_version`, is refused until the app is installed or updated, and the storefront keeps its theme version. The dashboard asks the merchant first.

Storefront apps never install apps. A storefront's theme picker can show which apps a theme needs and open the dashboard's install dialog. Wave, for example, shows **Needs <app> (<price>)** when the store doesn't have the app, and **Uses <app>** when it does.

## When an app is missing

A required app is installed whenever a storefront uses the theme, but it can still be disabled. An app a theme uses without requiring it can be missing too. Apps are installed per store, so one installation serves every storefront in the store.

- Editor: the storefront's editor marks sections whose app is missing, and warns on publish about each section that reads a missing app's data or posts a form to it, each required app that isn't installed, and each app older than the theme's `min_version`. The warnings don't stop publishing.
- Shoppers: a data source field gets `status: missing_app` and `value: null`, and the section renders its own empty state. An action form still renders, and when a shopper sends it, the storefront returns them to the page without calling anything.

A theme must render without broken sections when an app is missing. Give every section that reads an app's data an empty state for anything but `ok`, and render a form only when a data source from the same app answers, as in the example.

## Settings

A required app's settings are ordinary app settings, declared in its `settings/` files (see [Settings](https://developers.swell.is/apps/settings)), and merchants open them from the installed Apps list like any app. The theme's settings are separate: merchants change them in the storefront's theme editor.

## Versions and updates

A required app has its own versions and releases, and merchants update it like any other app, from the app's page. Keep each app version compatible with the theme versions merchants already have, and add data sources and actions rather than renaming or removing them: a section whose source or action is gone renders its empty state.

## Pricing

A required app keeps its own price, set in the partner dashboard like any app's, and a theme's price doesn't include it. Merchants see both in the install dialog. See [Selling themes](https://developers.swell.is/storefronts/selling-themes) and [App Store submission](https://developers.swell.is/apps/app-store-submission).

## Review

Theme Store review checks that a theme requires, in `theme.apps`, each app its pages are built around, and that each required app is published. Test the theme on a store without the app as well as with it: every page should render, with empty states in place of the app's data. Describe what each required app does for the theme in the theme's documentation.

## Requiring another developer's app

A theme can require any published app by its public id, including another developer's. Merchants get it with your theme the same way, at its own price. Its developer controls its versions, so check your theme against its new releases.

Merchants can install an app, and the Theme Store can show it, only once it's published, so release and publish your own app before the theme is listed. See [App distribution](https://developers.swell.is/apps/distribution).

The `companion` key in an app's `swell.json` is no longer used. An app pushed with it still pushes, and it's listed like any other app.
