2017-10-18 10:09:55 +02:00
## UniFi Controller API client class
2017-09-05 11:48:35 +02:00
2024-01-05 11:08:06 +01:00
A PHP class that provides access to Ubiquiti's [**UniFi Network Application** ](https://unifi-network.ui.com/ ) API.
2021-10-19 10:39:17 +02:00
2024-02-09 18:11:43 +01:00
This class is used by our API Browser tool, which can be found
2022-03-28 11:57:57 +02:00
[here ](https://github.com/Art-of-WiFi/UniFi-API-browser ).
2017-09-05 11:48:35 +02:00
2021-09-29 10:41:39 +02:00
The package can be installed manually or by using
2021-10-19 10:39:17 +02:00
composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for
2024-11-04 13:38:18 +01:00
easy inclusion in your projects. See the [installation instructions ](#Installation ) below for more details.
2021-01-23 11:44:12 +01:00
2024-11-04 13:32:34 +01:00
## Supported Versions
| Software | Versions |
|--------------------------------------|-----------------------------------------------------|
| UniFi Network Application/controller | 5.X.X, 6.X.X, 7.X.X, 8.X.X (**8.5.6 is confirmed**) |
| UniFi OS | 3.X, 4.X (**4.1.15 is confirmed**) |
2023-12-21 11:09:36 +01:00
2018-12-14 11:27:20 +01:00
## Requirements
2021-10-19 10:39:17 +02:00
- a server with:
2024-10-07 12:54:19 +02:00
- PHP **7.4.0** or higher (use version [1.1.83 ](https://github.com/Art-of-WiFi/UniFi-API-client/releases/tag/v1.1.83 )
for PHP 7.3.x and lower)
2021-10-19 10:39:17 +02:00
- PHP json and PHP cURL modules
2024-10-07 12:54:19 +02:00
- tested on Apache 2.4 with PHP 7.4.27 and cURL 7.60.0 and with PHP 8.3.11 and cURL 7.81.0
2022-03-28 11:57:57 +02:00
- direct network connectivity between this server and the host and port (usually TCP port 8443 or port 443 for
2021-10-19 10:39:17 +02:00
UniFi OS) where the UniFi Controller is running
2024-10-07 12:54:19 +02:00
- you **must** use an **account with local access permissions** to access the UniFi Controller API through this class
2024-01-05 11:02:38 +01:00
- do not use UniFi Cloud accounts and do not enable 2FA for the accounts that you use with this class
2020-06-07 14:11:52 +02:00
2023-12-21 11:09:36 +01:00
2021-01-06 17:03:21 +01:00
## UniFi OS Support
2020-01-31 12:14:52 +01:00
2023-12-21 11:09:36 +01:00
Support for UniFi OS-based controllers has been added as of version 1.1.47. These devices have been verified to work:
2022-03-28 11:57:57 +02:00
- UniFi Dream Router (UDR)
- UniFi Dream Machine (UDM)
- UniFi Dream Machine Pro (UDM PRO)
2023-03-28 12:19:29 +02:00
- UniFi Cloud Key Gen2 (UCK G2), firmware version 2.0.24 or higher
- UniFi Cloud Key Gen2 Plus (UCK G2 Plus), firmware version 2.0.24 or higher
- UniFi Cloud Console, details [here ](https://help.ui.com/hc/en-us/articles/4415364143511 )
2023-12-21 11:09:36 +01:00
- UniFi Express (UX)
2024-10-07 12:54:19 +02:00
- UniFi Dream Wall (UDW)
- UniFi Cloud Gateway Ultra (UCG-Ultra)
- UniFi Enterprise Fortress Gateway (EFG)
2022-03-28 11:57:57 +02:00
2023-12-18 12:32:03 +01:00
The class automatically detects UniFi OS consoles and adjusts the URLs and several functions/methods accordingly.
2024-10-07 12:54:19 +02:00
UniFi OS-based controllers require you to connect using port **443** instead of **8443** which is used for
"software-based" controllers. If your own code implements strict validation of the URL that is passed to the
constructor, please adapt your logic to allow URLs without a port suffix or with port 443 when working with a
UniFi OS-based controller.
2020-01-31 12:14:52 +01:00
2024-01-05 11:02:38 +01:00
### Remote API access to UniFi OS-based controllers
2024-10-07 12:54:19 +02:00
When connecting to a UniFi OS-based gateway through the WAN interface, you need to create a specific firewall rule to
2023-12-21 11:09:36 +01:00
allow this. See this blog post on the Art of WiFi website for more details:
https://artofwifi.net/blog/how-to-access-the-unifi-controller-by-wan-ip-or-hostname-on-a-udm-pro
2021-01-23 11:44:12 +01:00
2024-01-05 11:02:38 +01:00
The "custom firewall rule" approach described there is the recommended method.
2024-02-09 18:11:43 +01:00
## Upgrading from a previous version
When upgrading from a version before **1.1.84** , please:
- make sure you are using PHP **7.4** or higher
- test the client with your code for any breaking changes; the class methods now have strict parameter type hints and
response types which can break your code in certain cases where the wrong type is passed or a different response type
is expected back
2020-01-31 12:14:52 +01:00
## Installation
2018-12-14 11:27:20 +01:00
2021-10-19 10:39:17 +02:00
Use [Composer ](#composer ), [Git ](#git ) or simply [Download the Release ](#download-the-release ) to install the
API client class.
2021-01-23 11:44:12 +01:00
2023-12-21 11:09:36 +01:00
2018-12-14 11:27:20 +01:00
### Composer
2021-10-19 10:39:17 +02:00
The preferred installation method is through [composer ](https://getcomposer.org ).
Follow these [installation instructions ](https://getcomposer.org/doc/00-intro.md ) if you don't have composer
installed already.
2018-12-14 11:27:20 +01:00
2021-10-19 10:39:17 +02:00
Once composer is installed, simply execute this command from the shell in your project
directory:
2018-12-14 11:27:20 +01:00
```sh
composer require art-of-wifi/unifi-api-client
```
2021-09-29 10:41:39 +02:00
Or manually add the package to your composer.json file:
2018-12-14 11:27:20 +01:00
```javascript
{
"require": {
"art-of-wifi/unifi-api-client": "^1.1"
}
}
```
2023-12-21 10:41:38 +01:00
Finally, be sure to include the composer autoloader in your code if your framework doesn't already do this for you:
2018-12-14 11:27:20 +01:00
```php
2023-12-21 10:41:38 +01:00
/**
* load the class using the composer autoloader
*/
2021-01-21 10:59:27 +01:00
require_once 'vendor/autoload.php';
2018-12-14 11:27:20 +01:00
```
2024-01-05 11:02:38 +01:00
2018-12-14 11:27:20 +01:00
### Git
Execute the following `git` command from the shell in your project directory:
```sh
git clone https://github.com/Art-of-WiFi/UniFi-API-client.git
```
When git is done cloning, include the file containing the class like so in your code:
```php
2023-12-21 10:36:10 +01:00
/**
* load the class directly instead of using the composer autoloader
*/
2021-01-21 10:59:27 +01:00
require_once 'path/to/src/Client.php';
2018-12-14 11:27:20 +01:00
```
2024-01-05 11:02:38 +01:00
2018-12-14 11:27:20 +01:00
### Download the Release
2021-09-29 10:41:39 +02:00
If you prefer not to use composer or git,
2021-11-08 10:30:22 +01:00
simply [download the package ](https://github.com/Art-of-WiFi/UniFi-API-client/archive/master.zip ), unpack the zip
2021-09-29 10:41:39 +02:00
file, then include the file containing the class in your code like so:
2018-12-14 11:27:20 +01:00
```php
2023-12-21 10:36:10 +01:00
/**
* load the class directly instead of using the composer autoloader
*/
2021-01-21 10:59:27 +01:00
require_once 'path/to/src/Client.php';
2018-12-14 11:27:20 +01:00
```
2024-01-05 11:02:38 +01:00
2018-12-14 11:27:20 +01:00
## Example usage
A basic example how to use the class:
```php
/**
* load the class using the composer autoloader
*/
2021-01-21 10:59:27 +01:00
require_once 'vendor/autoload.php';
2018-12-14 11:27:20 +01:00
/**
2021-09-29 10:41:39 +02:00
* initialize the UniFi API connection class, log in to the controller and request the alarms collection
2018-12-14 11:27:20 +01:00
* (this example assumes you have already assigned the correct values to the variables used)
*/
2021-10-22 17:35:05 +02:00
$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version, true);
2018-12-14 11:27:20 +01:00
$login = $unifi_connection->login();
$results = $unifi_connection->list_alarms(); // returns a PHP array containing alarm objects
```
2021-09-29 10:41:39 +02:00
Please refer to the `examples/` directory for some more detailed examples which can be used as a starting point for your
own PHP code.
2021-01-23 11:44:12 +01:00
2023-12-21 11:09:36 +01:00
2018-12-14 11:27:20 +01:00
#### IMPORTANT NOTES:
2021-09-29 10:41:39 +02:00
1. In the above example, `$site_id` is the short site "name" (usually 8 characters long) that is visible in the URL when
managing the site in the UniFi Network Controller. For example with this URL:
2018-12-14 11:27:20 +01:00
`https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard`
2019-01-21 09:41:47 +01:00
`jl3z2shm` is the short site "name" and the value to assign to $site_id.
2018-12-14 11:27:20 +01:00
2021-10-19 10:39:17 +02:00
2. The 6th optional parameter that is passed to the constructor in the above example (`true`), enables validation of
2021-09-29 10:41:39 +02:00
the controller's SSL certificate which is otherwise **disabled** by default. It is **highly recommended** to enable
this feature in production environments where you have a valid SSL cert installed on the UniFi Controller that is
associated with the FQDN in the `controller_url` parameter. This option was added with API client version 1.1.16.
2021-01-23 11:44:12 +01:00
2021-11-08 10:30:22 +01:00
3. Using an administrator account (`$controller_user` in the above example) with **read-only** permissions can limit
2022-03-28 11:57:57 +02:00
visibility on certain collection/object properties. See this
[issue ](https://github.com/Art-of-WiFi/UniFi-API-client/issues/129 ) and this
[issue ](https://github.com/Art-of-WiFi/UniFi-API-browser/issues/94 ) for an example where the WPA2 password isn't
visible for **read-only** administrator accounts.
2021-11-08 10:30:22 +01:00
2023-12-21 11:09:36 +01:00
2019-04-29 17:35:09 +02:00
## Functions/methods supported
2017-09-05 11:48:35 +02:00
2023-12-21 11:09:36 +01:00
The class currently supports the following functions/methods to access the UniFi Controller API. This list is sorted
alphabetically. Please refer to the comments in the source code for more details on each of the functions/methods,
their purpose, and their respective parameters.
2018-02-01 16:56:17 +01:00
2017-09-05 11:48:35 +02:00
- adopt_device()
2023-12-21 11:09:36 +01:00
- advanced_adopt_device()
2017-10-19 11:24:24 +02:00
- archive_alarm()
2020-06-20 16:12:13 +02:00
- assign_existing_admin()
2017-09-05 11:48:35 +02:00
- authorize_guest()
- block_sta()
2020-06-20 16:12:13 +02:00
- cancel_rolling_upgrade()
2020-10-26 09:51:41 +01:00
- check_controller_update()
- check_firmware_update()
2021-10-19 10:39:17 +02:00
- cmd_stat()
- count_alarms()
2023-12-21 11:09:36 +01:00
- create_apgroup()
2020-06-20 16:12:13 +02:00
- create_dynamicdns()
2018-08-19 12:00:19 +02:00
- create_firewallgroup()
2017-09-05 11:48:35 +02:00
- create_hotspotop()
2017-09-08 12:36:12 +02:00
- create_network()
- create_radius_account()
2018-07-02 15:06:31 +02:00
- create_user()
2020-06-20 16:12:13 +02:00
- create_usergroup()
2017-09-05 11:48:35 +02:00
- create_voucher()
2017-09-08 12:36:12 +02:00
- create_wlan()
2019-04-29 17:35:09 +02:00
- custom_api_request()
2023-12-21 11:09:36 +01:00
- delete_apgroup()
2018-02-01 16:56:17 +01:00
- delete_device()
2018-08-19 12:00:19 +02:00
- delete_firewallgroup()
2017-09-08 12:36:12 +02:00
- delete_network()
- delete_radius_account()
2017-09-05 11:48:35 +02:00
- delete_site()
2017-09-08 12:36:12 +02:00
- delete_usergroup()
- delete_wlan()
2017-09-05 11:48:35 +02:00
- disable_ap()
2023-12-21 11:09:36 +01:00
- disable_wlan()
- edit_apgroup()
2018-10-20 14:29:08 +02:00
- edit_client_fixedip()
2021-10-23 10:34:39 +02:00
- edit_client_name()
2018-08-19 12:00:19 +02:00
- edit_firewallgroup()
2017-09-08 12:36:12 +02:00
- edit_usergroup()
- extend_guest_validity()
2023-12-21 11:09:36 +01:00
- forget_sta()
2023-03-28 12:19:29 +02:00
- generate_backup()
- generate_backup_site()
2023-12-21 11:09:36 +01:00
- get_class_version()
- get_cookie()
- get_cookies()
- get_curl_connection_timeout()
- get_curl_http_version()
- get_curl_method()
- get_curl_request_timeout()
- get_curl_request_timeout()
- get_curl_ssl_verify_host()
- get_curl_ssl_verify_peer()
- get_debug()
- get_is_unifi_os()
- get_last_error_message()
- get_last_results_raw()
- get_site()
2018-03-30 13:57:59 +02:00
- invite_admin()
2017-09-05 11:48:35 +02:00
- led_override()
- list_admins()
2020-06-20 16:12:13 +02:00
- list_all_admins()
2023-12-21 11:09:36 +01:00
- list_alarms()
- list_aps()
2018-10-20 14:29:08 +02:00
- list_backups()
2017-09-05 11:48:35 +02:00
- list_clients()
2018-03-24 10:46:42 +01:00
- list_country_codes()
2017-09-08 12:36:12 +02:00
- list_current_channels()
2017-09-05 11:48:35 +02:00
- list_dashboard()
2021-01-23 11:44:12 +01:00
- list_device_name_mappings()
2023-12-21 11:09:36 +01:00
- list_device_states()
2021-10-19 10:39:17 +02:00
- list_devices()
2023-12-21 11:09:36 +01:00
- list_devices_basic()
2017-09-05 11:48:35 +02:00
- list_dynamicdns()
- list_events()
- list_extension()
2018-08-19 12:00:19 +02:00
- list_firewallgroups()
2020-06-08 12:52:32 +02:00
- list_firmware()
2017-09-05 11:48:35 +02:00
- list_guests()
- list_health()
- list_hotspotop()
2017-10-18 08:25:48 +02:00
- list_known_rogueaps()
2017-09-05 11:48:35 +02:00
- list_networkconf()
- list_portconf()
- list_portforward_stats()
- list_portforwarding()
2023-12-21 11:09:36 +01:00
- list_radius_accounts()
2017-09-08 12:36:12 +02:00
- list_radius_profiles()
2017-09-05 11:48:35 +02:00
- list_self()
- list_settings()
- list_sites()
2023-12-21 11:09:36 +01:00
- list_tags()
2017-09-05 11:48:35 +02:00
- list_users()
- list_wlan_groups()
- list_wlanconf()
2017-09-08 12:36:12 +02:00
- locate_ap()
2023-12-21 11:09:36 +01:00
- login()
- logout()
2018-02-01 16:56:17 +01:00
- move_device()
2017-10-18 08:25:48 +02:00
- power_cycle_switch_port()
2020-06-20 16:12:13 +02:00
- reboot_cloudkey()
2017-09-05 11:48:35 +02:00
- rename_ap()
2020-06-20 16:12:13 +02:00
- revoke_admin()
2017-09-05 11:48:35 +02:00
- revoke_voucher()
- set_ap_radiosettings()
2023-12-21 11:09:36 +01:00
- set_ap_wlangroup()
- set_connection_timeout()
- set_cookies()
- set_curl_http_version()
- set_curl_request_timeout()
- set_curl_ssl_verify_host()
- set_curl_ssl_verify_peer()
- set_debug()
2017-10-18 08:25:48 +02:00
- set_device_settings_base()
2020-06-20 16:12:13 +02:00
- set_dynamicdns()
2023-12-21 11:09:36 +01:00
- set_element_adoption()
2017-09-05 11:48:35 +02:00
- set_guestlogin_settings()
2018-03-24 10:46:42 +01:00
- set_guestlogin_settings_base()
2023-12-21 11:09:36 +01:00
- set_ips_settings_base()
- set_is_unifi_os()
2017-09-05 11:48:35 +02:00
- set_locate_ap() (deprecated but still available as alias)
2017-09-08 12:36:12 +02:00
- set_networksettings_base()
- set_radius_account_base()
2023-12-21 11:09:36 +01:00
- set_request_method()
- set_request_timeout()
- set_site()
2018-03-24 10:46:42 +01:00
- set_site_connectivity()
- set_site_country()
- set_site_guest_access()
- set_site_locale()
- set_site_mgmt()
- set_site_name()
- set_site_ntp()
- set_site_snmp()
2017-09-05 11:48:35 +02:00
- set_sta_name()
- set_sta_note()
2020-06-20 16:12:13 +02:00
- set_super_identity_settings_base()
- set_super_mgmt_settings_base()
- set_super_smtp_settings_base()
2017-09-05 11:48:35 +02:00
- set_usergroup()
2017-09-06 17:16:32 +02:00
- set_wlan_mac_filter()
2017-09-08 12:36:12 +02:00
- set_wlansettings()
- set_wlansettings_base()
2017-09-05 11:48:35 +02:00
- site_leds()
2017-09-08 12:36:12 +02:00
- spectrum_scan()
- spectrum_scan_state()
2020-06-20 16:12:13 +02:00
- start_rolling_upgrade()
2023-12-21 11:09:36 +01:00
- stat_5minutes_aps()
- stat_5minutes_gateway()
- stat_5minutes_site()
- stat_5minutes_user()
2017-09-05 11:48:35 +02:00
- stat_allusers()
- stat_auths()
- stat_client()
2017-10-10 09:37:29 +02:00
- stat_daily_aps()
2023-12-21 11:09:36 +01:00
- stat_daily_gateway()
2017-10-10 09:37:29 +02:00
- stat_daily_site()
2023-12-21 11:09:36 +01:00
- stat_daily_user()
2021-10-19 10:39:17 +02:00
- stat_full_status()
2020-06-20 16:12:13 +02:00
- stat_hourly_aps()
2023-12-21 11:09:36 +01:00
- stat_hourly_gateway()
2020-06-20 16:12:13 +02:00
- stat_hourly_site()
2023-12-21 11:09:36 +01:00
- stat_hourly_user()
- stat_ips_events()
2021-10-19 10:39:17 +02:00
- stat_monthly_aps()
- stat_monthly_gateway()
- stat_monthly_site()
- stat_monthly_user()
2017-09-05 11:48:35 +02:00
- stat_payment()
- stat_sessions()
- stat_sites()
2018-10-20 14:29:08 +02:00
- stat_speedtest_results()
2017-09-05 11:48:35 +02:00
- stat_sta_sessions_latest()
2017-10-18 08:25:48 +02:00
- stat_status()
2017-09-05 11:48:35 +02:00
- stat_sysinfo()
- stat_voucher()
2017-09-08 12:36:12 +02:00
- unauthorize_guest()
- unblock_sta()
- unset_locate_ap() (deprecated but still available as alias)
- upgrade_device()
- upgrade_device_external()
2017-09-05 11:48:35 +02:00
2020-03-19 16:07:31 +01:00
2017-09-07 12:27:20 +02:00
## Need help or have suggestions?
2021-10-19 10:39:17 +02:00
There is still work to be done to add functionality and further improve the usability of
this class, so all suggestions/comments are welcome. Please use the GitHub
2024-01-05 10:47:11 +01:00
[Issues section ](https://github.com/Art-of-WiFi/UniFi-API-client/issues ) or the Ubiquiti
2021-10-19 10:39:17 +02:00
Community forums (https://community.ubnt.com/t5/UniFi-Wireless/PHP-class-to-access-the-UniFi-controller-API-updates-and/td-p/1512870)
2021-09-29 10:41:39 +02:00
to share your suggestions and questions.
2021-01-23 11:44:12 +01:00
2023-12-21 11:09:36 +01:00
2024-01-05 10:47:11 +01:00
#### IMPORTANT NOTE:
When encountering issues with the UniFi API using other libraries, cURL or Postman, please do **not** open an Issue. Such issues will be closed immediately.
Please use the [Discussions ](https://github.com/Art-of-WiFi/UniFi-API-client/discussions ) section instead.
2017-09-07 12:27:20 +02:00
## Contribute
2021-10-19 10:39:17 +02:00
If you would like to contribute code (improvements), please open an issue and include
your code there or else create a pull request.
2021-01-23 11:44:12 +01:00
2023-12-21 11:09:36 +01:00
2017-09-11 16:50:24 +02:00
## Credits
2019-04-29 17:35:09 +02:00
This class is based on the initial work by the following developers:
2021-09-29 10:41:39 +02:00
- domwo: https://community.ui.com/questions/little-php-class-for-unifi-api/933d3fb3-b401-4499-993a-f9af079a4a3a
2017-09-11 16:50:24 +02:00
- fbagnol: https://github.com/fbagnol/class.unifi.php
2019-04-29 17:35:09 +02:00
and the API as published by Ubiquiti:
2024-01-05 11:02:38 +01:00
- https://dl.ui.com/unifi/8.0.26/unifi_sh_api
2021-01-23 11:44:12 +01:00
2023-12-21 11:09:36 +01:00
2017-09-05 11:48:35 +02:00
## Important Disclaimer
2017-09-07 12:27:20 +02:00
2021-10-19 10:39:17 +02:00
Many of the functions in this API client class are not officially supported by Ubiquiti
and as such, may not be supported in future versions of the UniFi Controller API.