A Python client/wrapper for the UniFi Access API
Project description
UniFi Access Python Client
A modern, thin API wrapper for the UniFi Access API. This library provides a clean and intuitive interface for managing UniFi Access devices, users, visitors, access policies, and more.
Features
- Thin API Wrapper: Returns simple dictionaries for flexibility and performance
- Comprehensive API Coverage: Full support for all UniFi Access API endpoints
- Async Support: Built-in async client for high-performance applications
- Easy Configuration: Environment variable support with
.envfiles - Webhook Support: Built-in webhook listener and manager
Installation
pip install unifi_access
Quick Start
Configuration
Create a .env file in your project root:
UNIFI_ACCESS_BASE_URL=https://192.168.1.1
UNIFI_ACCESS_API_TOKEN=your-api-token-here
UNIFI_ACCESS_PORT=12445
UNIFI_SESSION_TIMEOUT=30
UNIFI_SESSION_TIMEOUT is optional and defaults to 15 seconds. Increasing timeout time is sometimes required on busy systems with large amounts of users, doors, etc. and the Unifi console/NVR takes a little longer to process requests.
Basic Setup
from unifi_access.client import UniFiAccessClient
# Initialize with environment variables
client = UniFiAccessClient()
# Or initialize with explicit credentials
client = UniFiAccessClient(
base_url="https://192.168.1.1",
api_token="your-api-token",
port="12445",
verify_ssl=False
)
Usage Examples
Most methods/functions are named identical to Unifi Access API documentation section headings. IE: From the unifi docs "7.5 Fetch All Door Groups" the method will be client.spaces.fetch_all_door_groups()
User Management
# Fetch all users - returns a list of dictionaries
users = client.users.fetch_all_users()
if users:
user = users[0]
print(f"User: {user.get('first_name')} {user.get('last_name')}")
# Update user
client.users.update_user(user['id'], first_name="NewName")
# Assign an NFC card
client.users.assign_nfc_card_to_user(user['id'], "card_token_123")
# Get access policies
policies = client.users.fetch_access_policies_assigned_to_user(user['id'])
Visitor Management
# Create a visitor - returns a dictionary
visitor = client.visitors.create_visitor(
first_name="John",
last_name="Doe",
start_time=1688546460, # Unix timestamp in timezone of the location
end_time=1688572799,
email="john.doe@example.com",
resources=[
{
"id": "door_id_123",
"type": "door"
}
],
week_schedule={
"sunday": [],
"monday": [],
"tuesday": [
# Single time slot
{
"start_time": "06:00:00", # 6 am
"end_time": "18:00:00" # 6 pm
}
],
"wednesday": [],
"thursday": [
# Multiple time slots in single day.
{
"start_time": "06:00:00",
"end_time": "09:00:00", # 9 am
},
{
"start_time": "18:00:00", # 6 pm
"end_time": "23:59:59" # Midnight
}
],
"friday": [],
"saturday": []
}
)
# Manage visitor
client.visitors.assign_nfc_card_to_visitor(visitor['id'], "card_token_456")
client.visitors.assign_pin_code_to_visitor(visitor['id'], "1234")
client.visitors.update_visitor(visitor['id'], remarks="VIP visitor")
client.visitors.delete_visitor(visitor['id'])
Flexible Schedules
Both Visitor and Access Policy managers support flexible weekly schedules. You only need to provide the days you want to set; other days will default to empty. You can also use the everyday shortcut.
# Partial schedule - only Monday is set, others default to empty
client.visitors.create_visitor(
...,
week_schedule={
"monday": [{"start_time": "09:00:00", "end_time": "17:00:00"}]
}
)
# Everyday shortcut - applies to all 7 days
client.access_policies.create_schedule(
name="Everyday Access",
week_schedule={
"everyday": [{"start_time": "08:00:00", "end_time": "20:00:00"}]
}
)
# Everyday with override - Sunday will be empty, others will have the everyday schedule
client.visitors.create_visitor(
...,
week_schedule={
"everyday": [{"start_time": "08:00:00", "end_time": "20:00:00"}],
"sunday": []
}
)
Door Management
# Fetch all doors - returns a list of dictionaries
doors = client.spaces.fetch_all_doors()
if doors:
door = doors[0]
print(f"Door: {door.get('name')}")
# Control doors
client.spaces.unlock_door(door['id'])
client.spaces.set_temporary_door_locking_rule(door['id'], "keep_unlock", 60) # Keep unlocked for 1 hour
Access Policy Management
# List all access policies
policies = client.access_policies.fetch_all_access_policies()
if policies:
policy = policies[0]
print(f"Policy: {policy.get('name')}")
# Manage policies
client.access_policies.update_access_policy(policy['id'], name="New Policy Name")
client.access_policies.delete_access_policy(policy['id'])
Credential Management
# Import 26-bit Wiegand cards
wiegand_cards = [
{"facility_code": 100, "card_number": 1234},
{"facility_code": 100, "card_number": 1235},
]
client.credentials.import_26bit_wiegand_cards(wiegand_cards)
Async Support
from unifi_access.client import AsyncUniFiAccessClient
# Use async client for high-performance applications
async with AsyncUniFiAccessClient() as client:
users = await client.users.fetch_all_users()
for user in users:
print(f"{user.get('first_name')} {user.get('last_name')}")
API Managers
The client provides the following manager interfaces. Click each to see available functions:
client.users (User and user group management)
create_userupdate_userfetch_userfetch_all_usersdelete_usersearch_usersassign_access_policy_to_userassign_nfc_card_to_userunassign_nfc_card_from_userassign_pin_code_to_userunassign_pin_code_from_usercreate_user_groupfetch_all_user_groupsfetch_user_groupupdate_user_groupdelete_user_groupassign_users_to_user_groupunassign_users_from_user_groupfetch_users_in_a_user_groupfetch_all_users_in_a_user_groupfetch_access_policies_assigned_to_userassign_access_policy_to_user_groupfetch_access_policies_assigned_to_user_groupassign_touch_pass_to_userunassign_touch_pass_from_userbatch_assign_touch_passes_to_usersassign_license_plate_numbers_to_userunassign_license_plate_number_from_userupload_user_profile_picture
client.visitors (Visitor management)
create_visitorupdate_visitorfetch_visitorfetch_all_visitorsdelete_visitorassign_nfc_card_to_visitorunassign_nfc_card_from_visitorassign_pin_code_to_visitorunassign_pin_code_from_visitorassign_qr_code_to_visitorunassign_qr_code_from_visitorassign_license_plate_numbers_to_visitorunassign_license_plate_numbers_from_visitor
client.access_policies (Access policy management)
create_access_policyupdate_access_policydelete_access_policyfetch_access_policyfetch_all_access_policiescreate_holiday_groupupdate_holiday_groupdelete_holiday_groupfetch_holiday_groupfetch_all_holiday_groupscreate_scheduleupdate_scheduledelete_schedulefetch_schedulefetch_all_schedules
client.credentials (Credential management)
generate_pin_codeenroll_nfc_cardfetch_nfc_card_enrollment_statusremove_session_created_for_nfc_card_enrollmentfetch_nfc_cardfetch_all_nfc_cards(alias:list_nfc_cards)update_nfc_carddelete_nfc_cardfetch_the_touch_pass_listsearch_touch_passfetch_all_assignable_touch_passesupdate_touch_passfetch_touch_pass_detailspurchase_touch_passesdownload_qr_code_imageimport_third_party_nfc_cardsimport_third_party_nfc_cards_as_listimport_26bit_wiegand_cards
client.spaces (Door and door group management)
fetch_door_group_topologycreate_door_groupfetch_door_groupupdate_door_groupfetch_all_door_groupsdelete_door_groupfetch_doorfetch_all_doorsunlock_doorset_temporary_door_locking_rulefetch_door_lock_ruleset_door_emergency_statusfetch_door_emergency_status
client.devices (Device management)
fetch_devicesfetch_access_devices_access_method_settingsupdate_access_devices_access_method_settingstrigger_doorbells
client.system_logs (System log retrieval)
fetch_system_logsexport_system_logsfetch_resources_in_system_logsfetch_static_resources_in_system_logs
client.https_certificates (HTTPS certificate management)
upload_https_certificatedelete_https_certificate
client.notifications (Notification management)
fetch_webhook_endpoints_listadd_webhook_endpointupdate_webhook_endpointdelete_webhook_endpoint
client.identity (Identity management)
send_invitationsfetch_available_resourcesassign_resources_to_usersfetch_user_resourcesassign_resources_to_user_groupsfetch_user_group_resources
Requirements
- Python 3.8+
- httpx >= 0.23.0
License
MIT License - see LICENSE file for details
Author
Travis Tucker
Links
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unifi_access-0.1.6.tar.gz.
File metadata
- Download URL: unifi_access-0.1.6.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19334bfbfc5bd8236f574905f0b9c14a16115b615cd3047dc8740ec9c51c8962
|
|
| MD5 |
8aec87c875c385fda5fbc0e95c5df458
|
|
| BLAKE2b-256 |
838efdc8fb69a1ab84f48ace10c25109084a4c1c8af561b4ae01c6e2815df78a
|
File details
Details for the file unifi_access-0.1.6-py3-none-any.whl.
File metadata
- Download URL: unifi_access-0.1.6-py3-none-any.whl
- Upload date:
- Size: 35.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd4efb43b9711e80aa7796a65af7fcd2c4641493200ce65b2e936e3f6cb7a03c
|
|
| MD5 |
138e66a1e6f7c41d04e794fa08368568
|
|
| BLAKE2b-256 |
9085e7d62b7041b741fd5feb0c8df800e072b804fb827d5b4653812c64e8fa84
|