Unofficial Python API and CLI for UniFi Protect, for any UniFi OS console that runs Protect. It is the library behind the Home Assistant UniFi Protect integration and can be used on its own. uiprotect is not affiliated with or endorsed by Ubiquiti.
The library is moving to Ubiquiti's documented Public Integration API, which authenticates with an API key. Features the public API does not offer use the undocumented private API, which can change between Protect releases.
Reporting issues and requesting features
Check the public API first
The Public Integration API documentation lists every device field and control the library can support. If a field or control is not there, uiprotect cannot add it as a new feature.
A field is not enough when only some models have the feature: there also has to be a feature flag (under featureFlags in the output) or another field that tells which devices support it. Otherwise Home Assistant would offer controls that do nothing on the other models.
To see what your console returns, query it with curl and an API key. Replace HOST with the IP address or hostname of your console and API_KEY with your key. The command saves the answer to cameras.json in the current folder.
For other devices, replace cameras at the end of the URL with another endpoint from the documentation, such as lights, sensors or cameras/CAMERA_ID for a single camera, using its id from cameras.json. Doorbells are cameras. -k skips certificate verification. It is needed while the console uses its default self-signed certificate.
If the file only contains an error, check the key and HOST. Otherwise open it in a text editor and remove MAC addresses, IP addresses and serial numbers before you post it. Never post the command itself, it contains your API key.
Home Assistant diagnostics are no substitute. Their public_bootstrap section only contains fields uiprotect already reads, so a field the library does not know yet is missing there. The bootstrap section is the private API.
[!IMPORTANT]
This library does not accept new features built on the private API.
uiprotect is migrating from the undocumented private API to Ubiquiti's official
Public Integration API. If a capability is missing from the public API, the right
path is to request it from Ubiquiti / wait for it to be exposed there — not to
add it on the private path. Issues or PRs that introduce new private-API
functionality will be closed.
Requirements
UniFi Protect 7.2+, tested against the latest stable release
PyAV, installed as a dependency, for talkback audio
Windows is not actively supported and not tested in CI, although it should work. Issues about Windows are welcome as a hint, but there is no promise of a fix. The Docker container and WSL are the supported options there.
Installation
From PyPI
pip install uiprotect
To use the command-line interface, install the cli extra:
The Docker container needs no local Python. Add this to your .bashrc or similar. It passes the environment variables from your shell into the container:
Update with docker pull ghcr.io/uilibs/uiprotect:latest.
$PWD is mounted to /data. Files written anywhere else are lost when the container exits.
Images exist for linux/amd64 and linux/arm64, so Docker Desktop on macOS and Windows works.
TZ sets the time zone the CLI uses for local times. Use the time zone name your Protect console uses. The command line docs list the other environment variables.
Authentication
API key
Create an API key on the console under Settings > Control Plane > Integrations with an administrator account. Ubiquiti's Getting Started guide describes it under "Obtaining an API Key". There is no read-only key, so treat it like a password.
Local user
Features the public API does not offer also need a local user with access to Protect.
[!WARNING]
UI Account (cloud) logins and MFA are not supported. Use a local-access user, not the owner account.
Quickstart
CLI
export UFP_ADDRESS=YOUR_CONSOLE_IP
export UFP_API_KEY=YOUR_API_KEY
# set to true if the console has a certificate your machine trusts
export UFP_SSL_VERIFY=false
uiprotect cameras list-ids
In Windows PowerShell, set the variables with $env:UFP_API_KEY = "YOUR_API_KEY" and so on.
With only an API key, the CLI runs in public-only mode, and commands without a public equivalent, such as uiprotect nvr, stop with an error. With a local user set as well, the CLI logs in and the device commands read the private API instead:
In this mode the private entry points (update(), authenticate(), get_bootstrap()) raise PublicOnlyModeError. A rejected key raises NotAuthorized on HTTP calls and turns the websockets to WebsocketState.AUTH_FAILED.
For features that still need the private API, pass the local user as well and call both update() and update_public():
subscribe_events delivers typed (ProtectEvent, EventChange) pairs for detection, sensor, alarm hub and access events. subscribe_devices delivers a ProtectDeviceChange for each added, updated or removed device. Devices that already exist are not announced, read them from public_bootstrap after priming. Both need a prior update_public(). subscribe_events_and_prime() and subscribe_devices_and_prime() do both in the right order.
Callbacks are plain functions called on the event loop. They must not block and must not be async def. Exceptions in a callback are logged and swallowed.
The usage docs describe events, device changes, reconnects and the other public API features in detail.
Contributing
Please open an issue and agree on the approach before implementing anything.
A maintainer decides on the issue and usually has a bot implement it, then
reviews the result. The bot works from the architecture notes in
AGENTS.md and the
test corpus. A pull request written without that context, by hand or with AI,
usually costs more review than it saves, and every round waits on its author.
Pull requests without an agreed approach are closed.
What helps most is what we cannot supply ourselves: tests on devices we do not
own, sanitized payloads from them, and first-hand knowledge of how they behave.
Developer Setup
The recommended way to develop is using the provided devcontainer with VS Code:
When prompted, click "Reopen in Container" (or use Command Palette: "Dev Containers: Reopen in Container")
The devcontainer will automatically set up Python, Poetry, pre-commit hooks, and all dependencies
Alternatively, if you want to develop natively without devcontainer:
# Install dependencies (--all-extras installs the cli extra for CLI tests)
poetry install --with dev --all-extras
# Install pre-commit hooks
poetry run pre-commit install --install-hooks
# Run tests
poetry run pytest
# Run pre-commit checks manually
poetry run pre-commit run --all-files
History
This project was split off from pyunifiprotect because that project changed its license to one that would not be accepted in Home Assistant. This project is committed to keeping the MIT license.
Credits
Bjarne Riis (@briis) for the original pyunifiprotect package
Christopher Bailey (@AngellusMortis) for maintaining the pyunifiprotect package