{"id":16526286,"url":"https://github.com/jens-maus/node-unifi","last_synced_at":"2025-04-05T16:06:42.842Z","repository":{"id":17069747,"uuid":"80537647","full_name":"jens-maus/node-unifi","owner":"jens-maus","description":"NodeJS class for querying/controlling a UniFi-Controller (www.ubnt.com)","archived":false,"fork":false,"pushed_at":"2024-05-01T02:33:18.000Z","size":1762,"stargazers_count":132,"open_issues_count":9,"forks_count":48,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-05-02T17:50:38.555Z","etag":null,"topics":["cloud-key","javascript","nodejs","ubiquiti","udm","udm-pro","unifi","unifi-controller"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jens-maus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"jens-maus","custom":"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=RAQSDY9YNZVCL"}},"created_at":"2017-01-31T16:18:56.000Z","updated_at":"2024-05-01T02:32:41.000Z","dependencies_parsed_at":"2023-02-19T06:16:21.242Z","dependency_job_id":"9293e597-abb0-4f89-9bff-517e528df4b7","html_url":"https://github.com/jens-maus/node-unifi","commit_stats":{"total_commits":305,"total_committers":12,"mean_commits":"25.416666666666668","dds":"0.31803278688524594","last_synced_commit":"5f4fc2339cfbecb6cfdd1a948c1cb1f25267bea2"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jens-maus%2Fnode-unifi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jens-maus%2Fnode-unifi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jens-maus%2Fnode-unifi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jens-maus%2Fnode-unifi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jens-maus","download_url":"https://codeload.github.com/jens-maus/node-unifi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361686,"owners_count":20926643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-key","javascript","nodejs","ubiquiti","udm","udm-pro","unifi","unifi-controller"],"created_at":"2024-10-11T17:27:06.818Z","updated_at":"2025-04-05T16:06:42.816Z","avatar_url":"https://github.com/jens-maus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/jens-maus","https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=RAQSDY9YNZVCL"],"categories":["API Libraries"],"sub_categories":["Node.js / JavaScript"],"readme":"\u003cimg height=\"100px\" src=\"unifi.png\" align=\"left\"\u003e\u003cbr/\u003e\n# node-unifi\n\n[![Build](https://github.com/jens-maus/node-unifi/workflows/CI/badge.svg)](https://github.com/jens-maus/node-unifi/actions)\n[![NPM version](https://img.shields.io/npm/v/node-unifi.svg?logo=npm)](https://www.npmjs.com/package/node-unifi)\n[![Downloads](https://img.shields.io/npm/dm/node-unifi.svg)](https://www.npmjs.com/package/node-unifi)\n[![License](https://img.shields.io/github/license/jens-maus/node-unifi.svg)](https://github.com/jens-maus/node-unifi/blob/master/LICENSE)\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=RAQSDY9YNZVCL)\n[![GitHub stars](https://img.shields.io/github/stars/jens-maus/node-unifi.svg?style=social\u0026label=Star)](https://github.com/jens-maus/node-unifi/stargazers/)\n\nNode-UniFi is a NodeJS module that allows to query/control [UniFi devices](http://www.ubnt.com/) via the official UniFi-Controller API. It is developed to be compatible to the UniFi-Controller API version starting with v4.x.x up to v8.x.x\n\n## Features\n* Support all UniFi-Controller API features introduced with v4.x.x up to v8.x.x.\n* Support CloudKey Gen1, CloudKey Gen2, UnifiOS-based UDM-Pro Controller as well as self-hostd UniFi controller software.\n* Returns all data in well-defined JSON parsable strings/objects.\n* Use of modern [axios](https://github.com/axios/axios)-based nodejs http library.\n* API functions returning NodeJS [Promises](https://nodejs.dev/learn/understanding-javascript-promises) for modern nodejs uses via `async`/`await` or `then()`/`catch()`.\n* Support for WebSocket-based push notifications of UniFi controllers for listening for state/object changes using [EventEmitter](https://github.com/EventEmitter2/EventEmitter2)-based nodejs functionality.\n* Usable with local and UniFI cloud accounts and with 2FA authentication.\n\n## Requirements\n* Installed [UniFi-Controller/Network](https://www.ubnt.com/download/unifi) version v4 up to v8 (UDM-Pro, UDM-SE, UDM, UDR, UDW, CloudKey Gen1/Gen2).\n* Direct network connectivity between the application using node-unifi and the host:port (normally TCP port 443 or 8443) where the UniFi controller is running on.\n* Node.js version \u003e= 16.x\n\n## Installation\nnode-unifi can be installed using the following npm command:\n\n```sh\nnpm install node-unifi\n```\n\n## Examples\nnode-unifi has been designed to be used quite straight forward and without introducing\nackward language constructs. The following example should give a brief introduction on\nhow to use node-unifi in your own applications using its Promises-based API interface:\n\n```js\nconst Unifi = require('node-unifi');\nconst unifi = new Unifi.Controller({'\u003cHOSTNAME\u003e', '\u003cPORT\u003e', sslverify: false});\n\n(async () =\u003e {\n  try {\n    // LOGIN\n    const loginData = await unifi.login('\u003cUSERNAME\u003e', '\u003cPASSWORD\u003e');\n    console.log('login: ' + loginData);\n\n    // GET SITE STATS\n    const sites = await unifi.getSitesStats();\n    console.log('getSitesStats: ' + sites[0].name + ':' + sites.length);\n    console.log(JSON.stringify(sites));\n\n    // GET SITE SYSINFO\n    const sysinfo = await unifi.getSiteSysinfo();\n    console.log('getSiteSysinfo: ' + sysinfo.length);\n    console.log(JSON.stringify(sysinfo));\n\n    // GET CLIENT DEVICES\n    const clientData = await unifi.getClientDevices();\n    console.log('getClientDevices: ' + clientData.length);\n    console.log(JSON.stringify(clientData));\n\n    // GET ALL USERS EVER CONNECTED\n    const usersData = await unifi.getAllUsers();\n    console.log('getAllUsers: ' + usersData.length);\n    console.log(JSON.stringify(usersData));\n\n    // LOGOUT\n    const logoutData = await unifi.logout();\n    console.log('logout: ' + JSON.stringify(logoutData));\n  } catch (error) {\n    console.log('ERROR: ' + error);\n  }\n})();\n```\n\nPlease note that every `unifi.XXXXX()` function returns a `Promise`, thus `async`/`await` as well as `.then()`/`.catch()` can be used accordingly.\n\n### Event-Emitter WebSockets Interface\n\nSince version 2.0.0 node-unifi supports (thanks to [unifi-axios-events](https://github.com/worldwidewoogie/unifi-axios-events)) the WebSocket interface\nof a UniFi controller. This new interface allows to listen for events using `unifi.listen()` and automatically receive events\nas soon as the UniFi controller sends them out via its WebSocket functionality. For receiving these events in a nodejs-compatible\nway node-unifi uses internally [EventEmitter2](https://github.com/EventEmitter2/EventEmitter2) which allows to execute actions based\non event filters defined by `unifi.on(...)`.\n\nAn example on how to use this EventEmitter-based functionality of node-unifi to immediately receive state changes rather than\nregularly having to poll a unifi controller for changes can be seen here:\n\n```js\nconst Unifi = require('node-unifi');\nconst unifi = new Unifi.Controller({'\u003cHOSTNAME\u003e', '\u003cPORT\u003e', sslverify: false});\n\n(async () =\u003e {\n  try {\n    // LOGIN\n    const loginData = await unifi.login('\u003cUSERNAME\u003e', '\u003cPASSWORD\u003e');\n    console.log('login: ' + loginData);\n\n    // LISTEN for WebSocket events\n    const listenData = await unifi.listen();\n    console.log('listen: ' + listenData);\n\n    // Listen for alert.client_connected\n    unifi.on('alert.client_connected', function (data) {\n      const ts = new Date(data[0].timestamp).toISOString();\n      console.log(`${ts} - ${this.event}: ${data[0].parameters.CLIENT.id} (${data[0].parameters.CLIENT.name})`);\n    });\n\n    // Listen for alert.client_disconnected\n    unifi.on('alert.client_disconnected', function (data) {\n      const ts = new Date(data[0].timestamp).toISOString();\n      console.log(`${ts} - ${this.event}: ${data[0].parameters.CLIENT.id} (${data[0].parameters.CLIENT.name})`);\n    });\n\n    // Listen for ctrl.* events\n    unifi.on('ctrl.*', function () {\n      console.log(`${this.event}`);\n    });\n  } catch (error) {\n    console.log('ERROR: ' + error);\n  }\n})();\n```\n\nMore examples can be found in the \"examples\" sub-directory of this GitHub repository.\n\n## Moving from v1 of node-unifi to v2+\nIf you are having an application still using the obsolete v1 version of node-unifi and you want to port it to using the new/revised\nv2 version, all you have to do is:\n\n* make sure your application can deal with NodeJS [Promises](https://nodejs.dev/learn/understanding-javascript-promises) as all node-unifi API functions return proper Promises allowing to use `async`/`await` or `.then()`/`.catch()` logic for synchronous processing of events (see Examples) rather than expecting callback functions, forcing you to nest them properly.\n* eliminate the previously necessary `site` function argument required when calling a node-unifi function. Now you can either use the `{ site: 'my site' }` argument when passing contructor options to node-unifi or you switch to a different site using `unifi.opts.site='my site'` before calling a node-unifi API function.\n* as the API functions had been changed to work on a single site only, make sure your app is changed so that it expects a single site JSON return dataset only.\n* The new version by default verifies SSL connections and certificates. To restore the behaviour of the old version set `sslverify: false` in the constructor options\n\n## References\nThis nodejs package/class uses functionality/Know-How gathered from different third-party projects:\n\n* [Art-of-WiFi/UniFi-API-client](https://github.com/Art-of-WiFi/UniFi-API-client)\n* [worldwidewoogie/unifi-axios-events](https://github.com/worldwidewoogie/unifi-axios-events)\n* [unifi_sh_api](https://dl.ui.com/unifi/5.12.35/unifi_sh_api)\n\n## Use-Cases\nThe following projects are known to use this nodejs class for query/control UniFi devices:\n\n* [ioBroker.unifi](https://github.com/iobroker-community-adapters/ioBroker.unifi)\n\n## License\nThe MIT License (MIT)\n\nCopyright (c) 2017-2023 Jens Maus \u0026lt;mail@jens-maus.de\u0026gt;\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjens-maus%2Fnode-unifi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjens-maus%2Fnode-unifi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjens-maus%2Fnode-unifi/lists"}