Download the PHP package art-of-wifi/unifi-api-client without Composer

On this page you can find all versions of the php package art-of-wifi/unifi-api-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package unifi-api-client

UniFi Controller API client class

License Packagist Version Downloads PHP Version

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.

Why use this API client?

Supported Versions

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)

Requirements

UniFi OS Support

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.

Remote API access to UniFi OS-based gateways

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

Upgrading from previous versions

When upgrading to 2.3.0 or later, please note:

When upgrading from a version before 2.0.0, please:

Installation

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:

Or manually add the package to your composer.json file:

Finally, be sure to include the composer autoloader in your code if your framework doesn't already do this for you:

Authentication

The API client supports three authentication methods. Choose the one that fits your setup.

Option 1: API Key (recommended for UniFi OS)

API key authentication is stateless — no login/logout flow is needed. This is the simplest way to get started with UniFi OS-based controllers.

How to generate an API key:

  1. Open your UniFi OS console in a browser
  2. Navigate to Integrations in the sidebar menu
  3. Click Create New API Key
  4. Copy the generated key — it will not be shown again

The API key inherits the permissions from the admin user that created it.

Key points:

Option 2: Username/Password

The traditional authentication method that works with both self-hosted controllers and UniFi OS consoles.

Requirements for username/password authentication:

Option 3: Site Manager Proxy

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.

Finding the console ID: The console ID (host ID) is visible in the URL when managing a console via unifi.ui.com:

Site 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:

SSL 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.

When to use which method?

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.

Migrating from username/password to API keys

If you have existing code using username/password authentication and want to switch to API keys, the changes are minimal:

Before (username/password):

After (API key):

What changes:

  1. Pass empty strings for the $user and $password constructor parameters
  2. Call set_api_key() instead of login()
  3. Remove logout() calls (or leave them — they will be silently ignored)
  4. In your exception handling, LoginFailedException and LoginRequiredException will no longer occur; you can keep or remove those catch blocks as you prefer

IMPORTANT NOTES:

  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:

    https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard

    jl3z2shm is the short site "name" and the value to assign to $site_id.

  2. The 6th optional parameter that is passed to the constructor in the above example (true), enables validation of 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.

  3. Using an administrator account ($controller_user in the above example) with read-only permissions can limit visibility on certain collection/object properties. See this issue and this issue for an example where the WPA2 password isn't visible for read-only administrator accounts.

Code Examples:

More code examples are available in the examples/ directory.

Exception handling

The API Client class throws Exceptions for various error conditions instead of using PHP's trigger_error() function. This allows for more granular error handling in your application code.

All Exceptions thrown by the API Client class implement the UniFi_API\Exceptions\UnifiApiExceptionInterface interface, and all but one extend the UniFi_API\Exceptions\UnifiApiException base class. Catch the interface (or the base class) when you want to handle all client errors uniformly.

The following Exceptions can be thrown:

Exception Thrown when
ControllerErrorException the controller returns an error in its response, e.g. api.err.Invalid; see getApiErrorCode()/getResponse()
ConsoleOfflineException the console is offline when using the Site Manager proxy (HTTP 408)
CurlExtensionNotLoadedException the PHP cURL extension is not loaded
CurlGeneralErrorException cURL reports an error; see getHttpResponseCode()/getCurlGetinfoResults()
CurlTimeoutException the cURL request timed out
EmailInvalidException an invalid email address is passed to a method
InvalidArgumentException an invalid argument is passed to a method (extends PHP's \InvalidArgumentException, not UnifiApiException)
InvalidBaseUrlException the base URL passed to the constructor is invalid
InvalidCurlMethodException an unsupported HTTP method is passed to set_curl_method()
InvalidSiteNameException the site name is invalid
JsonDecodeException the controller response could not be decoded as JSON
LoginFailedException login failed; see getHttpResponseCode()
LoginRequiredException a method that requires login is called before login()
MacAddressEmptyException an empty MAC address is passed to a method
MacAddressInvalidException an invalid MAC address is passed to a method
MethodDeprecatedException a deprecated method is called
NotAUnifiOsConsoleException a UniFi OS-only method is called against a classic controller

Here is an example of how to catch each of the Exceptions individually:

The PHP DocBlocks for most public methods contain @throws UnifiApiException, which covers the Exceptions that the internal request helper can raise (LoginRequiredException, ControllerErrorException, JsonDecodeException, the cURL Exceptions and LoginFailedException). It is recommended to catch specific Exceptions where your application needs to respond differently to them.

The list_alarms.php example in the examples/ directory is a good starting point to see how you can implement Exception handling.

Functions/methods supported

The API Client class currently supports a large and growing number of functions/methods to access the UniFi Controller API. Please refer to the comments/PHP DocBlocks in the source code for more details on each of the functions/methods, their purpose, and their respective parameters.

If you are using an advanced IDE such as PHPStorm or VS Code, you can use its code completion and other features to explore the available functions/methods thanks to the extensive PHP DocBlocks throughout the code.

For a quick overview of the available functions/methods, you can also check the API Reference here:
API Reference

Need help or have suggestions?

There is still work to be done to add functionality and further improve the usability of this API Client class, so all suggestions/comments are welcome. Please use the GitHub Issues section or the Ubiquiti Community forums (https://community.ui.com/questions/PHP-client-class-to-access-the-UniFi-controller-API-updates-and-discussion-part-2/a793904e-6023-4a7f-bcae-340db2a03fc1) to share your suggestions and questions.

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 section instead.

Looking for version 1.x.x?

With versions 1.x.x of the API client, the entire client was contained within a single file which can be useful in specific cases. This has changed with version 2.0.0 where the code is now split across multiple files and inclusion in your project is managed using composer.

If you are looking for the version 1.x.x code, you can tell composer to install that version by using the following syntax in your composer.json file:

Alternatively, you can download the latest 1.x.x code from the releases page.

Whenever necessary, we will make sure to update the version_1 branch with the latest 1.x.x code.

Credits

This API Client class is based on the initial work by the following developers:

and the API as published by Ubiquiti:

Contributors

A big thanks to all the contributors who have helped with this project!

Contributors

If you would like to contribute to this project, please open an issue and include your suggestions or code there or else create a pull request.

About Art of WiFi

Art of WiFi develops software and tools that enhance the capabilities of UniFi networks. From captive portals and reporting solutions to device search utilities, our goal is to make UniFi deployments more powerful and easier to manage.

If you're looking for a specific solution or just want to see what else we offer, feel free to explore our website:

Important Disclaimer

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.


All versions of unifi-api-client with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4.0
ext-curl Version *
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package art-of-wifi/unifi-api-client contains the following files

Loading the files please wait ...