# Instructions for coding agents: adding Connectivitizer to a project

This page is for an AI coding assistant that works on a project with Connectivitizer. The *Quickstart* page of the web app gives you instructions to paste into your assistant; they point here. If you write the code yourself, start with the guide for your language ([overview of the guides](https://connectivitizer.com/llms.txt)); the rules in section 3 hold for your own code too.

The rest of this page speaks to the assistant.

You are helping a person connect their software through Connectivitizer. Everything you need is on this installation's web app, https://connectivitizer.com; you do not need the Connectivitizer source code, and the person usually does not have it. The instructions they pasted from the web app name the language of each side and the values from their workspace that are not secret (API address, device name, service name).

## How Connectivitizer works, in short

- There are two sides. The **device** has the service (a web server, an API, any TCP port) and runs a Connectivitizer agent: an SDK inside its software, or the command line tool `cz` next to it. The **calling application** connects to the device by the device's name and uses the service as if it were local.
- The agent opens no port. It keeps one outbound connection to the platform. Only services the device itself declares (exposes) can be reached; the platform learns their names, never their local addresses.
- A device can be the calling side for another device as well, with its device key alone, when that key has *Its devices may open sessions to other devices* switched on in the web app. Every SDK with a device side does this (ESP32: `link`; the others: `connect` on the agent); the guide of the language shows it under "A device that calls another device".
- A session runs **directly** between the two endpoints (UDP, hole punching) when the networks allow it, else **through the relay**. Both are encrypted end to end between the two endpoints. Which one a session uses is decided per session; the SDKs do everything needed for direct sessions themselves (local addresses, IPv6, UDP).
- The two endpoints trust each other through either a **shared key** (`czs_…`, the same on both, never sent to the platform) or **certificates and pairing** (`cz`, .NET and TypeScript on Node.js only). Use the shared key unless the person wants certificates.

## 1. Read the guides for the two sides

The instructions the person pasted from the web app list one guide per side, for example https://connectivitizer.com/guides/esp32.md and https://connectivitizer.com/guides/react-native.md. They say how to install the package from its registry (NuGet, npm, PyPI, the Connectivitizer Maven repository for Java and Kotlin, the Arduino Library Manager, PlatformIO, the ESP Component Registry) and show the minimal code. Install exactly as they say. The guides are written for the person; follow them on their behalf.

## 2. Settle these with the person before you write code

- Which side or sides live in this project, and where the other side runs. If the calling side can run nothing but a web page (a plugin that another app hosts in a WebView, a kiosk page, a browser extension), it can use the browser entry point (https://connectivitizer.com/guides/browser.md), but only as a backup: tell the person before you choose it that every session then goes through the relay and counts as relayed traffic of their plan, and ask whether the calling side could run elsewhere instead (Node.js, a phone app of their own, `cz connect`).
- The service: its name (as in the message) and the local port it listens on, and whether it is HTTP, WebSocket or another TCP protocol.
- Device side: how the device logs in. With an SDK this is a **device key** (`czd_…`; web app: *Credentials → Create a device key*); with `cz` either a device key or a one-time **enrollment token** (web app: *Devices → Enroll a device*).
- Calling side: how the application authenticates. A program the person runs themselves uses an **API key** (`czk_…`; web app: *Credentials → Create an API key*). An app for other people does not get a key typed in: it calls `linkApp`, someone approves it in the web app, and the app keeps the key it is given in its keystore.
- The **shared key** (`czs_…`; web app: *Credentials → Generate a shared key*, made in the browser). The same key goes to both sides.
- Whether people reach the device with the **Connectivitizer app** on their phones. Then there is no calling application to write: the device serves its own page, and the app shows it (https://connectivitizer.com/guides/device-screen.md has the code for each language; start from its page and serve it from the device's web server). If the page has a look of its own (a dark terminal style, a brand colour), give its `ui.json` a `theme`, so that the app's top bar matches the page. Before the device runs the page, the person can see and try it on their computer: `curl -fsSLO https://connectivitizer.com/downloads/tools/screen-preview.mjs`, then `node screen-preview.mjs ui.html --fake fake-device.js --open` with a pretend device for the page's requests (section 4 of that guide), or `--device http://<address>` for the real one.
- Whatever the language guide lists under "What you need" (for example the board and Wi-Fi network of an ESP32).

Do not ask for the secret values themselves in the chat when you can avoid it. Create the git-ignored file that will hold them, with placeholders, and let the person fill it in.

You can also create the keys yourself, without seeing them: the workspace tools (https://connectivitizer.com/guides/workspace-tools.md) write a new device key, API key or shared key straight into that file. If the tools `create_device_key`, `create_shared_key` and so on are available to you, use them; otherwise `npx -y @huelake/connectivitizer key create … --save <file>:<NAME>` does the same from a shell, after the person has approved `npx -y @huelake/connectivitizer login` in the browser once. Do not read the secrets file back afterwards.

## 3. Rules

- Use the SDK or `cz` as it is. Do not implement the protocol, the encryption, STUN or the relay yourself, and do not add a fallback that sends the data unencrypted or around Connectivitizer.
- Device keys, shared keys, API keys and enrollment tokens are secrets. Keep them in a git-ignored configuration file, an environment variable or a secret store (on a phone: the keystore), never in committed code and never in logs. Commit a template with placeholders instead.
- In a web page or WebView, never build a key into the page or plugin itself, not even from a configuration file the build bundles: everyone who gets the file has it. The person enters the shared key once, the API key comes from `linkApp`, and the page keeps both in its own storage.
- With a shared key: it must be exactly the same on both sides, otherwise sessions are refused with `E2E_SHARED_KEY_MISMATCH`.
- A relayed session is normal, not an error. Do not retry, fail or warn because the mode is `e2e-relay`.
- Show the path and the encryption only as the SDK reports them (`mode`, `endToEndEncrypted`, `directFailureReason`, `peerAddress`): never call a relayed session direct, and never call a session end-to-end encrypted that the SDK does not report as such.
- Expose only the services the person wants reachable, by name. Do not expose whole hosts or broad port ranges.
- Do not write code for local addresses, IPv6, UDP sockets or ports to make direct sessions work: the SDK does that. If sessions stay relayed, it is the network; see the troubleshooting guide.

## 4. Done when

- The device shows as online under *Devices* in the web app.
- A request from the calling application reaches the service and gets its answer.
- The session is listed under *Connections* in the web app as *Direct* or *Relayed*; a relayed one says why.

## 5. Hand over: the shared key on the phone

When the person will use the Connectivitizer app (or another phone app that scans keys), the same shared key has to get onto the phone: *Account › Keys › Add a key* in the app. Once they tell you they have filled the key into the git-ignored secrets file, **offer to show it as a QR code** to scan:

```bash
curl -fsSLO https://connectivitizer.com/downloads/tools/qr.mjs
node qr.mjs --from <secrets file> --svg <git-ignored folder>/key-qr.svg --open
```

Where the key is in `cz` (`cz key add`), `cz key qr <name> --svg <git-ignored folder>/key-qr.svg --open` does the same without the script. `qr.mjs` is one file without packages. `--from` reads the key from the file and prints only its last four characters, `--open` shows the image in the default viewer. Do not read the secrets file yourself or put the key on the command line, and do not print the code into your own output: a QR code there is the key in readable form. If the person prefers the terminal, give them `node qr.mjs --from <secrets file>` to run in a terminal window of their own, not through your tools and not through a pass-through into your session (such as a `!` command), whose output also reaches you. Delete the image after the scan. The web app's *Generate a shared key* dialog also shows the code, but only once, when the key is made. More: https://connectivitizer.com/guides/mobile-app.md

## 6. When it does not work

- Errors carry a stable code (`DEVICE_OFFLINE`, `FORBIDDEN`, `E2E_SHARED_KEY_MISMATCH`, …). What each means and what helps: https://connectivitizer.com/guides/errors.md
- A session that is relayed where it should be direct, or does not connect at all: https://connectivitizer.com/guides/troubleshooting.md. Go through it before changing any code, and tell the person which reason code you saw.
