npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unifi-client

v0.11.0

Published

NodeJs client for Unifi products (https://www.ui.com/)

Downloads

250

Readme

unifi-client

NPM version CI codecov Downloads License Known Vulnerabilities unifi-client Donate GitHub stars Package Quality

Bugs Code Smells Duplicated Lines (%) Lines of Code Maintainability Rating Quality Gate Status Reliability Rating Security Rating Technical Debt Vulnerabilities

Dependencies update - renovate

NPM

This library is a nodejs client to talk with unifi rest API .

Start

install it

yarn add unifi-client

or via npm

npm i unifi-client

and import it in your code :

import Controller from 'unifi-client'

or with require :

const { Controller } = require('unifi-client')

Requirements

  • Installed UniFi-Controller version v6 or more, UnifiOs or not ( cloud keys / UDM or just unifi controller software ) ( not tested below v6 )
  • A network connectivy between unifi-client and the controller ( env http_proxy and https_proxy can be handled automaticaly, but not tested )
  • An account, cloud accounts, local accounts and 2FA are available
  • Node.js version >= LTS (14)

Example

Examples are located in examples folder

import Controller from 'unifi-client'

// works with local account, check examples for 2FA
const controller = new Controller({
  username: 'ubnt',
  password: 'ubnt',
  url: 'https://unifi',
  strictSSL: false
});

//login to the controller
await controller.login()

//retrieve sites
const sites = controller.getSites()

//work on one site
const site = sites[0];

//example request to get firewall rules
const rules = await site.firewall.getRules();

To work on multiples sites on the same time

const controller = new Controller({...});

const sites = controller.getSites();

//get firewallRules on two sites
const rules = await site[0].firewall.getRules();
const rules2 = await site[1].firewall.getRules();

How to use the HTTP instances

//use the controller instance directly . Authentication, url construction and other is already managed for you
const self = await controller.getInstance().get('/api/self');

//for a custom site :
const topology = await site.getInstance().get('/topology');

Get firewall rules, it return an array of FWRule

const rules = await site.firewall.getRules();

Instances

The instances returned by getInstance are basicaly some axios instances . With some additions :

//url params
// use them to manage a rest url for example
instance.get('/url/:uuid', {
    urlParams: {
        uuid: 'my-uuid'
    }
})
// will call the url /url/my-uuid


// apiVersion :
// some api call are on the v2 api, so you can set the api version when calling
console.log(site.name); // default
site.getInstance().get('/super/endpoint', {
    apiVersion: 2
});
// will call the url /v2/api/site/default/super/endpoint

how to use URLs from the unifi front :

The urls of unifi are like :

  • /proxy/network/v2/api/site/default/notifications for unifiOs or
  • /v2/api/site/default/notifications for non unifiOs

the url is constructed like : /[v<ApiVersion>/]api/site/<sitename>[/<urlToCall>]

  • ApiVersion : the version of the API (for version > 1)
  • sitename : the name of the site (already included in site instance)

To illustrate, to call the url I see on my browser network tabs :

https://192.168.1.1/proxy/network/v2/api/site/default/notifications

I just need to :

// configure my controller
const controller = new Controller({
    username: 'ubnt',
    password: 'ubnt',
    url: 'https://192.168.1.1',
    strictSSL: false
});

//login to the controller
await controller.login()

//retrieve sites
const sites = controller.getSites()

// select the site "default"
const site = sites.find((site) => site.name === 'default');

//call my url
const notifications = site.getInstance().get('/default/notifications', {
    apiVersion: 2
});

//do something with notifications

More examples in the folder examples

Websockets

This library supports websockets, you can listen on them with :

// initWebSockets for controller :
await controller.initWebSockets();

// listen for controller websockets ( only for unifiOS )
controller.on() / controller.ws.on()

// listen for super site websockets
controller.superWS.on();


// initWebSockets for site :
await site.initWebSockets();
// listen for this site websockets
site.on() / site.ws.on()

// doesn't known the name of the event ? you can listen on joker :
controller.on('*', (eventName, ...args) => {
    console.log(eventName, ...args);
});

// want to listen on all websockets of a controller on the same listener
// all websockets registered :
// - controller websockets if unifiOS
// - super site WS
// - all sites where ws are init ( closed or not )
controller.globalWS.on('*', (eventName, ...args) => {
    console.log(eventName, ...args);
});

the support of websocket is experimental, and with a really bad coverage . Doesn't hesitate to open a PR, to add more websockets types

We add some optionnal dependencies to improve performances, more informations read here they can be skipped by using npm install --no-optional

Technical documentation

All the technical documentation is available here

Work In Progress

check technical documentation for available methods

Tests

This library is auto-tested on :

  • UDM-pro : latest (7.3.83)

  • Unifi controller : latest, 7.3.83, 7.2.95, 7.1.68, 7.0.25, 6.5.55, 6.4.54, 6.2.26, 6.0.45 (only some functions are tested, in an empty environment)

with the last node LTS

References

This nodejs package/class uses functionality/Know-How gathered from different third-party projects:

Debug

To debug, you can use the debug library, just set the DEBUG environnement variable (check this) .

  • unifi-client : base debug namespace
  • unifi-client:<className> : will show debug for this class (eg : unifi-client:Controller will log controller logs )
  • unifi-client:axios : will logs axios request (can contain secrets informations)
  • unifi-client:axios:verbose : will logs more things on axios, like send and receive payload (can contain secrets informations)
  • unifi-client:axios:curl : will log curl cmd corresponding to the axios request (can contain secrets informations)

Useful links

ubntwiki : https://ubntwiki.com/products/software/unifi-controller/api