Use unifi-cli from AI agents
About 1357 words
unifi-cli is built to be driven by a coding agent as much as by a person. Every command has a machine-readable output, nothing needs a terminal once you're signed in, and the commands that change the network refuse to run unattended unless told to.
This guide covers the agent plugin, and then the contract an agent, or any script, can rely on.
Install the plugin
The repository ships a plugin for Claude Code and for Codex. It bundles three skills that load on demand:
| Skill | Teaches |
|---|---|
unifi | the grammar, discovering contexts and sites, reading safely, filters, changing things with confirmation, port management |
unifi-config-as-code | the export, diff and apply workflow with manifests |
unifi-troubleshooting | diagnosing connection and permission errors, and network problems such as weak signal or offline devices |
For Claude Code:
/plugin marketplace add SpechtLabs/unifi-cli
/plugin install unifi@unifi-cliFor Codex, from a checkout of the repository:
codex plugin marketplace add .agents/plugins
codex plugin install unifiThe plugin needs the unifi binary on PATH and at least one context that is signed in. Agents can't complete the interactive unifi login, so sign in yourself first, or give the agent's environment a configuration file and UNIFI_API_KEY.
Give the agent the right key
Decide what the agent may do before it starts, and enforce that with the key.
An API key inherits the role of the administrator who created it. For an agent that should diagnose and report, create the key as a view-only administrator: every read works, and every change fails with a clear message, whatever the agent tries. Hand out a full-access key only for sessions where you want changes made, and give that agent its own administrator so the audit log (unifi events get --audit) shows what it did.
A separate context for the agent keeps this tidy:
UNIFI_API_KEY="$READONLY_KEY" unifi login --local --address https://192.168.1.1 \
--ca-file udm.pem --name agent-ro --no-switchThe contract
Output
Pass --output json (or -o json) on every command an agent parses.
- JSON and YAML go to standard output with no decoration: no colors, no hints, no progress text.
- A list prints an array, and a single object prints an object.
unifi clients getis an array;unifi clients get nasis one object. - Configuration subjects print manifests, the same shape
unifi applyreads. - Commands that change something print an action result, one shape for all of them, so an agent can confirm what happened without re-reading. Verbs that take several targets print an array with one result per target; a failing target is reported as
status: "failed"in its place, the others still run, and the exit code is 1. - Errors go to standard error as text, with advice lines. They are meant to be read, by a person or a model, and aren't JSON.
An action result looks like this:
{
"status": "ok",
"action": "block",
"target": {
"kind": "client",
"name": "Guest-iPhone",
"id": "1ee982dc-c729-4dfe-aa49-f53523f24018",
"mac": "02:9e:18:89:f4:6a"
},
"changes": [
"Blocked Guest-iPhone."
],
"message": "Blocked Guest-iPhone."
}status is ok when something changed, unchanged when the target already was in the wanted state, and failed otherwise; dryRun: true marks a preview. unchanged comes with exit code 0, so repeating a command is safe.
The default text format is for people. Its columns are truncated to the terminal width and its layout may change between releases, so don't parse it.
Exit codes
| Code | Meaning |
|---|---|
| 0 | Success. For unifi diff: no differences. |
| 1 | The command failed. |
| 2 | unifi diff found differences. |
| 130 | The command was interrupted. |
Nothing interactive, unless asked
Without a terminal on standard input, unifi-cli never prompts. A command that would ask a question fails instead and says which flag answers it:
$ unifi devices restart "AP Office" < /dev/null
✗ This action needs confirmation, and no terminal is attached to ask.
Advice:
• Pass `--yes` to confirm.That makes --yes the explicit switch for "a human approved this". Every verb that changes something accepts --yes, so an agent doesn't have to know which ones ask. The ones that do ask are those that take something live offline or destroy something: delete, forget, restart and upgrade of a device that is online, block of a connected client, ports disable of a port with link or power, cutting PoE on a port that powers something, disable of an object in use, and apply --prune when it would delete. A no-op never asks.
Three commands are interactive by nature and aren't for agents: unifi dashboard and unifi topology --watch (full-screen views), and unifi shell without --command. Their one-shot forms are fine: unifi dashboard --output json, unifi topology --output json, unifi shell -c "...". In shell scripts, staged changes need an explicit commit line; a script that leaves a resource mode without one fails.
Looking before changing
Every way of changing configuration has a preview that touches nothing:
| Change | Preview |
|---|---|
unifi <subject> set ... | add --dry-run |
unifi settings <shortcut> set ... | add --dry-run |
unifi apply -f ... | unifi diff -f ..., or apply --dry-run |
Port and device actions (ports poe, ports set, devices restart) have no dry run; their preview is reading the current state with unifi ports get <device> <port> --output json.
Note that unifi apply creates and updates without asking. Only deletions (--prune) need --yes. An agent should therefore always run unifi diff first and show the result to the person it works for.
Being explicit about the target
--context and --site work on every command. An agent should pass both on anything that changes the network, so the target is in the command line it shows you and never depends on which context happens to be current.
unifi --context home --site Default ports poe "Core Switch" "Camera Driveway" cycle --yesDiscovering what exists
Agents shouldn't guess names. These commands list what there is, cheaply:
unifi ctx get --output json # contexts, with the current one marked
unifi sites get --output json # sites of the controller
unifi resources get --output json # configuration kinds, their commands and API family
unifi dashboard --list-widgets --output json
unifi <subject> --help # verbs of a subjectField names for --filter are the JSON field names of the objects, and a misspelled field fails with the list of valid ones. The same goes for writing: --set and manifests refuse a field that no object of the kind on the site has, with a suggestion, unless --force is passed.
Two flags mean different things on different commands, which trips up agents that generalize: -f is --filename on apply, diff and create, and --follow on events get. --filter has no short form anywhere.
Rate limits and politeness
An agent can issue commands faster than a person, and each command is at least two API requests. Controllers rate-limit, the cloud connector more so. unifi-cli retries read requests that hit a limit, up to three attempts with backoff; writes are never retried.
One unifi clients get --output json followed by local filtering with jq beats twenty filtered calls.
unifi-cli helps a little on its own: it caches a controller's site list and version for ten minutes, so a burst of commands doesn't repeat those requests. An agent that has just changed something outside unifi-cli and needs the present state passes --no-cache, or sets UNIFI_NO_CACHE=1 for the session.
Without the plugin
The plugin is a convenience. Any agent that can run shell commands can use unifi-cli with a short instruction along these lines:
Use the `unifi` CLI for anything about the UniFi network. Grammar: `unifi <subject> <verb>`.
Always pass `--output json`. List contexts and sites first; never guess names.
Read before you change. Show me the `--dry-run` or `unifi diff` output and wait for my go-ahead
before running anything with `--yes`.The full skill text is in the repository under plugins/unifi/skills.
