# Headless runs and CI

> Record, check and replay API traffic in CI with cloakctl run, and run API-client collections like Newman with JUnit, JSON and HTML reports.

Source: https://docs.subcloak.com/cloak-proxy/automation/headless-and-ci/

Cloak Proxy works without its window. [`cloakctl`](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/) can wrap your test command in a capture proxy, record everything it sends, write a report, and fail the build on server errors, leaked secrets or schema changes. It can also run your [API client](https://docs.subcloak.com/cloak-proxy/api-client/overview/) collections with their tests, like Newman or the Postman CLI. Use this page to set up both on your Mac or on a macOS CI runner.

On Linux, `cloak-proxy --headless` runs the proxy without a window, with the same `cloakctl` commands and MCP server. See [Install on Linux](https://docs.subcloak.com/cloak-proxy/getting-started/install-linux/#run-it-without-a-window).

## What you can do headless

| Goal | Command |
|---|---|
| Record every API call your tests make, as HAR | `cloakctl run --ssl '*' --record run.har -- npm test` |
| Fail the build on server errors or leaked secrets | `cloakctl run --ssl '*' --fail-on-5xx --fail-on-secrets -- npm test` |
| Get a Markdown report for a pull request | `cloakctl run --ssl '*' --report report.md -- npm test` |
| Give every test request a backend trace and fail on broken propagation (added in Cloak Proxy 0.4.0) | `cloakctl run --ssl '*' --trace 'api.example.com' --record run.har -- npm test` then `cloakctl trace check --har run.har` |
| Test retries and error handling with failure scenarios (added in Cloak Proxy 0.4.0) | Export rules that include a [failure scenario](https://docs.subcloak.com/cloak-proxy/tools/failure-scenarios/) switched on, then `cloakctl run --ssl '*' --tools rules.json -- npm test` |
| Run tests offline against a recording | `cloakctl run --mock run.har -- npm test` |
| Run a collection's tests with JUnit output | `cloakctl collection run "Shop API" -r cli,junit` |
| Load test a collection | `cloakctl perf "Shop API" --vus 20 --duration 60` |
| Run a flow and fail the job if it fails (added in Cloak Proxy 0.2.0) | `cloakctl flow run "Checkout" --input sku=A1` |
| Run a monitor now and record it in its history (added in Cloak Proxy 0.2.0) | `cloakctl monitor run "Shop health"` |
| Generate a client SDK or MCP server (added in Cloak Proxy 0.2.0) | `cloakctl sdk "Shop API" --language typescript --out sdk` |
| Lint an OpenAPI spec | `cloakctl spec lint openapi.yaml` |

## Requirements

- macOS 14 Sonoma or later (Apple silicon or Intel), or Linux with the [Linux beta](https://docs.subcloak.com/cloak-proxy/getting-started/install-linux/): `cloak-proxy --headless` runs the engine on a Linux runner with the same `cloakctl` commands.
- Cloak Proxy installed, because `cloakctl` ships inside it at `/Applications/Cloak Proxy.app/Contents/Resources/cloakctl`. See [Install](https://docs.subcloak.com/cloak-proxy/getting-started/install/) and [Put cloakctl on your PATH](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/#put-cloakctl-on-your-path).
- No account and no license key. `cloakctl` is free for life, like the app.

`cloakctl run` creates the root certificate in `~/Library/Application Support/CloakProxy/` the first time it needs one. It doesn't need the certificate to be trusted in the macOS keychain, because it hands the certificate to the wrapped command through environment variables.

## Wrap a test command

Put your test command after `--`. `cloakctl run` starts the proxy, runs the command with the proxy and certificate already set, and stops when the command exits.

1. Choose which HTTPS traffic to decrypt. `--ssl '*'` decrypts every host. To be narrower, repeat `--ssl` with host patterns such as `api.example.com` or `*.example.com`. Without `--ssl`, HTTPS shows only as encrypted tunnels.

2. Add what you want out of the run: `--record` for the traffic, `--report` for a summary, and any `--fail-on-…` checks.

3. Run it:

   ```bash title="Terminal"
   cloakctl run --port 9091 --ssl '*' \
     --record run.har --redact --report report.md \
     --fail-on-5xx --fail-on-secrets \
     -- npm test
   ```

4. Read the summary on standard error and check the exit code:

   ```text
   Recorded 42 requests to run.har
   Report written to report.md
   42 requests, 9 endpoints, 0 errors, 0 possible secrets, 0 schema changes
   ```

> **Use another port while the app is open:**
> `cloakctl run` listens on `9090` unless you pass `--port`. If the Cloak Proxy app is running on `9090` on the same Mac, pass `--port 9091` (or any free port).

### The environment the command gets

The wrapped command is started through `/usr/bin/env` with these variables added to your current environment:

| Variable | Value | Who reads it |
|---|---|---|
| `HTTP_PROXY`, `HTTPS_PROXY`, `http_proxy`, `https_proxy` | `http://127.0.0.1:PORT` | curl, Python, Ruby, Go, git, Deno and most HTTP libraries |
| `NODE_USE_ENV_PROXY` | `1` | Node.js 24 and later, so its built-in `fetch` and `http` use the proxy |
| `NODE_EXTRA_CA_CERTS` | Path of the root certificate | Node.js |
| `SSL_CERT_FILE` | Path of the root certificate | OpenSSL-based tools, Ruby, Go |
| `REQUESTS_CA_BUNDLE` | Path of the root certificate | Python `requests` |
| `CURL_CA_BUNDLE` | Path of the root certificate | curl |
| `GIT_SSL_CAINFO` | Path of the root certificate | git |

(added in Cloak Proxy 0.4.2) From 0.4.2, `SSL_CERT_FILE`, `REQUESTS_CA_BUNDLE`, `CURL_CA_BUNDLE` and `GIT_SSL_CAINFO` point at `ca-bundle.pem` instead, next to the root certificate. It holds the system's trusted root certificates plus Cloak Proxy's, so the command trusts both the hosts Cloak Proxy decrypts and the ones it passes through untouched (hosts outside `--ssl`, sign-in pages). Up to 0.4.1 they point at Cloak Proxy's certificate alone, so HTTPS to a host Cloak Proxy doesn't decrypt fails to verify in tools that read them: decrypt those hosts with `--ssl` too. `NODE_EXTRA_CA_CERTS` keeps pointing at the root certificate, which Node.js adds to its own list. The bundle is written each time `cloakctl run` starts; on Linux the system's roots come from `/etc/ssl/certs/ca-certificates.crt` (or the distribution's equivalent).

Programs that ignore these variables aren't captured. See [Terminal and runtimes](https://docs.subcloak.com/cloak-proxy/devices/terminal-and-runtimes/) for runtimes that need extra setup, such as older Node.js versions or Java.

> **Shell syntax after --:**
> The command after `--` runs directly, not through a shell, so pipes, `&&` and variable expansion inside it don't work. Wrap a shell line in `sh -c`, for example `-- sh -c 'npm run build && npm test'`.

### Run without a command

Leave out `--` and the command, and the proxy runs until you press <kbd>Control</kbd> <kbd>C</kbd>, send `SIGTERM`, or `--duration` runs out. Point any app at `127.0.0.1:PORT` in the meantime. With `--duration` and a wrapped command, the command is stopped when the time is up.

## Fail the build on problems

Each `--fail-on-…` flag adds a check. If any check fails, `cloakctl run` prints `FAILED:` with the reasons and exits with `1`.

| Flag | Fails when |
|---|---|
| `--fail-on-5xx` | Any response had a 5xx status. |
| `--fail-on-error` | Any response had a 4xx or 5xx status, or any request failed (connection error, DNS failure, timeout). |
| `--fail-on-secrets` | The [secret scanner](https://docs.subcloak.com/cloak-proxy/insights/secrets/) found a high-severity secret in the traffic: for example an AWS, GitHub, Slack, OpenAI or Anthropic key, a live Stripe key, a private key, a card number, or credentials in a URL or sent over plain HTTP. |
| `--fail-on-schema-change` | An endpoint's JSON response changed shape during the run: a field was added or removed, or changed type. See [Schema changes](https://docs.subcloak.com/cloak-proxy/insights/schema-changes/). |

```text
42 requests, 9 endpoints, 3 errors, 1 possible secrets, 0 schema changes
FAILED: 2 server errors, 1 high-severity secrets
```

### Exit codes

| Exit code | Meaning |
|---|---|
| `0` | No check failed, and the wrapped command (if any) exited with `0`. |
| `1` | A `--fail-on-…` check failed, an option was wrong, or a file couldn't be read or written. |
| The command's own code | No check failed, but the wrapped command exited with a non-zero code. Your test failures still fail the build. |
| `127` | The wrapped command couldn't be started (for example, it isn't installed). |

## Security checks in CI

(added in Cloak Proxy 0.5.1)

Record only the engagement's hosts, then gate the build on what the security tools find. These commands exit with `2` when the check fails, so a CI step fails on it:

```bash title="Terminal"
cloakctl run --scope engagement.cloakscope.json --record run.cloaksession -- npm test
cloakctl ai-scan --session run.cloaksession --fail-on high
cloakctl mcp-pins check --session run.cloaksession --pins mcp-pins.json
cloakctl findings check ./pentest --fail-on high
```

| Command | Fails when |
|---|---|
| `cloakctl run --scope FILE` | Doesn't fail on its own: it records only what the [scope](https://docs.subcloak.com/cloak-proxy/security/scope/) file includes. Add `--out-of-scope block` to refuse the rest. |
| `cloakctl ai-scan --fail-on high` | A tool result or MCP response carries prompt injection at that level, or a prompt to a model provider carried a key or personal data. See [AI security](https://docs.subcloak.com/cloak-proxy/security/ai-security/#from-the-command-line). |
| `cloakctl mcp-pins check` | An MCP server in the recording lists tools that differ from the pin file, or a server isn't pinned. |
| `cloakctl findings check --fail-on high` | An engagement has an open finding at or above that severity. See [Findings and reports](https://docs.subcloak.com/cloak-proxy/security/findings/). |

## Record and report

`--record FILE` writes the traffic when the run ends. A file ending in `.har` gets [HAR](https://docs.subcloak.com/cloak-proxy/sessions/export/) that any HAR viewer opens. Any other name gets a Cloak Proxy session you can open in the app with **File › Open Session…**. See [Save and open sessions](https://docs.subcloak.com/cloak-proxy/sessions/save-and-open/).

`--redact` removes tokens, cookies, API key headers, credential parameters, password-like fields and anything the secret scanner matches, before the recording is written. Use it whenever the recording leaves your Mac, for example as a CI artifact. See [Redaction](https://docs.subcloak.com/cloak-proxy/sessions/redaction/).

`--report FILE` writes a Markdown report, the same one as **Export Report** in [Insights](https://docs.subcloak.com/cloak-proxy/insights/report/):

| Section | What it lists |
|---|---|
| Summary line | Requests, endpoints, errors, possible secrets and schema changes. |
| Endpoints | Up to 50 endpoint templates (such as `/users/{id}`) with calls, errors, p50 and p95. |
| Possible secrets | Each finding's severity, kind, where it was found, host and request id. Values are masked. |
| Security headers | An A to F grade per host with its issues. |
| Performance | Slow and large responses, missing compression, duplicates, N+1 bursts and similar findings. |
| Schema changes | Added, removed and retyped fields per endpoint. |

Empty sections are left out. The report is made before redaction, but it only ever shows masked secret values.

> **Recordings without --redact contain credentials:**
> A HAR or session recorded without `--redact` holds every cookie, bearer token and request body your tests sent. Don't upload it as a CI artifact or attach it to a public issue.

## Replay a recording offline

`--mock FILE` loads a `.har` or `.cloaksession` file as a [mock set](https://docs.subcloak.com/cloak-proxy/tools/mock-sets/) and answers matching requests from it instead of the network. A request to a recorded host that isn't in the recording gets a `404`, so nothing slips through to the real API. Requests to other hosts go to the network as usual. Record once against a real backend, then run the same tests offline or in CI without the backend:

```bash title="Terminal"
# Once, against the real API:
cloakctl run --ssl 'api.example.com' --record fixtures/api.har --redact -- npm test

# Every CI run, with no network access to the API:
cloakctl run --mock fixtures/api.har --quiet -- npm test
```

You can combine `--mock` with `--tools RULES.json` to add Map Local, Map Remote or other rules exported from the app with **Tools › Export Rules…**. See [Mock a backend](https://docs.subcloak.com/cloak-proxy/guides/mock-a-backend/).

## Run collections like Newman

`cloakctl collection run` runs a collection from your API client workspace, with its pre-request scripts, tests, auth and variables. It doesn't need the app or a proxy. Every flag is listed in [cloakctl › Run a collection](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/#run-a-collection).

```bash title="Terminal"
cloakctl collection run "Shop API" \
  --workspace ./api-workspace \
  -e Staging \
  -d "Login users" \
  --env-var "apiKey=$SHOP_API_KEY" \
  -r cli,junit,html \
  --reporter-junit-export results/api.xml \
  --reporter-html-export results/api.html
```

### Reporters

| Reporter | Output | Default file |
|---|---|---|
| `cli` | Live results in the terminal, then a summary line. | Standard output |
| `json` | The full run as JSON. | `cloak-run.json` |
| `junit` | JUnit XML, which most CI systems show as test results. | `cloak-run.xml` |
| `html` | A standalone HTML report. | `cloak-run.html` |

Pick several with commas: `-r cli,junit,json`. The folder for an export path must already exist.

### Secrets in CI

Secret variable values live in the workspace's git-ignored `.cloak/` folder, and `{{vault:name}}` values live in the macOS Keychain, so neither is in your repository. On a CI runner, pass secrets in with `--env-var KEY=VALUE` from your CI system's secret store. See [Variables and environments](https://docs.subcloak.com/cloak-proxy/api-client/variables-and-environments/).

### Collection run exit codes

`collection run` exits with `0` when every request and test passed, and `1` when any request failed to send or any test failed. `--bail` stops the run at the first failure.

## Run flows and monitors

(added in Cloak Proxy 0.2.0)

[Flows](https://docs.subcloak.com/cloak-proxy/api-client/run/flows/) and [monitors](https://docs.subcloak.com/cloak-proxy/api-client/run/monitors-and-webhooks/) run from the command line too, without the app.

```bash title="Terminal"
cloakctl flow run "Checkout" --workspace ./api-workspace -e Staging --input sku=A1
cloakctl monitor run "Shop health" --workspace ./api-workspace
```

`flow run` prints each block with a check mark or cross and exits with `1` when the flow fails. `monitor run` runs the monitor's collection with its environment, prints the same summary as `collection run`, exits with `1` when a request or test fails, and adds the run to the monitor's history. That history is saved in the workspace folder, so on a CI runner it changes a file in your checkout; don't commit it back unless you want to. See [cloakctl › Run a flow](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/#run-a-flow).

## Example: GitHub Actions

This workflow runs on a self-hosted macOS runner that has Cloak Proxy installed in `/Applications`. It runs a collection with JUnit output, then runs the app's own tests through the recorder and fails on server errors or leaked secrets.

```yaml title=".github/workflows/api-tests.yml"
name: API tests

on: [push, pull_request]

jobs:
  api:
    runs-on: [self-hosted, macOS]
    env:
      CLOAKCTL: /Applications/Cloak Proxy.app/Contents/Resources/cloakctl
    steps:
      - uses: actions/checkout@v4

      - name: Prepare results folder
        run: mkdir -p results

      - name: Run the API collection
        env:
          SHOP_API_KEY: ${{ secrets.SHOP_API_KEY }}
        run: |
          "$CLOAKCTL" collection run "Shop API" \
            --workspace ./api-workspace \
            -e Staging \
            --env-var "apiKey=$SHOP_API_KEY" \
            --bail \
            -r cli,junit \
            --reporter-junit-export results/api.xml

      - name: Lint the OpenAPI spec
        run: '"$CLOAKCTL" spec lint api-workspace/specs/shop-api.yaml'

      - name: Record and check the app's API traffic
        run: |
          "$CLOAKCTL" run --port 9091 --ssl '*' \
            --record results/traffic.har --redact \
            --report results/report.md \
            --fail-on-5xx --fail-on-secrets \
            -- npm test

      - name: Upload results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: api-results
          path: results/
```

Adjust the collection name, environment, spec path and test command to your project. `--port 9091` keeps the job clear of a Cloak Proxy app running on `9090` on the same runner.

> **Note:**
> GitHub's hosted macOS runners don't come with Cloak Proxy. Use a self-hosted Mac runner where you've installed it, or install it on the runner as part of your own setup.

## Run on a schedule

The app's [monitors](https://docs.subcloak.com/cloak-proxy/api-client/run/monitors-and-webhooks/) and scheduled [flows](https://docs.subcloak.com/cloak-proxy/api-client/run/flows/) run only while the app is open. For checks that run around the clock, schedule `cloakctl monitor run`, `cloakctl flow run` or `cloakctl collection run` with cron, launchd or your CI system's scheduled workflows. `monitor run` adds each run to the monitor's history, so you see the results in the app next time you open it.

```bash title="crontab -e"
# Every 15 minutes, run the "Shop health" monitor and record the result in its history.
*/15 * * * * /usr/local/bin/cloakctl monitor run "Shop health"
```

## Related

  - [cloakctl](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/): Every command and flag.
  - [Run API tests in CI](https://docs.subcloak.com/cloak-proxy/guides/run-api-tests-in-ci/): A complete walkthrough from collection to green build.
  - [Collection runner](https://docs.subcloak.com/cloak-proxy/api-client/run/collection-runner/): Iterations, order, folders and delays in the app.
  - [Insights report](https://docs.subcloak.com/cloak-proxy/insights/report/): The Markdown report cloakctl run --report writes.
