A PHP API client class to interact with Ubiquiti's UniFi Controller API
A PHP API client class to interact with Ubiquiti's UniFi Controller API
A PHP class that provides access to Ubiquiti's UniFi Network Application API.
This class is used by our API Browser tool, which can be found here.
The package can be installed manually or by using composer/packagist for easy inclusion in your projects. See the installation instructions below for more details.
custom_api_request() for calling any API endpoint.| Software | Versions |
|---|---|
| UniFi Network Application/controller | 5.x, 6.x, 7.x, 8.x, 9.x, 10.x (10.5.62 is confirmed) |
| UniFi OS | 3.x, 4.x, 5.x (5.1.26 is confirmed) |
php-curl) module enabledphp-json) module enabled (always available on PHP 8.0 and later, but it can be
disabled at compile time on PHP 7.4)Starting from version 1.1.47, this API client also supports UniFi OS-based controllers. These applications/devices/services have been verified to work:
The API Client automatically detects UniFi OS consoles/servers and adjusts URLs and several functions/methods accordingly.
UniFi OS-based consoles require you to connect using port 443, whereas port 8443 is used for the self-hosted/software-based controllers. When connecting to UniFi OS Server, you are required to use port 11443.
When connecting to a UniFi OS-based gateway through the WAN interface, you need to create a specific firewall rule to
allow this. See this blog post on the Art of WiFi website for detailed instructions when using the "Classic"
firewall:
https://artofwifi.net/blog/how-to-access-the-unifi-controller-by-wan-ip-or-hostname-on-a-udm-pro
See this blog post when using the Zone-Based firewall (ZBF):
https://artofwifi.net/blog/how-to-access-the-unifi-controller-by-wan-ip-or-hostname-on-a-udm-pro-using-zbf
When upgrading from a version before 2.0.0, please:
The preferred installation method is through Composer. Follow these installation instructions if you don't have Composer installed already.
Once Composer is installed, execute this command from the shell in your project directory:
composer require art-of-wifi/unifi-api-clientOr manually add the package to your composer.json file:
{
"require": {
"art-of-wifi/unifi-api-client": "^2.0"
}
}Finally, be sure to include the composer autoloader in your code if your framework doesn't already do this for you:
/**
* load the class using the composer autoloader
*/
require_once 'vendor/autoload.php';The API client supports three authentication methods. Choose the one that fits your setup.
API key authentication is stateless — no login/logout flow is needed. This is the simplest way to get started with UniFi OS-based controllers.
require_once 'vendor/autoload.php';
$unifi_connection = new UniFi_API\Client('', '', 'https://unifi:443', 'default');
$unifi_connection->set_api_key('your-api-key-here');
$results = $unifi_connection->list_alarms(); // no login() neededHow to generate an API key:
The API key inherits the permissions from the admin user that created it.
Key points:
login() or logout() calls are needed (calling them is harmless and will be ignored)The traditional authentication method that works with both self-hosted controllers and UniFi OS consoles.
require_once 'vendor/autoload.php';
$unifi_connection = new UniFi_API\Client(
$controller_user,
$controller_password,
$controller_url,
$site_id
);
$login = $unifi_connection->login();
$results = $unifi_connection->list_alarms();Requirements for username/password authentication:
If your console is managed through unifi.ui.com and you don't have a direct network path to it, you can route API requests through the Ubiquiti Site Manager cloud proxy. This uses the Site Manager API connector to reach the console via UI.com's cloud infrastructure.
require_once 'vendor/autoload.php';
$client = UniFi_API\Client::connect_via_site_manager(
'245A4CA234150000000005F23204000000000638FE970000000061156371:48913759', // console ID
'your-site-manager-api-key', // Site Manager API key
'default' // site (optional)
);
// No login() needed — proxy mode is stateless
$stats = $client->stat_daily_site();Finding the console ID:
The console ID (host ID) is visible in the URL when managing a console via unifi.ui.com:
https://unifi.ui.com/consoles/{console_id}/network/default/dashboardSite Manager API key: Generate a Site Manager API key at https://unifi.ui.com under your account settings. This is not the same as a local controller API key generated in the UniFi OS console.
Requirements:
You can also enable/disable proxy mode on an existing client instance:
$client = new UniFi_API\Client('', '', 'https://127.0.0.1', 'default');
$client->enable_site_manager_proxy($console_id, $site_manager_api_key);
// ... make API calls through the proxy ...
$client->disable_site_manager_proxy();
// Client must be re-configured (login, set_api_key, etc.) before making direct callsSSL verification:
In proxy mode, all requests go to api.ui.com which has a valid public CA certificate. SSL peer and host
verification is automatically enforced.
Performance note: Proxied requests add ~800ms of latency compared to direct access due to the cloud hop. Use direct access when available. The proxy is best suited for remote/headless deployments where a direct network path does not exist.
Rate limits:
The Site Manager API enforces rate limits (10,000 requests/minute for v1 stable). When exceeded,
the API returns HTTP 429 with a Retry-After header.
| Scenario | Method |
|---|---|
| Controller is part of a UniFi Fabric | API key (required) |
| UniFi OS console (UDM, UDR, UCG, etc.) | API key (recommended) or username/password |
| UniFi OS Server | API key (recommended) or username/password |
| Self-hosted Network Application (non-UniFi OS Server) | Username/password (API keys are not supported) |
| Remote console via UI.com (no direct network path) | Site Manager proxy |
Important: When your controller is a member of a UniFi Fabric, username/password authentication is not available. You must use API key authentication. UniFi Fabric uses centralized identity management which does not support local login sessions through the API.
If you have existing code using username/password authentication and want to switch to API keys, the changes are minimal:
Before (username/password):
$unifi_connection = new UniFi_API\Client($user, $password, $url, $site_id);
$unifi_connection->login();
$results = $unifi_connection->list_alarms();
$unifi_connection->logout();After (API key):
$unifi_connection = new UniFi_API\Client('', '', $url, $site_id);
$unifi_connection->set_api_key($api_key);
$results = $unifi_connection->list_alarms();What changes:
$user and $password constructor parametersset_api_key() instead of login()logout() calls (or lNo open issues yet, or sync has not completed.