# Author and publish policies with the CLI (/docs/preference-management/policy-engine/author-policies-with-the-cli)

Admin Dashboard pages: /infrastructure/api-keys

Build a Permissions policy on your machine, test it, and make it live with the Transcend CLI. For how decisions work, see [Policy Engine](./index.mdx).

To let a coding agent do these steps, see [Author policies with an AI agent](./author-policies-with-ai-agents.mdx).

## Prerequisites [#prerequisites]

- Node.js 22.12 or later.
- An API key with the **Manage Policy** scope to upload versions. Activating a version needs the **Activate Policy** scope. Create the key under [**Developer Tools → API Keys**](https://app.transcend.io/infrastructure/api-keys).
- An API key with the **View Managed Consent Database Admin API** or **Modify User Stored Preferences** scope, to call the Permissions API in the last step.
## 1. Install the tools [#1-install-the-tools]

Install the Transcend CLI, version 13 or later:

```shellscript
npm install --global @transcend-io/cli
```

Install [OPA](https://www.openpolicyagent.org/docs#1-download-opa) version 1 or later and [Regal](https://www.openpolicyagent.org/projects/regal), the Rego linter. On macOS with Homebrew:

```shellscript
brew install opa regal
```

## 2. Create a policy workspace [#2-create-a-policy-workspace]

From the root of your repository, run:

```shellscript
transcend policy init
```

`init` creates an empty workspace in `transcend/policy`, with shared Regal settings and a README. It offers editor settings, the Policy Engine agent skill, and a GitHub Actions check. It needs no credentials.

## 3. Add a Permissions bundle [#3-add-a-permissions-bundle]

```shellscript
transcend policy new --template permissions --name permissions --yes
```

The command creates `transcend/policy/permissions-bundle`:

<FileTree>
  <FileTreeFolder name="permissions-bundle">
    <FileTreeFile name=".manifest" description='OPA manifest; roots: ["permissions"]' />
    <FileTreeFile name="input.schema.json" description="Permissions API input schema" />
    <FileTreeFile name="input.example.json" description="Example input, committed" />
    <FileTreeFile name="input.json" description="Local scratch input, gitignored" />
    <FileTreeFolder name="permissions">
      <FileTreeFolder name="config">
        <FileTreeFile name="config.rego" />
        <FileTreeFile name="data.json" description="Purpose slugs the policy knows about" />
      </FileTreeFolder>
      <FileTreeFolder name="helpers">
        <FileTreeFolder name="preference">
          <FileTreeFile name="preference.rego" description="enabled(), disabled(), undecided()" />
          <FileTreeFile name="preference_test.rego" />
        </FileTreeFolder>
      </FileTreeFolder>
      <FileTreeFolder name="purposes">
        <FileTreeFile name="entrypoint.rego" description="package permissions.purposes" />
        <FileTreeFolder name="analytics">
          <FileTreeFile name="analytics.rego" description="The Analytics decision" />
          <FileTreeFile name="analytics_test.rego" />
        </FileTreeFolder>
      </FileTreeFolder>
    </FileTreeFolder>
  </FileTreeFolder>
</FileTree>

Three names appear in this guide. `--name` is the Rego package root. The folder is `permissions-bundle`. The remote bundle name, set when you publish, must be `permissions` for the Permissions API.

## 4. Write a purpose rule [#4-write-a-purpose-rule]

Add one package under `permissions/purposes` for each purpose your application asks about. List each purpose slug in `permissions/config/data.json`.

This example allows the SaleOfInfo purpose only after an explicit opt-in. When the user never chose and the caller says they are in California, it escalates. Save it as `permissions/purposes/sale_of_info/sale_of_info.rego`:

```rego
# METADATA
# title: Sale of personal information decision
# description: Allows sale only after an explicit opt-in.
package permissions.purposes.sale_of_info

import rego.v1

import data.permissions.helpers.preference

default result := {
	"decision": "deny",
	"reason_code": "default_deny",
}

result := {
	"decision": "allow",
	"reason_code": "explicit_allow",
} if {
	preference.enabled("SaleOfInfo")
}

result := {
	"decision": "escalate",
	"reason_code": "undecided_in_california",
} if {
	preference.undecided("SaleOfInfo")
	input.context.region == "US-CA"
}
```

The response key for this purpose is `sale_of_info`, the last segment of the package name. Keep the conditions of each `result` rule mutually exclusive. Two matching rules with different values make the evaluation fail.

## 5. Add tests [#5-add-tests]

Save tests next to the policy, in a file that ends in `_test.rego`. The CLI does not upload test files.

```rego
package permissions.purposes.sale_of_info_test

import rego.v1

import data.permissions.purposes.sale_of_info

test_opt_in_allows if {
	result := sale_of_info.result with input as {"preferences": [{"name": "SaleOfInfo", "choice": true}]}
	result.decision == "allow"
}

test_undecided_in_california_escalates if {
	result := sale_of_info.result with input as {
		"preferences": [{"name": "SaleOfInfo", "choice": null}],
		"context": {"region": "US-CA"},
	}
	result.decision == "escalate"
}
```

## 6. Check, test, and evaluate [#6-check-test-and-evaluate]

Run the full gate. It checks the manifest roots, OPA formatting, strict compilation, Regal lint, and tests:

```shellscript
transcend policy check
```

Add `--fix` to apply formatting and safe lint fixes. Run only the tests with `transcend policy test`.

Evaluate the policy against an input file to see the response your application will get:

```shellscript
transcend policy eval transcend/policy/permissions-bundle \
  --package=data.permissions.purposes \
  --input=transcend/policy/permissions-bundle/input.json \
  --schema=transcend/policy/permissions-bundle/input.schema.json
```

Edit `input.json` to try other preferences and context values. `--schema` rejects input that does not match the Permissions API input schema.

## 7. Publish a version [#7-publish-a-version]

Upload the bundle as a new version. The first upload also creates the remote bundle.

```shellscript
transcend policy publish transcend/policy/permissions-bundle \
  --remote-bundle-name=permissions \
  --auth="$TRANSCEND_API_KEY" \
  --version="permissions-v1" \
  --description="Require opt-in for SaleOfInfo"
```

Without `--version`, the label is the bundle name and a timestamp. The uploaded version is not live yet. On US hosting, add `--transcend-url=https://api.us.transcend.io` to this and every later command.

## 8. Activate the version [#8-activate-the-version]

```shellscript
transcend policy activate \
  --remote-bundle-name=permissions \
  --version="permissions-v1" \
  --auth="$TRANSCEND_API_KEY"
```

Without `--version`, the CLI activates the most recently uploaded version. Add `--dry-run` to see which version it would activate.

## 9. Call the Permissions API [#9-call-the-permissions-api]

Send a user identifier and context to the Permissions API for one Preference Store partition. Use the partition key from [Partition Keys](../partition-keys.mdx):

```shellscript
curl -X POST "https://multi-tenant.sombra.transcend.io/v1/permissions/$PARTITION" \
  -H "Authorization: Bearer $TRANSCEND_PREFERENCES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "identifier": { "name": "email", "value": "user@example.com" },
    "context": { "region": "US-CA" }
  }'
```

Replace the host with your organization's Sombra URL. On US hosting, Transcend-hosted Sombra is `https://multi-tenant.sombra.us.transcend.io`. The identifier name must be one configured for your Preference Store. The response has one decision per purpose, as described in [Policy result](./index.mdx#policy-result).

## Roll back [#roll-back]

List the uploaded versions, then activate an earlier one:

```shellscript
transcend policy versions --remote-bundle-name=permissions --auth="$TRANSCEND_API_KEY"
transcend policy activate --remote-bundle-name=permissions --version="permissions-v0" --auth="$TRANSCEND_API_KEY"
```

`transcend policy deactivate --remote-bundle-name=permissions` stops serving the bundle. The Permissions API then returns 503 for every call, so prefer rolling back to a known good version.

## Command reference [#command-reference]

| Command                       | Does                                                       | Scope           |
| ----------------------------- | ---------------------------------------------------------- | --------------- |
| `transcend policy init`       | Creates a workspace, with optional editor, skill, and CI   | None            |
| `transcend policy new`        | Adds a bundle from the `permissions` or `generic` template | None            |
| `transcend policy check`      | Runs every local check                                     | None            |
| `transcend policy lint`       | Runs OPA formatting and Regal only                         | None            |
| `transcend policy test`       | Runs OPA tests                                             | None            |
| `transcend policy eval`       | Evaluates a bundle against an input file                   | None            |
| `transcend policy publish`    | Uploads a new version                                      | Manage Policy   |
| `transcend policy activate`   | Makes a version live                                       | Activate Policy |
| `transcend policy deactivate` | Stops serving a bundle                                     | Activate Policy |
| `transcend policy bundles`    | Lists remote bundles                                       | View Policy     |
| `transcend policy versions`   | Lists the versions of a bundle                             | View Policy     |
| `transcend policy download`   | Downloads a version, by default the active one             | View Policy     |

Run any command with `--help` for every flag. The CLI reads `TRANSCEND_API_KEY` when you omit `--auth`, and `TRANSCEND_API_URL` when you omit `--transcend-url`.
