This repository is a small TypeScript CLI that demonstrates how to call the UniFi Network Integration API with an application API key.
The current example sends a GET request to the sites endpoint, handles common self-signed certificate problems, and prints the parsed JSON response to stdout.
- A recent version of Node.js and npm
- Network access to your UniFi controller or gateway
- A UniFi application API key
- Local DNS or a hosts entry that resolves
unifito your UniFi endpoint
This project runs a TypeScript build during npm install, so runtime.json must exist first.
Create a file named runtime.json in the repository root:
{
"api-key": "paste-your-unifi-application-api-key-here"
}Notes:
runtime.jsonis gitignored and should stay local- The API key must be an application API key
- Site Manager keys and Protect API keys are not valid for this example
The hostname is currently hard-coded as unifi in src/main.ts.
If your UniFi controller is reachable at 192.168.1.1, a local hosts entry would look like this:
192.168.1.1 unifi
If your environment uses a different hostname, either:
- Update local DNS or your hosts file so
unifiresolves correctly - Change the hostname constant in
src/main.ts
npm installnpm startThis package also publishes a CLI named unifi-api-usage-example in package.json.
During npm install, the postinstall hook runs npm link --ignore-scripts ., so a successful install can also expose the example as a direct shell command:
unifi-api-usage-exampleThis uses the same repository checkout, runtime.json, and compiled distribution/ output as npm start.
Notes:
- This assumes your npm-linked executable path is configured correctly in your shell environment
- Because the linked executable points back to this checkout, keep the repository at the same path or rerun
npm installafter moving it - In shell setups that append
${PWD}/node_modules/.binor the git root'snode_modules/.bintoPATHwhen youcdinto the repository, other project-local npm binaries in this checkout will also be callable directly
The current implementation sends a request equivalent to:
GET /proxy/network/integration/v1/sites?offset=0&limit=0&filter= HTTP/1.1
Host: unifi
Accept: application/json
X-API-Key: <your-api-key>On success, the CLI prints the parsed response:
{ data: ... }
Many UniFi deployments use self-signed or privately issued certificates. This example includes a fallback for common certificate validation failures such as:
DEPTH_ZERO_SELF_SIGNED_CERTERR_TLS_CERT_ALTNAME_INVALIDSELF_SIGNED_CERT_IN_CHAINUNABLE_TO_GET_ISSUER_CERT_LOCALLYUNABLE_TO_VERIFY_LEAF_SIGNATURE
When one of those errors occurs, the CLI:
- Opens a TLS connection with verification temporarily disabled so it can inspect the peer certificate
- Converts the peer certificate into PEM format
- Builds an
https.Agentthat trusts that certificate - Retries the request using a hostname that matches the certificate when possible
This makes the example more useful in lab, home, and internal-network environments where public CA trust is not always available.
npm startbuilds and runs the compiled CLI fromdistribution/npm run buildcompiles the TypeScript sourcesnpm testruns Jest after buildingnpm run lintruns ESLintnpm run fixruns ESLint with automatic fixesnpm run generate-secretprints a random URL-safe secret
src/main.tscontains the CLI, request logic, and TLS certificate fallbacksrc/runtime.tsloads the runtime configuration fromruntime.jsonruntime.schema.jsondocuments the required runtime configuration shapescripts/contains optional helper scripts for certificate, PKCE, and local environment tasks
Create runtime.json before running npm install.
Make sure npm install completed successfully, your npm-linked executable directory is on PATH, and rerun npm install if the repository was moved after it was linked.
Your machine cannot resolve unifi. Fix local DNS, add a hosts entry, or update the hostname in src/main.ts.
Make sure the key in runtime.json is an application API key and that it is still active.
If the certificate fallback still cannot complete the request, use a hostname that appears in the certificate's SAN or common name, or install the correct CA certificate in your environment.
- Do not commit
runtime.jsonor real API keys - Use the least-privileged API key that works for your use case
- If you discover a security issue, see
SECURITY.md
See CONTRIBUTING.md for contribution guidance and CODE_OF_CONDUCT.md for community expectations.