@serkonda7/unifi-api
v0.1.1
Published
TypeScript client for the UniFi Site Manager API
Maintainers
Readme
unifi-api
TypeScript client for the UniFi Site Manager API.
ℹ️ Disclaimer
This project is an unofficial API client for the UniFi Site Manager API.
It is not affiliated with, endorsed by, or sponsored by Ubiquiti Inc. UniFi and related trademarks are the property of Ubiquiti Inc.
Use of this package requires a UniFi account and a Site Manager API key.
The authors provide this client as-is and make no guarantees regarding correctness, availability, compatibility, or future changes of the UniFi Site Manager API.
📦 Installation
bun install @serkonda7/unifi-api🚀 Usage
Also check out the official UniFi Site Manager API docs: https://developer.ui.com/site-manager-api/
To get an API key, sign in at https://unifi.ui.com, open the API section, and create an API key. Personal API keys can only reach consoles owned by your account; organization API keys can reach all consoles in the organization.
Site Manager endpoints are rate limited — a client that exceeds its limit receives HTTP 429 with a Retry-After header.
Create the client
import { createUnifiClient } from '@serkonda7/unifi-api'
const unifi = createUnifiClient({
apiKey: process.env.UNIFI_API_KEY!,
// baseUrl defaults to https://api.ui.com
})Each client owns an isolated instance, so auth can't leak between clients. Rotate the key later with unifi.setApiKey(nextKey). unifi.instance exposes the underlying hey-api client (interceptors, raw SDK calls).
Make requests
import { createUnifiClient, UnifiApiError } from '@serkonda7/unifi-api'
const unifi = createUnifiClient({ apiKey: process.env.UNIFI_API_KEY! })
try {
// GET /v1/hosts — follows `nextToken` pagination automatically
const hosts = await unifi.hosts.listAll()
// GET /v1/hosts/{id}
const hostId = hosts[0]?.id
if (hostId) {
const host = await unifi.hosts.get(hostId)
// GET /v1/devices, filtered by host
const devices = await unifi.devices.listAll({ hostIds: [hostId] })
}
// GET /v1/sites
for await (const site of unifi.sites.iterate()) {
console.log(site.siteId, site.hostId, site.isOwner)
}
// GET /v1/isp-metrics/{type}
const metrics = await unifi.ispMetrics.get('5m', { duration: '24h' })
} catch (error) {
if (error instanceof UnifiApiError) {
console.error(error.status, error.code, error.message, error.traceId, error.retryAfter)
} else {
throw error
}
}Methods return the response's data directly. List methods return { data, nextToken } pages, iterate() is an async generator that follows pagination, and listAll() collects all pages. Non-2xx responses throw a UnifiApiError with the fields status, code, message, traceId, retryAfter, and body.
Available resources:
| Resource | Methods | Endpoint(s) |
| --- | --- | --- |
| hosts | list, iterate, listAll, get | GET /v1/hosts, GET /v1/hosts/{id} |
| sites | list, iterate, listAll | GET /v1/sites |
| devices | list, iterate, listAll | GET /v1/devices |
| ispMetrics | get, query | GET /v1/isp-metrics/{type}, POST /v1/isp-metrics/{type}/query |
| sdWan | list, get, status | GET /v1/sd-wan-configs, GET /v1/sd-wan-configs/{id}, GET /v1/sd-wan-configs/{id}/status |
| connector | get, post, put, patch, delete, request, forConsole, network, protect | * /v1/connector/consoles/{id}/{path} |
Full method reference (regenerated via bun run docs): docs/unifi-client.html.
🔌 Cloud connector
The connector proxies requests through api.ui.com to the local UniFi applications (Network, Protect, InnerSpace) running on a console. It requires console firmware ≥ 5.0.3 and is limited to 100 requests/minute per console, a 25 s timeout, and a 10 MB response body. The upstream response is passed through unchanged (it is not wrapped).
const hostId = '900A6F00301100000000074A6BA90000000007A3387E0000000063EC9853:123456789'
// GET /v1/connector/consoles/{hostId}/network/integration/v1/sites
const network = unifi.connector.network(hostId)
const sites = await network.get<{ data: { id: string; name: string }[] }>('/v1/sites')
// GET /v1/connector/consoles/{hostId}/protect/integration/v1/meta/info
const info = await unifi.connector.protect(hostId).get('/v1/meta/info')
// Any other path, any method
await unifi.connector.post(hostId, `network/integration/v1/sites/${siteId}/...`, { /* body */ })See the Network API docs at https://developer.ui.com/network and the Protect API docs at https://developer.ui.com/protect.
🧰 Generated SDK
The raw hey-api functions (listHosts, getHostById, listSites, listDevices, getIspMetrics, queryIspMetrics, listSdWanConfigs, getSdWanConfigById, getSdWanConfigStatus) are exported too. They return { data, error } unions. Configure the shared singleton:
import { client, listHosts } from '@serkonda7/unifi-api'
client.setConfig({ auth: process.env.UNIFI_API_KEY! })
const { data, error } = await listHosts({ query: { pageSize: '50' } })Note: the generated connector* functions cannot be used for real requests (the spec's wildcard path is not expanded) — use unifi.connector instead.
🛠️ Development
bun install— install dependenciesbun run generate— regeneratesrc/generatedfromopenapi/site-manager-1.0.0.jsonbun run check— type-check the projectbun run lint— lint/format with Biomebun test— run the test suitebun run build— build the packagebun run docs— generate the method reference
📜 License
The UniFi Site Manager API and its specification are provided by Ubiquiti Inc. under their terms.


