1.0.1 • Published 2 years ago

unifi.js v1.0.1

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
2 years ago

Unifi.js

An Unofficial Javascript / NodeJS client for Ubiquity Unifi Controllers. Allows for RESTful requests and listening to WebSocket events. Designed around the documentation found here.

Note: While you can hackishly make it work in a browser, because the Unifi Controllers use Cookie-Based authentication you will have CORS issues when trying to use this library in a browser. It is recommended to use this package in a NodeJS server environment.


Quick Start

const host = '192.168.1.1'
const port = 443
const https = true
const user = 'someuser'
const password = 'somepassword'
const udm = true
const Unifi = require('unifi.js')
Unifi.initialize( host, user, password, port, https, udm ).then( client => {
    // interact with the client here
    client.disconnect()
})

Constructor

new Unifi(host, user, password, port, https, udm)

host (string) The IP address or hostname used to access the Unifi Controller

user (string) The username or email of the user which you will be authenticating as

password (string) The password of the user which you will be authenticating as

port (uint) The port which the controller is accessable from

https (boolean) If the unifi controller is using https / tls

udm (boolean) If the unifi controller is being access from a Unifi Dream Machine device such as the UDM Base, UDM Pro or UDM SE

Properties

authenticated (boolean) If the client has successfully authenticated

user (object/boolean) If the client is authenticated, this is an object containing the authenticated user's information

connected (boolean) Describes if the WebSocket for events is connected

Methods

async request, get, delete, head, options, post, put, patch

These methods are all customized proxies of the methods from axios. While their API's are the same, they will never throw an error. Instead, the response.data object may be an error which has a property data which allows you to see the original data from the response.

Also, if the endpoint returns data.data, the response.data will contain the data.data object instead of the top level data object. This is used to normalize responses which are in different formats.

disconnect

Disconnects any connected sockets and resets authentication.

async authenticate

Sends an authentication request with the user & password from the constructor, and then as a byproduct will initialize the events WebSocket connection on success.

On failure, this method will throw an Error

static async initialize (host, user, password, port = 447, https = true, udm = true)

A static function which creates a new client and attempts to authenticate before returning the client. Will throw an error if the authentication fails.

host (string) The IP address or hostname used to access the Unifi Controller

user (string) The username or email of the user which you will be authenticating as

password (string) The password of the user which you will be authenticating as

port (uint) The port which the controller is accessable from

https (boolean) If the unifi controller is using https / tls

udm (boolean) If the unifi controller is being access from a Unifi Dream Machine device such as the UDM Base, UDM Pro or UDM SE

Events

authenticated (<boolean> is)

Triggered when the authentication state has changed.

is (boolean) The state of authentication

connected (<boolean> is)

Triggered when the event websocket connection state has changed.

is (boolean) The state of the connection

event (<object> msg)

Triggered when an event message is received from the events websocket.

msg (object)

An object containing information on the event