# cz: any language, next to unchanged software

`cz` is the Connectivitizer command line tool. It runs next to software in any language, which stays as it is and keeps speaking plain HTTP, WebSocket or any TCP protocol on localhost:

- on the **device**, `cz agent run` makes local services reachable by name;
- on the **calling side**, `cz connect` opens a local port that leads to a device's service.

It runs on desktops and servers (Windows, Linux, macOS), not on phones. Use it when there is no SDK for your language, when the software is not your own and cannot be changed, or to try things by hand.

## Install

One file, nothing else to install. These commands download it, put it on the `PATH` and make `cz` work right away, in the same window:

```powershell tab="Windows"
$ProgressPreference = 'SilentlyContinue'; $dir = "$env:LOCALAPPDATA\Programs\cz"; New-Item -ItemType Directory -Force $dir | Out-Null
Invoke-WebRequest https://connectivitizer.com/downloads/cz/cz-win-x64.exe -OutFile "$dir\cz.exe"
$path = [Environment]::GetEnvironmentVariable('Path', 'User'); if ($path -notlike "*$dir*") { [Environment]::SetEnvironmentVariable('Path', "$path;$dir", 'User') }; $env:Path += ";$dir"
cz version
```
```bash tab="macOS"
sudo mkdir -p /usr/local/bin && sudo curl -fL -o /usr/local/bin/cz https://connectivitizer.com/downloads/cz/cz-osx-arm64 && sudo chmod +x /usr/local/bin/cz
cz version
```
```bash tab="Linux"
sudo curl -fL -o /usr/local/bin/cz https://connectivitizer.com/downloads/cz/cz-linux-x64 && sudo chmod +x /usr/local/bin/cz
cz version
```
```bash tab="Raspberry Pi"
sudo curl -fL -o /usr/local/bin/cz https://connectivitizer.com/downloads/cz/cz-linux-arm64 && sudo chmod +x /usr/local/bin/cz
cz version
```

Windows: PowerShell, for your user only (no administrator rights). macOS: Apple silicon. Raspberry Pi: a 64-bit OS (Pi 4 or 5); the same file runs on any ARM64 Linux. To download a file yourself:

| System | Download |
|---|---|
| Linux x64 | https://connectivitizer.com/downloads/cz/cz-linux-x64 |
| Linux ARM64 (Raspberry Pi 4/5 with a 64-bit OS) | https://connectivitizer.com/downloads/cz/cz-linux-arm64 |
| Windows x64 | https://connectivitizer.com/downloads/cz/cz-win-x64.exe |
| macOS (Apple silicon) | https://connectivitizer.com/downloads/cz/cz-osx-arm64 |

`cz` keeps its keys and settings in a state directory (default: your user's app data folder; `--home <dir>` or `CONNECTIVITIZER_HOME` to choose another). `cz status` shows where it is. The private keys never leave it.

## What you need

Decide first which side `cz` runs on (the device, the calling side, or both), on which machine, and whether it should start with the machine. Then have these ready from the web app:

- **On the device:** a **device key** (`czd_…`, from *Credentials → Create a device key*) and the name the device should have, or a one-time **enrollment token** (from *Devices → Enroll a device*). Also the local services you want to reach: for each a name, its `host:port` and its protocol.
- **On the calling side:** an **API key** (`czk_…`, from *Credentials → Create an API key*), or none: `cz link` gets one when you approve it in the web app (below). Also the local port your software should use.
- **On both sides:** the same **shared key** (`czs_…`, from *Credentials → Generate a shared key*), if the other side is an ESP32, a phone app or any other SDK that uses a shared key. Between two `cz` (or .NET or Node.js) endpoints you can pair certificates instead (below).

## Device side

```bash
cz device login --key czd_… --name home-pc      # or: cz device enroll --token cze_… --name home-pc
cz service add api 127.0.0.1:8080 --protocol http                  # nothing is reachable until you do
cz key add default czs_…                                           # the shared key, if the other side uses one
cz agent run                                                       # keep it running
```

The target does not have to be on the same machine: `cz service add sensor 192.168.1.50:80` makes a device in the local network reachable through the machine that runs `cz`. The encrypted tunnel then ends at that machine; the last hop runs through the local network.

A running agent picks up new services and keys only after a restart. `cz status` shows what this machine is, exposes and trusts.

### Start with the machine

On Linux, a systemd unit keeps `cz agent run` going and starts it at boot. Name the state directory with `--home`, so that the service finds the keys you set up: `cz status`, run as the user who set it up, prints it as *State directory*. Put that path and that user into the unit in place of `pi`.

```ini
# /etc/systemd/system/connectivitizer.service
[Unit]
Description=Connectivitizer agent
Wants=network-online.target
After=network-online.target

[Service]
User=pi
ExecStart=/usr/local/bin/cz agent run --home /home/pi/.config/connectivitizer
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl enable --now connectivitizer
journalctl -u connectivitizer -f          # the agent's log
```

On Windows, a task in Task Scheduler that runs at startup does the same: program `cz.exe`, arguments `agent run --home <the state directory>`.

### A device that also calls

A machine that runs `cz agent run` can reach a service of another device at the same time, without an API key:

```bash
cz key add cabin czs_…                                             # the other device's shared key, named like it (or "default")
cz agent run --link cabin/api=127.0.0.1:8081                       # its service "api" on port 8081 of this machine
```

The device key this machine logged in with must have *Its devices may open sessions to other devices* switched on in the web app; otherwise the log says `FORBIDDEN`. `cz` keeps the session open and opens it again when it ended. `--link` can be given several times.

## Calling side

```bash
cz key add living-room-lamp czs_…          # the shared key, named like the device (or "default")
cz connect living-room-lamp/api --listen 127.0.0.1:18080 --api-key czk_…
curl http://127.0.0.1:18080/state          # your software talks to the local port
```

`cz` picks the shared key named like the device, else the only one it has, else the one named `default`.

`cz connect` prints how the session runs:

```
Connected to Living room lamp (living-room-lamp/api).
  listening on          127.0.0.1:18080
  mode                  direct, over udp-kcp
  peer address          203.0.113.7:54249 (in a direct session each side sees the other's public address)
  end-to-end encrypted  yes
  platform can read     no
```

A relayed session says `e2e-relay` as its mode, followed by the reason it is not direct (see [Troubleshooting](https://connectivitizer.com/guides/troubleshooting.md)). Ctrl+C ends the session.

Instead of `--api-key`, you can get the tool a key of its own: `cz link --name my-tool` prints a page and a code; you open the page, check that it shows the same code, and approve. `cz` then prints the API key once. Keys can also come from the environment (`CZ_API_KEY`, `CZ_SHARED_KEY`) rather than the command line, which keeps them out of the shell history.

## A device's own screen in your browser: `cz ui`

A device can bring a screen of its own, one HTML page it serves itself ([Device screens](https://connectivitizer.com/guides/device-screen.md) says how). The Connectivitizer app shows it on a phone; `cz ui` shows it in the browser of the computer `cz` runs on: Windows, Linux, macOS, a Raspberry Pi with a display.

```bash
cz key add living-room-lamp czs_…
cz ui living-room-lamp --api-key czk_…
```

`cz` connects to the device's service named `ui`, else `api` (name another one with `cz ui living-room-lamp/panel`), reads `/.cz/ui.json`, and opens the page in your browser. It prints the address too; `--no-open` only prints it, for a machine where `cz` cannot start a browser.

```
Connected to Living room lamp (living-room-lamp/api): Lamp, version 1.
  mode                  direct, over udp-kcp
  end-to-end encrypted  yes, up to this program
  platform can read     no
The device's screen is served on this machine only, for the browser that opens this address:
  http://127.0.0.1:51823/.cz-host/open?t=…
```

What to know:

- The session and its keys stay in `cz`. The browser talks to `cz` on `127.0.0.1`; the encryption ends in `cz`, on your own machine.
- Only the browser that opened the printed address can use it: the address carries a secret of this run, and requests from other sites are refused. Open it on the same machine; it is not reachable from another one.
- In the page, `fetch("/state")` and `connectivitizer.webSocket("/events")` reach the device as they do in the app. `connectivitizer.host` is `{ name: "cz", platform: "desktop" }`.
- As in the app, the page reaches its device and may load from `https:` (your own servers, a CDN, map tiles); it cannot keep data in the browser. For the camera, microphone or location the browser asks you, as for any website.
- Ctrl+C ends the session and the local address with it.

The Connectivitizer web app does not show device screens, on purpose: your shared key would have to be used on our website.

## Certificates and pairing (between cz and .NET or Node.js endpoints)

Instead of a shared key, two endpoints can each have an identity of their own and pin each other's once. That takes three steps and two strings, which you copy from one machine to the other:

```bash
cz pair create            # on the device: prints an invitation (czp1.…)
cz pair accept czp1.…     # on the calling side: pins the device, prints a response (czr1.…)
cz pair complete czr1.…   # on the device: verifies the response, pins the application
```

The invitation is valid for a limited time and can be used once. An agent that is already running uses the new trust after a restart.

## Shared keys

```bash
cz key new [<name>]           # make a key here; it is printed as text this one time
cz key add <name> czs_…       # store a key made elsewhere (on the calling side, name it like the device)
cz key list                   # the names
cz key qr <name>              # the key as a QR code for the Connectivitizer app (--plain, --svg <file> [--open])
cz key remove <name>
```

The keys stay in the state directory and are never sent to Connectivitizer. `cz key qr` never prints the key as text, so it does not end up in the terminal's scrollback; the code is still the key, so show it on your own screen only. A running agent takes a new key after a restart.

## Keys and devices without the web app

`cz` runs endpoints. Creating keys, switching what a key may do and looking at devices and sessions is done by the command `connectivitizer` of the npm package, from a shell or from an AI client: see [Workspace tools](https://connectivitizer.com/guides/workspace-tools.md). Both use the same state directory.

## Direct connections

Direct sessions (UDP, and IPv6) need no settings. `cz` can also connect directly over TCP when one side has a port forwarded by its router: `--tcp-port 47000 --public-tcp 203.0.113.7:47000` (on `cz agent run`, `cz connect` or `cz ui`). Why a session was relayed: [Troubleshooting](https://connectivitizer.com/guides/troubleshooting.md). Errors: [Error codes](https://connectivitizer.com/guides/errors.md).

## A screen in the Connectivitizer app

Your device can bring its own screen: one HTML page that the Connectivitizer app shows when someone taps the device, so there is no app to write. The device serves two files on the service the app connects to (`ui`, else `api`): `GET /.cz/ui.json`, the manifest `{"name": "Desk lamp", "entry": "/.cz/ui.html", "version": "1"}`, and `GET /.cz/ui.html`, the page.

The page, as `ui.html`: a switch, a slider and a value the device reports, with a comment at each part to show where yours go.

```html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- CSS: how the page looks -->
<style>
  body { margin: 0; padding: 24px; font: 16px system-ui, sans-serif; background: #fdf6e3; color: #3b3526; }
  h1 { font-size: 44px; margin: 4px 0 24px; }
  label { display: block; margin: 28px 0 8px; }
  button { font: inherit; padding: 12px 24px; border-radius: 12px; border: 0; background: #b58900; color: #fff; }
  input, select { font: inherit; padding: 10px; border-radius: 10px; border: 1px solid #c8b98a; background: #fff; color: inherit; }
  input[type=range] { width: 100%; padding: 0; border: 0; background: none; accent-color: #b58900; }
</style>

<!-- HTML: what is on the page. Each id connects an element to the script below. -->
<p>Desk lamp</p>
<h1 id="state">…</h1>
<button id="toggle">Switch</button>

<label for="level">Brightness: <b id="level-value">…</b> %</label>
<input id="level" type="range" min="0" max="100" step="5">

<p>Running for <b id="uptime">…</b> s</p>

<!-- JavaScript: what happens. fetch('/…') goes to the device, through the app. -->
<script>
  const $ = (id) => document.getElementById(id);

  // The device answers every request with its state, e.g. {"on":true,"level":40,"uptime":120}.
  // show() puts it on the page: one line per value.
  const show = async (response) => {
    const s = await response.json();
    $('state').textContent = s.on ? 'On' : 'Off';
    $('level-value').textContent = s.level;
    if (document.activeElement !== $('level')) $('level').value = s.level;
    $('uptime').textContent = s.uptime;
  };
  const refresh = () => fetch('/state').then(show).catch(() => { $('state').textContent = 'No /state yet'; });

  // A tap or a change sends a request; the answer is shown right away.
  $('toggle').onclick = () => fetch('/toggle', { method: 'POST' }).then(show);
  $('level').oninput = () => { $('level-value').textContent = $('level').value; };
  $('level').onchange = () => fetch('/level?value=' + $('level').value, { method: 'POST' }).then(show);

  refresh();
  setInterval(refresh, 2000);   // new values every 2 seconds
</script>
</html>
```

The page comes from your program's own web server, the one `cz service add api 127.0.0.1:8080` exposes; `cz ui <device>` shows it on a computer (above). To try a page before your program serves it:

```bash
# Your program's web server answers GET /.cz/ui.json and GET /.cz/ui.html, and the page's own
# requests: GET /state, POST /toggle and POST /level?value=40, each with {"on":…,"level":…,"uptime":…}.
# To see the page first, any static server will do (the switch and the slider need your program):
mkdir -p screen/.cz
cp ui.html screen/.cz/ui.html   # the page
echo '{"name":"Desk lamp","entry":"/.cz/ui.html","version":"1"}' > screen/.cz/ui.json
python3 -m http.server 8080 --bind 127.0.0.1 --directory screen

# exposed as "api" by: cz service add api 127.0.0.1:8080 --protocol http
```

More controls to copy, a preview on your computer that needs no device, what a page can do (colours for the app's bar, the camera, the location, links), and finished examples, with a short start for anyone new to HTML: [Device screens](https://connectivitizer.com/guides/device-screen.md).
