Skip to main content

A Python utility library for interacting with Ubiquiti UniFi Controller REST APIs

Project description

UnifiUtils (Python)

A Python utility library for interacting with Ubiquiti UniFi Controller REST APIs. This library provides a clean abstraction layer for HTTP communication with UniFi network controllers, handling authentication, endpoint management, and JSON request/response processing.

Ported from the Java UnifiUtils library.

Features

  • Enumeration-based API interface - Type-safe API endpoint definitions with 129 UniFi endpoints
  • Dynamic URL templating - Automatic placeholder substitution for site, MAC address, and other parameters
  • Flexible HTTP methods - Support for GET, POST, PUT, DELETE, and HEAD operations
  • Built-in authentication - API key header management for all requests
  • Native JSON handling - Returns Python dicts directly
  • Comprehensive logging - Python logging module integration for debugging and traceability

Requirements

  • Python 3.10+
  • requests library

Installation

From Source

git clone https://github.com/RogerJoys/unifi-utils-python.git
cd unifi-utils-python
pip install -e .

Development

pip install -e ".[dev]"

Usage

Basic Initialization

from unifi_utils import UnifiUtils, UnifiAPI

# Initialize with UniFi controller details
unifi = UnifiUtils(
    endpoint="https://your-controller.url",  # Controller endpoint
    api_key="your-api-key",                   # API key
    site="default",                            # Site name
)

Making API Calls

# Simple GET request
status = unifi.make_api_call(UnifiAPI.StatusGet)

# Get all active clients
clients = unifi.make_api_call(UnifiAPI.ActiveClientsGet)

# Get site health
health = unifi.make_api_call(UnifiAPI.HealthGet)

# POST request with JSON body
body = {"username": "admin", "password": "password"}
response = unifi.make_api_call(UnifiAPI.LoginPost, json_body=body)

# Device-specific command with MAC address
device_response = unifi.make_api_call(
    UnifiAPI.DeviceLocateEnablePost,
    mac_address="aa:bb:cc:dd:ee:ff",
)

# Full control with custom endpoint substitutions
from unifi_utils import UnifiEndpointSymbolics

substitutions = {UnifiEndpointSymbolics.ID: "device-id-here"}
update_body = {"name": "New Device Name"}
update_response = unifi.make_api_call(
    UnifiAPI.DeviceUpdatePut,
    json_body=update_body,
    added_substitutions=substitutions,
)

Available API Endpoints

The library provides 129 API endpoints covering all UniFi Controller functionality:

Category Count
Controller APIs (auth, system) 9
Health & Status 5
Events & Alarms 4
Client Management 11
VPN 1
Device Management 16
Network Configuration 9
Firewall Configuration 8
Port Forwarding 4
WLAN Configuration 5
Site Settings 2
DPI & Analytics 6
RADIUS 8
Dynamic DNS 3
Switch Port Profiles 4
Rogue APs 2
Hotspot / Vouchers / Guests 5
Hotspot Operators 4
User Groups 4
Media Streaming 2
Backup Commands 3
Site Management 6
Event Management 1
SDN / Cloud Access 1
Miscellaneous (Tags, Counters, Dashboard, WLAN Groups) 7

Project Structure

unifi-utils-python/
├── src/
│   └── unifi_utils/
│       ├── __init__.py      # Public exports
│       ├── enums.py         # HTTPCall, UnifiApiClass, UnifiCommandManager, UnifiEndpointSymbolics
│       ├── api.py           # UnifiAPI enum (129 endpoint definitions)
│       └── client.py        # UnifiUtils class (main HTTP client)
├── tests/
│   ├── test_enums.py
│   ├── test_api.py
│   └── test_client.py
├── pyproject.toml
├── LICENSE
└── README.md

Core Classes

UnifiUtils

The main class for interacting with the UniFi Controller API. Provides a single make_api_call() method with optional parameters:

  • api_call - The UnifiAPI enum member to call
  • json_body - Optional dict for POST/PUT request bodies
  • mac_address - Optional MAC address for device-specific commands
  • added_substitutions - Optional dict of UnifiEndpointSymbolics to string values for URL placeholders

UnifiAPI

Enumeration defining all supported API endpoints with their paths, HTTP methods, requirements, and manager types.

UnifiApiClass

Defines API classification types with their URL prefixes:

  • Controller - Root level endpoints
  • Site - Site-specific endpoints (/api/s/{{site}})
  • CallableCommand - Device command endpoints (/api/s/{{site}}/cmd/{{manager}})

UnifiCommandManager

Defines manager types for command routing: evtmgt, sitemgr, stamgr, devmgr, backup, system, stat.

Running Tests

pip install -e ".[dev]"
pytest

API Documentation

For detailed UniFi Controller API documentation, see:

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unifi_utils_python-1.0.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unifi_utils_python-1.0.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file unifi_utils_python-1.0.0.tar.gz.

File metadata

  • Download URL: unifi_utils_python-1.0.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.10

File hashes

Hashes for unifi_utils_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e5f5711a4492ffed883a8f66e83b798629b4e1ec56dbede1609c7c019cd47d2a
MD5 17d6d4f3a8fce2e6af8b9d4bec541140
BLAKE2b-256 775cc75e7cfda9b41652700272ba6b93f414382f7d6a2ce0177f490b1d85a065

See more details on using hashes here.

File details

Details for the file unifi_utils_python-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for unifi_utils_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b740aef6f8a902e6dadfb648e52685a86920aafe12204ed092fdd506d7f932c
MD5 f354a7d2c993a7144fc902b95f18d948
BLAKE2b-256 d1d459ff66dac1011a47f4137878ba1c046b8ed626da851f77517894e729905b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page