# Application Examples

These instructions apply to the current Admin Console.

Choose the example that matches what you want to deploy. Each shows the manifest and the files it expects. Use your own Worker code and Dockerfiles, or start with the [Worker sample files](https://docs.contextual.io/build/contextual-workers/quickstart.md#2-create-the-application-files) for a simple HTTP service.

For complete example projects with source files and run instructions, see the [Application Examples repository](https://github.com/ContextualIO/application-examples).

Use a tenant with Workers enabled, a signed-in [CLI configuration](https://docs.contextual.io/build/contextual-workers/quickstart.md#1-check-the-cli-and-connect-to-your-tenant), and a CLI version with `app print` and `app deploy`. The examples build images, so [configure Docker for the target's image registry](https://docs.contextual.io/build/contextual-workers/quickstart.md#1-check-the-cli-and-connect-to-your-tenant). Use IDs that are unused in the target tenant or belong to resources you intend to update.

To preview an example, run these commands from the directory that contains its manifest. Replace `my-config` with your CLI configuration ID. Pass `--file` or `--stage` when the example uses them.

```sh
ctxl app print --config-id my-config --pretty
ctxl app deploy --config-id my-config --dry-run --json --pretty
```

Check what the plan will deploy and what the target must already supply. If the dry run passes and its changes match what you intend, run the same deploy command without `--dry-run`. Confirm the destination at the prompt. See the [Applications CLI Reference](https://docs.contextual.io/build/applications/contextual-cli.md) for how to read the results.

## Deploy a single Worker

Use this layout when your repository contains one Worker. Put the Worker code and its Dockerfile under `worker/`. For an HTTP Worker, the code listens on the platform's `PORT` and the image provides a health check. The [Worker sample files](https://docs.contextual.io/build/contextual-workers/quickstart.md#2-create-the-application-files) implement both.

```text
greeting/
├── contextual.yaml
└── worker/
    ├── Dockerfile
    └── server.mjs
```

**`contextual.yaml`**

```yaml
manifestVersion: 0
application:
  id: greeting
workers:
  web:
    id: greeting-web
    build: ./worker
    size: small
```

The CLI builds the image from `worker/`, pushes it to the target's registry, and deploys `greeting-web`. Call the public URL from the Worker's **Deployment** view to verify its response.

## Deploy an Object Type and a Worker together

Use one manifest when the Worker and the Object Type definition change together. This example deploys a catalog Object Type, seeds one product record, and deploys a scheduled Worker configured with the Object Type ID.

```text
catalog/
├── contextual.yaml
├── types/
│   └── product.json
├── data/
│   └── products.jsonl
└── worker/
    ├── Dockerfile
    └── check.mjs
```

**`contextual.yaml`**

```yaml
manifestVersion: 0
application:
  id: example-catalog
objectTypes:
  products:
    definition: ./types/product.json
    data: ./data/products.jsonl
workers:
  job:
    id: example-catalog-job
    build: ./worker
    size: small
    schedule: "*/5 * * * *"
    environment:
      PRODUCT_TYPE: !ref objectTypes.products.id
```

**`types/product.json`**

```json
{
  "id": "example-catalog-product",
  "name": "Example Catalog Product",
  "type": "custom",
  "objectType": "internal",
  "display": "default",
  "defaultListStyle": "table",
  "schema": {
    "type": "object",
    "primaryKey": "id",
    "properties": {
      "id": { "type": "string" },
      "title": { "type": "string" }
    },
    "required": ["id", "title"]
  }
}
```

**`data/products.jsonl`**

```json
{"id":"kettle","title":"Electric kettle"}
```

In `worker/`, supply a Dockerfile that runs a short program such as `check.mjs`. Have it log the value of `PRODUCT_TYPE` and exit on each scheduled run.

In the plan, check that `PRODUCT_TYPE` resolves to `example-catalog-product`. The CLI saves the Object Type and adds the seed record before saving the Worker. Seed data adds missing records by primary key and leaves existing ones in place.

After deployment, read the sample record in the target tenant. Replace `my-config` with the configuration ID you deployed to.

```sh
ctxl records get native-object:example-catalog-product/kettle --config-id my-config --pretty
```

Look for `"title": "Electric kettle"`. After a schedule tick, [read the Worker logs](https://docs.contextual.io/build/contextual-workers/contextual-cli.md#read-logs) and check for `example-catalog-product`.

## Deploy alongside an existing Service

Use an Application to deploy a Worker that uses components you already deliver through a Service. The Service continues to manage its Object Type, and the Application references that type. Workers deploy separately from Service releases. See [Coordinate Worker Deployments and Service Releases](https://docs.contextual.io/build/contextual-workers/releases.md).

### Prepare the Service and tenant

1. [Install or update the Service release](https://docs.contextual.io/build/contextual-workers/releases.md#2-install-or-update-the-required-services) that supplies the `shipping-lane` Object Type. This example expects a string primary key and a string `zone` field.
2. Confirm the target has a `shipping-lane` record with primary key `smoke-lane` and `zone` set to `metro`. If the Service delivers only the type, create the sample record separately.
3. Configure a [Public Connection](https://docs.contextual.io/connect/connections/creating-a-connection.md) with ID `carrier-dev`. Set its base endpoint to an HTTPS endpoint you control. The Worker reads this value from the Connection record's `endpoint` field. Configure the endpoint to return `{"carrier":"dev-carrier"}` for `GET /zones/metro`.

### Configure the Application

```text
shipping-check/
├── contextual.yaml
├── compose.yaml
└── worker/
    ├── Dockerfile
    └── check.mjs
```

**`contextual.yaml`**

```yaml
manifestVersion: 0
application:
  id: shipping-check
variables:
  carrierConnectionId: {}
  expectedCarrier: {}
stages:
  dev:
    variables:
      carrierConnectionId: carrier-dev
      expectedCarrier: dev-carrier
objectTypes:
  lanes:
    id: shipping-lane
records:
  sampleLane:
    uri: native-object:shipping-lane/smoke-lane
  carrier:
    uri: native-object:api-configuration/${var.carrierConnectionId}
workers:
  carrier-coverage-check:
    id: carrier-coverage-check
    compose: ./compose.yaml
    size: small
    schedule: "*/5 * * * *"
    environment:
      LANE_TYPE: !ref objectTypes.lanes.id
      LANE_ID: !ref records.sampleLane.id
      CARRIER_CONNECTION_ID: !ref records.carrier.id
      EXPECTED_CARRIER: ${var.expectedCarrier}
```

**`compose.yaml`**

```yaml
x-contextual:
  version: 1
services:
  check:
    build: ./worker
    x-contextual:
      features: [native-object-api]
```

The type entry has only an `id` and the two record entries are lookups. The CLI builds and saves the Worker and leaves the existing type, lane, and Connection in place. Supply a Worker build that [reads tenant records](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md#read-tenant-records-from-a-custom-worker). Read the lane using `LANE_TYPE` and `LANE_ID` with the record token. Use the Agent token to read the Public Connection as an `api-configuration` record using `CARRIER_CONNECTION_ID`, with `doNotDecrypt=true`. Send `GET /zones/<zone>` to the Connection's `endpoint` using the lane's zone. Compare the returned `carrier` with `EXPECTED_CARRIER`, then log the result and exit. Exit with a nonzero status on failure.

Preview with the CLI configuration for the tenant where you prepared the Service and Connection. The example uses `shipping-dev` as the configuration ID.

```sh
ctxl app deploy --config-id shipping-dev --stage dev --dry-run --json --pretty
```

Expect one Worker in `changes`. A missing type, record, or Connection blocks deployment. After deployment, check the Worker logs after a schedule tick to confirm it found the `metro` lane and the Connection returned `dev-carrier`. If you reuse the manifest in another tenant, verify the same prerequisites there and add a stage with that tenant's Connection ID and expected response. Select the tenant with `--config-id` and the values with `--stage`.

## Deploy Applications independently from one repo

Give each independently deployed project its own manifest and build context. This example has two Applications in one repository. Copy the single-Worker layout for each project and give them distinct Application and Worker IDs.

```text
repo/
└── apps/
    ├── greeting/
    │   ├── contextual.yaml
    │   └── worker/
    │       ├── Dockerfile
    │       └── server.mjs
    └── status/
        ├── contextual.yaml
        └── worker/
            ├── Dockerfile
            └── server.mjs
```

**`apps/greeting/contextual.yaml`**

```yaml
manifestVersion: 0
application: { id: greeting }
workers:
  web: { id: greeting-web, build: ./worker, size: small }
```

**`apps/status/contextual.yaml`**

```yaml
manifestVersion: 0
application: { id: status }
workers:
  web: { id: status-web, build: ./worker, size: small }
```

From the repo root, select the manifest for the Application you intend to deploy.

```sh
ctxl app deploy --file apps/greeting/contextual.yaml --config-id my-config --dry-run --json --pretty
ctxl app deploy --file apps/status/contextual.yaml --config-id my-config --dry-run --json --pretty
```

Each `./worker` path is relative to its manifest's directory. Referenced files and build contexts must stay within that directory tree. Keep each Application's local `.contextual/state.json` out of version control. Each preview should list only its own Worker. Apply either independently by removing `--dry-run` from the selected command.

## Deploy two Workers from one manifest

Deploy a catalog API and a scheduled importer in one command. Each entry under `workers` has its own ID and build files. The API serves HTTP continuously, and the importer fetches the catalog on its own schedule.

[Run the full Catalog Workers example](https://github.com/ContextualIO/application-examples/tree/main/examples/catalog-workers) to try the API and importer locally or deploy them to a tenant.

```text
catalog-workers/
├── contextual.yaml
└── workers/
    ├── api/
    │   ├── compose.yaml
    │   ├── Dockerfile
    │   └── server.mjs
    └── importer/
        ├── Dockerfile
        └── import.mjs
```

**`contextual.yaml`**

```yaml
manifestVersion: 0
application:
  id: catalog-workers
workers:
  api:
    id: example-catalog-api
    compose: ./workers/api/compose.yaml
    size: small
  importer:
    id: example-catalog-importer
    build: ./workers/importer
    size: small
    schedule: "*/5 * * * *"
    environment:
      CATALOG_URL: !ref workers.api.origin
```

**`workers/api/compose.yaml`**

```yaml
x-contextual:
  version: 1
services:
  api:
    build: .
    x-contextual:
      public: 8080
      http_healthcheck:
        path: /health
        port: 8080
```

Use the [Worker sample files](https://docs.contextual.io/build/contextual-workers/quickstart.md#2-create-the-application-files) for the API. Keep `/health` and have `GET /` return a list of sample products. The API is public. See [Configure a Custom Compose Worker](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md#configure-a-custom-compose-worker) for the Compose settings.

In the importer build, have the Dockerfile run `import.mjs`. Have the script fetch `CATALOG_URL`, log the product count, and exit. On a failed request, log the error and exit with a nonzero status. The example runs the importer every five minutes in UTC.

The API uses Compose so `!ref workers.api.origin` can supply its URL to the importer. To have an `origin`, a Worker must use Compose, mark one service public, and run without a schedule. The value uses the default platform hostname for the selected tenant and silo, for example `https://example-catalog-api.service.<tenant>.my.contextual.io`.

The reference orders the API Worker's save before the importer's save. The CLI checks readiness after both saves, so an import scheduled during rollout can still fail while the API is starting.

Run the preview commands at the top of this page. In the `app print` output, `order` has a `worker` step for each Worker. The importer's step lists `workers.api` under `after`. Check that `CATALOG_URL` in the importer's `environment` matches `aliases["workers.api"].origin`. The dry run lists both Workers in `changes`.

After deployment, copy the public URL from the API Worker's **Deployment** view and check that it returns the sample products.

Start following the importer's [container output](https://docs.contextual.io/build/contextual-workers/contextual-cli.md#container-output) before the next five-minute UTC tick. The importer logs the product count and exits quickly. If you miss the count, leave the command running for the next tick.

```sh
ctxl workers logs example-catalog-importer --config-id my-config --follow
```

## Deploy a Worker with multiple containers

Use Compose when the containers should run as services within one Worker. This example exposes a web container on port 8080 and gives it the address of a helper container on port 8081.

```text
web-with-helper/
├── contextual.yaml
├── compose.yaml
├── web/
│   ├── Dockerfile
│   └── server.mjs
└── helper/
    ├── Dockerfile
    └── server.mjs
```

**`contextual.yaml`**

```yaml
manifestVersion: 0
application:
  id: web-with-helper
workers:
  app:
    id: example-web-with-helper
    compose: ./compose.yaml
    size: small
```

**`compose.yaml`**

```yaml
x-contextual:
  version: 1
services:
  web:
    build: ./web
    environment:
      HELPER_URL: http://helper:8081
    x-contextual:
      public: 8080
      http_healthcheck:
        path: /health
        port: 8080
  helper:
    build: ./helper
    environment:
      PORT: "8081"
    expose: ["8081"]
```

Supply a Dockerfile and code in each build directory. Have `web` listen on `PORT`, return a successful response at `/health`, and call `HELPER_URL`. Have `helper` read its own `PORT` value and listen on port 8081. The containers share a network, so they must listen on different ports. Services reach each other by service name. See [Configure a Custom Compose Worker](https://docs.contextual.io/build/contextual-workers/connect-to-contextual.md#configure-a-custom-compose-worker).

The plan contains two image builds and one Worker save. After deployment, call a route on the public URL that uses the helper and check its response.
