https://github.com/delian/node-unifiapi
UniFi API ported to Node.JS
https://github.com/delian/node-unifiapi
api controller ubiqiuiti unifi wifi
Last synced: 11 months ago
JSON representation
UniFi API ported to Node.JS
- Host: GitHub
- URL: https://github.com/delian/node-unifiapi
- Owner: delian
- License: gpl-3.0
- Created: 2017-01-25T08:57:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-11-24T15:59:56.000Z (over 5 years ago)
- Last Synced: 2025-05-07T19:58:23.617Z (about 1 year ago)
- Topics: api, controller, ubiqiuiti, unifi, wifi
- Language: JavaScript
- Size: 3.71 MB
- Stars: 51
- Watchers: 6
- Forks: 19
- Open Issues: 20
-
Metadata Files:
- Readme: README.hbs
- License: LICENSE
Awesome Lists containing this project
- awesome-unifi - delian/node-unifiapi - UniFi API ported to Node.js. (API Libraries / Node.js / JavaScript)
README
# node-unifiapi
UniFi API ported to Node.JS
This library is a rewrite of the PHP based UniFi-API-Browser written in JavaScript for Node-JS.
It is mimicking the UniFi-API-Browser API calls (the same commands the same effects) for Ubiquiti Unifi Controller versions 4 and 5 with addition of few more generic calls.
## Major features
* Implements the major (if not all) calls to the REST API of the Ubiquiti for Unifi Controller
* Supports WebRTC (over the Ubiquiti Unifi Cloud) protocol. If you have your devices registerred in the Unifi Cloud you can access them and execute the same REST API calls over WebRTC
* Supports SSH access to the devices that support it (mostly UAP) over WebRTC
* Supports Plug-in replacement for the WebRTC module (tested with electron-webrtc) in case wrtc doesn't work for you for some reason
## Warning
Quite unstable mostly due to the instability of the WebRTC implementation in Node.JS.
Also highly incompleted and untested. Any help appreciated!
## Installation
To install run:
npm install node-unifiapi --save
The installation DOES NOT depend on the Node's wrtc module anymore.
If you want to use the WebRTC functionality (Unifi cloudapi and SSH tunnels to device) you have to install webrtc module, and if this module is not node-webrtc you have to explicitly specify it according to the documentation.
To install node-webrtc do:
npm install node-webrtc --save
The install requirements for node-webrtc are visible here [node-webrtc](https://github.com/js-platform/node-webrtc). Please consult with the installation requirements of this module in order to be able to install it.
### XOpenDisplay Error
A frequent error caused by node-webrtc module is the one defined in issue [#281](https://github.com/js-platform/node-webrtc/issues/281)
node: symbol lookup error: [local-path]/build/wrtc/v0.0.61/Release/node-v47-linux-x64/wrtc.node: undefined symbol: XOpenDisplay
It happens mostly on Linux, almost exquisively if the Linux have X11 subsystem, although it is not caused directly by it (but a bad linking).
The easiest method to avoid it is to use non desktop (non X11 based) Linux distribution, like Ubuntu Server. We all hope that in version 0.0.62 of the node-webrtc module this issue will be fixed.
### Segmentation errors
Again, the node-wrtc module is quite unstable sometimes. I find it best working on OSX or Linux (Linux Server, without X11 libraries) with the
prebuilt binary images (which for the moment requires node version 6.9 maximum for Linux).
The problem with this instability seems to be well known to the wrtc community but I cannot predict when it will be fixed.
However, the unifiapi module uses the standart webrtc api, so it could work with any webrtc module with the standard api.
Following is an example with electron-webrtc module:
npm install electron-webrtc
And then a test (example) code:
let cloud = require('node-unifiapi/cloudapi');
let wrtc = require('electron-webrtc')({ headless: true });
let r = cloud({ device-id: 'xxx-xxx-xx-xx-xx-xx', username: 'myuser', password: 'mypass', webrtc: wrtc, waiter: 1000 }).api;
r.stat_sessions().then(data => console.log('Success', data).catch(err => console.log('Error', err);
The waiter property sets delay between every command sent to the webrtc in ms. I found electron-webrtc working better, if there is at least 500ms delay between the calls.
## Test from CLI
There is a sister project available here [https://github.com/delian/unificli](https://github.com/delian/unificli) which provides CLI tool where all (or most) of the calls of this API are exposed as REPL CLI commands one could use to test.
## Usage
All the API are Promises
### Direct access to Ubiquiti Unifi Controller
If you have a direct access to Ubiquiti Unifi Controller, you could use the following API:
let unifi = require('node-unifiapi');
let r = unifi({
baseUrl: 'https://127.0.0.1:8443', // The URL of the Unifi Controller
username: 'ubnt',
password: 'ubnt',
// debug: true, // More debug of the API (uses the debug module)
// debugNet: true // Debug of the network requests (uses request module)
});
r.stat_sessions()
.then((data) => {
console.log('Stat sessions', data);
return r.stat_allusers();
})
.then((data) => {
console.log('AP data', data);
})
.catch((err) => {
console.log('Error', err);
})
### Access via Unifi Cloud and WebRTC
If you have to access the Unifi Controller if it is behind NAT and you need to use WebRTC to access it or known only via Unifi Cloud:
let cloud = require('node-unifiapi/cloudapi');
let r = cloud({
deviceId: '801bb78e12c80000000001a22aea000000000203c905000000066660aaaa', // The cloud id of the device
username: 'clouduser',
password: 'cloudpass',
// debug: true, // More debug of the API (uses the debug module)
// debugNet: true // Debug of the network requests
});
r.api.stat_sessions()
.then((data) => {
console.log('Stat sessions', data);
return r.api.stat_allusers();
})
.then((data) => {
console.log('AP data', data);
})
.catch((err) => {
console.log('Error', err);
})
Be careful - when we use the cloud access all the Unifi calls are available under the .api property, to not confuse with the API calls that are related to the cloud management itself.
# Rebuild Readme.md
If you want to modify the README.md file for any reason (added jsdoc comment somewhere or have done change to README.hbs) please run
npm run readme
# API
{{>main}}