# Sites CLI Reference

These instructions apply to the current Admin Console.

{% hint style="info" %}
Contextual Sites run on Contextual Workers, which are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Workers be enabled for your tenant.
{% endhint %}

This documentation assumes you are running Contextual CLI 0.17.0 or later. Check your version with `ctxl --version` and follow [CLI installation and tenant sign-in](https://docs.contextual.io/build/contextual-workers/quickstart.md#1-check-the-cli-and-connect-to-your-tenant).

Replace `my-config` with the CLI configuration ID for the intended tenant. The examples use Site ID `customer-portal` and Auth Profile ID `customer-portal-auth`; substitute your own IDs when working with existing resources. Confirm the tenant with `ctxl config get my-config --json` before writing.

{% hint style="warning" %}
CLI writes affect the selected tenant immediately. Creating an Auth Profile or Site User also provisions identity-provider resources. Review the target configuration and payload before each write.
{% endhint %}

## Understand the Sites Resource Model

| Resource | CLI Type ID | Relationships |
| --- | --- | --- |
| Site | `site` | Holds application identity and optional `authProfileId` and `testAuthProfileId` references. |
| Auth Profile | `auth-profile` | Selects the Stytch configuration that Sites use for sign-in. |
| Site User | `site-user` | Belongs to one Auth Profile through `authProfileId`; supplies identity and an application role. |

```text
site.authProfileId     -> auth-profile <- site-user.authProfileId
site.testAuthProfileId -> auth-profile (intended for local development)
```

A Site User can sign in to Sites that reference the same Auth Profile. Selecting a Test Auth Profile records the intended local profile; it does not configure the local application automatically. See [About Auth Profiles](https://docs.contextual.io/build/sites/manage-auth-profiles.md).

Hosting is a separate Worker operation. A [Site Worker](https://docs.contextual.io/build/sites/site-workers.md#understand-the-site-binding) uses `managed-site-worker` with `SITE_ID` to select the Site and `IMAGE` to select the application image. Follow [Deploy and Update](https://docs.contextual.io/build/sites/releases.md) for those Site-specific selections and the [Workers CLI Reference](https://docs.contextual.io/build/contextual-workers/contextual-cli.md) for image builds, create/update commands, deployment inspection, and logs.

## Initialize Site Source

After you [create a Site](https://docs.contextual.io/build/sites/manage-sites.md#create-a-site), use `ctxl workers source` to get its Astro starter as a GitHub repository or a local ZIP. Both commands require permission to read the Site and create Workers in the selected tenant.

### Create a GitHub Repository

Use a [GitHub Connection](https://docs.contextual.io/build/contextual-workers/contextual-cli.md#deploy-from-github) for the account that will own the repository. Replace `github` with its Connection ID. You need read and update permissions for the Connection and update permission for the Site.

```bash
ctxl workers source clone astro-core --site customer-portal \
  --connection github --config-id my-config
```

The command creates a private GitHub repository, configures its Actions variables, and links it to the Site. The response includes the repository URL and the Worker ID. Use the URL with `git clone` to work on the code locally. To create a public repository, pass `--public`. If the repository name is taken or setup fails partway through, see [Site Source Initialization Fails](https://docs.contextual.io/build/sites/troubleshooting.md#site-source-initialization-fails).

### Download a ZIP

```bash
ctxl workers source download astro-core --site customer-portal \
  --dir ./sites --config-id my-config
```

The command saves the same starter files and prints the ZIP path in the `file` field. It creates the destination directory if needed and defaults to the current directory when you omit `--dir`. Downloading a ZIP requires no GitHub Connection.

### Choose Source and Worker Settings

Both commands accept `--name` to choose the repository or ZIP folder name and `--branch` to select a branch of the starter template. The generated `contextual-deploy.env` uses `<site-id>-site-worker` by default. To set a different Worker ID, pass `--worker-id` with an unused ID. An explicit ID that already exists returns `409`. To download the starter again for an existing Worker with the default ID, omit `--worker-id`.

## Inspect a Sites Resource

Read the deployed schema before preparing a payload. System types are not normally returned by `ctxl types list`; address `site`, `auth-profile`, or `site-user` explicitly:

```bash
ctxl types get native-object:site --config-id my-config --pretty
ctxl types get native-object:auth-profile --config-id my-config --pretty
ctxl types get native-object:site-user --config-id my-config --pretty
```

Use projected lists for routine discovery. These commands omit provider credentials:

```bash
ctxl records list \
  --type site \
  --fields id,name,authProfileId,testAuthProfileId \
  --config-id my-config --pretty

ctxl records list \
  --type auth-profile \
  --fields id,name,description,provider \
  --config-id my-config --pretty

ctxl records list \
  --type site-user \
  --exact-search authProfileId=customer-portal-auth \
  --fields id,email,name,role,authProfileId \
  --config-id my-config --pretty
```

Retrieve a specific record with `--type` and `--id`, for example:

```bash
ctxl records get \
  --type site --id customer-portal \
  --config-id my-config --pretty
```

Your CLI identity needs read permission for inspection, create permission for new records, and update permission for patches on the corresponding type. Creating or deleting a Site User also needs [Auth Profile permissions](https://docs.contextual.io/build/sites/manage-site-users.md#permissions-to-create-a-site-user). Action execution has an [additional permission requirement](#run-sites-actions).

## Create an Auth Profile or Site User

`ctxl records add` reads JSONL: one complete JSON object per line. Create an Auth Profile before adding its Site Users. For Site creation, use [Create a Site](https://docs.contextual.io/build/sites/manage-sites.md#create-a-site) in the console.

### Create a Managed Auth Profile

Save this one-line object as `auth-profile.jsonl`:

```json
{"id":"customer-portal-auth","name":"Customer portal authentication","description":"Users who can sign in to the customer portal","provider":"managed-stytch"}
```

```bash
ctxl records add \
  --type auth-profile --input-file auth-profile.jsonl \
  --config-id my-config --pretty
```

This example creates a Managed Stytch profile; Contextual supplies the provider configuration. Create BYO Stytch profiles in the [console](https://docs.contextual.io/build/sites/manage-auth-profiles.md#choose-an-authentication-provider) so the Stytch secret stays out of shell commands, files, and coding-agent prompts. Keep the Auth Profile ID and provider unchanged after creation.

### Create a Site User

Save this one-line object as `site-user.jsonl`:

```json
{"email":"site.user@example.com","name":"Site User","role":"member","authProfileId":"customer-portal-auth"}
```

```bash
ctxl records add \
  --type site-user --input-file site-user.jsonl \
  --config-id my-config --pretty
```

`email`, `role`, and `authProfileId` are required; `name` is optional. The role must be `member` or `admin`. Omit `id` so the platform generates it, and omit the provider-managed `externalId`. Record the returned ID for later updates. After either create, use the projected lists above to verify the saved values before continuing.

## Apply Common Sites Updates

Inspect the current schema and record first. Keep the Site ID and the Auth Profile ID/provider unchanged. For Site Users, keep the ID, email, and Auth Profile unchanged; update the name or role.

Connect an existing Auth Profile to a Site:

```bash
ctxl records patch \
  --type site --id customer-portal \
  --set authProfileId=customer-portal-auth \
  --config-id my-config --pretty
```

Follow [Apply Site Configuration Changes](https://docs.contextual.io/build/sites/manage-sites.md#apply-site-configuration-changes) for configuration refresh and pinned Site bindings. If the patch adds Managed Stytch to a running Site that had no authentication or used BYO Stytch, restart or update its Worker to register its origins. Then verify hosted sign-in, sign-out, and access to protected routes.

Change a Site User's application role, replacing `SITE_USER_ID` with the returned record ID:

```bash
ctxl records patch \
  --type site-user --id SITE_USER_ID \
  --set role=admin \
  --config-id my-config --pretty
```

Retrieve the record again after a patch to verify the change. See [Update a Site User](https://docs.contextual.io/build/sites/manage-site-users.md#update-a-site-user) for when saved name and role changes appear in the application.

## Run Sites Actions

The Auth Profile's `get-config` action requires `execute` permission on `auth-profile`. Record read/create/update permissions do not grant action execution. If the permission editor does not expose the required **Execute** grant, or the action is forbidden, [contact Contextual support](mailto:help@contextual.io) with the CLI version, tenant and Auth Profile IDs, and the non-secret error.

### Retrieve Local Auth Configuration

For the Astro starter's local development only, save this object as `auth-config-params.json`:

```json
{"environment":"test"}
```

```bash
ctxl records execute native-object:auth-profile/customer-portal-auth \
  --action-id get-config \
  --params-file auth-config-params.json \
  --config-id my-config --pretty
```

`--params-file -` reads the same JSON object from stdin. The result contains `authProfileId`, `organizationSlug`, `projectId`, and `publicToken`, without the Stytch secret. Put it in the `authConfig` field of the ignored `site-auth.local.json` file, following [Use an Auth Profile During Local Development](https://docs.contextual.io/build/sites/manage-auth-profiles.md#use-an-auth-profile-during-local-development). A hosted Site Worker loads its configuration automatically.

For Managed Stytch, `environment: "test"` selects the managed test configuration. BYO profiles return their stored project configuration; choose the separate non-production profile for local development. See [Configure BYO Stytch for Production and Local Development](https://docs.contextual.io/build/sites/manage-auth-profiles.md#configure-byo-stytch-for-production-and-local-development).

The Astro local helper invokes `find-or-create-site-user` automatically. Do not run it manually. Its `ctxlConfigId` setting selects a named CLI configuration; an empty value uses the current configuration. Keep CLI credentials separate from the Site repository.

## Keep Authentication and Authorization Separate

Keep Stytch secrets in the Auth Profile and Tenant API credentials out of browser code and source control. A Site User role does not grant tenant API permissions; follow [Site User Roles and Authorization](https://docs.contextual.io/build/sites/manage-site-users.md#understand-site-user-roles-and-authorization) when implementing server endpoints.
