Philips Hue API Library for Node.js

5.0.0-beta.16 · active · verified Wed Apr 22

The `node-hue-api` library provides a comprehensive, Promise-based API for interacting with Philips Hue Bridges from Node.js applications. It abstracts the underlying Hue REST API, offering 100% coverage for both local network and remote internet access. The current stable major version is `v4`, with `v5.0.0-beta.16` actively in development, indicating a consistent update cadence through minor and patch releases. Key differentiators include its robust handling of self-signed bridge certificates for secure local connections, built-in rate limiting to comply with Hue API best practices (since v4.0.0), and full support for modern JavaScript `async/await` patterns. It offers complete control over lights, groups, scenes, sensors, schedules, and bridge configuration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to discover local Philips Hue Bridges, register a new user (if needed), connect to the API, and then control a light by setting its state.

import { HueApi, discovery, LightState } from 'node-hue-api';
import { createLocal } from 'node-hue-api/dist/esm/api/Api';

const APP_NAME = 'my-node-hue-app';
const DEVICE_NAME = 'my-computer';

async function discoverAndConnect() {
  let host;
  try {
    console.log('Searching for Hue Bridges...');
    const discoveryResults = await discovery.upnpSearch(3000);
    if (discoveryResults.length === 0) {
      console.log('No bridges found via UPnP. Trying nUPnP...');
      const nupnpResults = await discovery.nupnpSearch();
      if (nupnpResults.length === 0) {
        throw new Error('No Hue Bridges found on the network.');
      }
      host = nupnpResults[0].ipaddress;
    } else {
      host = discoveryResults[0].ipaddress;
    }
    console.log(`Found bridge at ${host}`);

    const savedUsername = process.env.HUE_USERNAME ?? ''; // Or load from a config file
    let username = savedUsername;

    if (!username) {
      console.log('No username found, registering new user...');
      const unauthenticatedApi = createLocal(host).get.</unauthenticatedApi>
      username = await unauthenticatedApi.users.createUser(APP_NAME, DEVICE_NAME);
      console.log(`New user created: ${username}. Save this for future use.`);
    } else {
      console.log(`Using existing username: ${username}`);
    }

    const hueApi = createLocal(host).connect(username);
    console.log('Successfully connected to the Hue Bridge.');

    // Example: Set a light state
    const lights = await hueApi.lights.getAll();
    if (lights.length > 0) {
      const firstLightId = lights[0].id;
      console.log(`Setting first light (${lights[0].name}) to a random color and brightness.`);
      const state = new LightState()
        .on()
        .brightness(50 + Math.floor(Math.random() * 50)) // 50-100%
        .hue(Math.floor(Math.random() * 65535)); // Random hue
      await hueApi.lights.setLightState(firstLightId, state);
      console.log('Light state updated.');
    } else {
      console.log('No lights found to control.');
    }

  } catch (err) {
    console.error(`Error during discovery or connection: ${err.message}`);
  }
}

discoverAndConnect();

view raw JSON →