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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tp-cloud-sdk

v1.0.3

Published

A Node.js SDK for TP-LINK Commercial Cloud Platform with signature authentication

Readme

TP-LINK 商用云 SDK

Node.js SDK for TP-LINK Commercial Cloud Platform with HMAC-SHA256 signature authentication

Node.js Version License

简介

TP-LINK 商用云 SDK(tp-cloud-sdk)是一个用于与 TP-LINK 商用云平台进行交互的 Node.js SDK。它提供了完整的 API 封装,包括设备管理、预览回放、HTTP-FLV 拉流、消息订阅和云端录制等功能,并内置 HMAC-SHA256 请求签名认证。

功能特性

  • 设备管理 - 获取设备详细信息、获取项目型应用下设备列表
  • 预览回放 - 搜索当天录像数据、获取预览缩略图
  • HTTP-FLV 拉流 - 获取 HTTP-FLV 取流地址、获取某地址最后一次取流关闭的原因
  • 消息订阅 - 添加/查询应用维度的消息订阅配置,设置/查询报警消息推送签名密钥
  • 云端录制 - 提交设备抓图/录像任务,分页查询任务与文件,删除云录制文件
  • 签名认证 - 基于 HMAC-SHA256 的请求签名,自动生成 X-Authorization 请求头
  • 统一配置 - 支持通过带 TP_LINK_ 前缀的环境变量或编程方式配置

安装

npm install tp-cloud-sdk

快速开始

1. 环境变量配置

在项目根目录创建 .env 文件:

TP_LINK_ACCESS_KEY=your_access_key
TP_LINK_SECRET_KEY=your_secret_key
TP_LINK_API_HOST=api-smbcloud.tp-link.com.cn
TP_LINK_TERMINAL_ID=your_terminal_id

| 变量名 | 说明 | 必填 | |---|---|---| | TP_LINK_ACCESS_KEY | 开放平台 Access Key | 是 | | TP_LINK_SECRET_KEY | 开放平台 Secret Key | 是 | | TP_LINK_API_HOST | 商云平台服务地址,默认 api-smbcloud.tp-link.com.cn | 否 | | TP_LINK_TERMINAL_ID | 终端标识,可用 UUID 生成 | 否 | | TP_LINK_TIMEOUT | 请求超时时间(毫秒),默认 5000 | 否 |

2. 基础使用

const { TpCloudSDK } = require('tp-cloud-sdk');

async function main() {
  try {
    // 创建 SDK 实例(自动从 .env 加载 TP_LINK_* 配置)
    const sdk = new TpCloudSDK();

    console.log('正在获取项目型应用下设备列表...');
    const deviceList = await sdk.deviceManager.getDeviceListInProjectApplication({
      start: 0,
      limit: 20
    });
    console.log('设备列表:', JSON.stringify(deviceList, null, 2));

    console.log('正在获取设备详情...');
    const deviceDetails = await sdk.deviceManager.getDeviceDetails({
      qrCode: '16E0091709B04C926'  // mac 与 qrCode 至少存在一个
    });
    console.log('设备详情:', JSON.stringify(deviceDetails, null, 2));
  } catch (error) {
    console.error('发生错误:', error.message);
  }
}

main();

3. 手动配置

const { TpCloudSDK } = require('tp-cloud-sdk');

// 显式传入配置(无需 .env 文件)
const sdk = new TpCloudSDK({
  ACCESS_KEY: 'your_access_key',
  SECRET_KEY: 'your_secret_key',
  API_HOST: 'api-smbcloud.tp-link.com.cn',
  TERMINAL_ID: 'your_terminal_id',
  TIMEOUT: 5000
});

API 模块

设备管理 (sdk.deviceManager)

// 获取设备详细信息(mac 与 qrCode 至少存在一个,同时存在时以 qrCode 为准)
const deviceDetails = await sdk.deviceManager.getDeviceDetails({
  qrCode: '16E0091709B04C926',
  channelId: 1        // 可选,子设备通道编号
});
console.log('设备详情:', JSON.stringify(deviceDetails.result, null, 2));

// 获取项目型应用下设备列表(start、limit 必填)
const deviceList = await sdk.deviceManager.getDeviceListInProjectApplication({
  start: 0,    // 起始条目
  limit: 20    // 获取条目总数
});
console.log('设备列表:', JSON.stringify(deviceList.result, null, 2));

预览回放 (sdk.previewPlayback)

// 搜索当天录像数据
const videoData = await sdk.previewPlayback.searchVideo({
  qrCode: '16E0091709B04C926',
  channelId: 1,
  searchDay: '20240320',   // 搜索日期,yyyyMMDD 格式
  startIdx: 0,             // 录像事件开始索引
  endIdx: 99               // 录像事件结束索引,与 startIdx 差值不能超过 100
});
console.log('录像数据:', JSON.stringify(videoData.result, null, 2));

// 获取预览缩略图
const thumbnail = await sdk.previewPlayback.captureThumbnail({
  qrCode: '16E0091709B04C926',  // 与 mac 至少存在一个,同时存在时以 qrCode 为准
  channelId: 1
});
console.log('缩略图地址:', thumbnail.result.url);
// url 为抓图后的图片路径,正常企业版服务图片有效期为 1h

HTTP-FLV 拉流 (sdk.httpFlv)

// 获取 HTTP-FLV 取流地址
const flvUrl = await sdk.httpFlv.getHttpFlvUrl({
  qrCode: '16E0091709B04C926',
  channelId: 1,
  type: 'video',        // 流类型:video(预览)/ sdvod(回放)
  resolution: 'HD',     // 清晰度:VGA(流畅)/ HD(高清),预览时必选
  videoCode: 'H264'     // 取流视频编码:H264 / H265
});
console.log('取流地址:', flvUrl.result.url);
// 取流地址有效期为 30 分钟,一个地址同时只能拉一路 FLV 流

// 获取某地址最后一次取流关闭的原因
const closeReason = await sdk.httpFlv.getHttpFlvCloseReason({
  url: 'https://xxx/vms/open/httpFlvServer/v1/getHttpFlvStream.flv?token=xxx'
});
console.log('关闭原因:', closeReason.result.closeReason);

消息订阅 (sdk.messageSubscription)

// 添加或者更新应用维度的消息订阅配置
await sdk.messageSubscription.setAppMsgPushConfig({
  serverUrl: 'http://your-server:8002/msg',  // 接收设备报警消息的 URL
  openMsgTransport: 1,                       // 订阅消息开关:0 关闭,1 开启
  msgContentType: ['ipcDeviceOnline', 'ipcDeviceOffline']  // 接收的消息类型,空数组表示全部
});

// 查询应用的消息订阅配置
const pushConfig = await sdk.messageSubscription.getAppMsgPushConfig();

// 设置应用报警消息推送签名密钥
await sdk.messageSubscription.setAppMsgPushSk({ sk: 'your_sign_key' });

// 查询应用报警消息推送签名密钥
const pushSk = await sdk.messageSubscription.getAppMsgPushSk();

云端录制 (sdk.cloudRecording)

// 提交设备抓图任务
const imageTask = await sdk.cloudRecording.submitCaptureImageTask({
  qrCode: '16E0091709B04C926',
  channelId: 1,
  type: 1                    // 抓图类型:1 实时抓图,2 回放抓图,3 报警消息抓图
});
console.log('任务ID:', imageTask.result.taskId);

// 提交设备录像任务
const videoTask = await sdk.cloudRecording.submitCaptureVideoTask({
  qrCode: '16E0091709B04C926',
  channelId: 1,
  type: 102,                              // 录像类型:102 回放录像
  playbackStartTime: '20230509093100',    // yyyyMMddhhmmss,东8区时间
  playbackEndTime: '20230509093200'
});

// 分页查询任务列表
const taskPage = await sdk.cloudRecording.getTaskPage({
  pageIndex: 1,
  pageSize: 10
});

// 查询任务详情
const taskInfo = await sdk.cloudRecording.getTaskInfo({ taskId: 'task-id' });

// 分页查询任务文件列表
const filePage = await sdk.cloudRecording.getTaskFilePage({
  taskId: 'task-id',
  pageIndex: 1,
  pageSize: 10,
  urlRequired: true          // 是否需返回文件下载链接
});

// 删除云录制文件
await sdk.cloudRecording.deleteFile({ fileId: 'file-id' });

错误处理

SDK 在请求失败或参数缺失时抛出 Error,可通过 message 前缀区分错误类型:

try {
  await sdk.deviceManager.getDeviceDetails({ qrCode: 'xxx' });
} catch (error) {
  if (error.message.startsWith('API Error:')) {
    // 服务端返回非 2xx 状态码,message 中包含状态码与响应体
    console.error('API 错误:', error.message);
  } else if (error.message.startsWith('Network Error:')) {
    console.error('网络错误:', error.message);
  } else {
    console.error('请求错误:', error.message);
  }
}

演示工程

demo/ 目录提供了一个完整可运行的演示工程:

cd demo
npm install
npm test

开发

安装依赖

npm install

运行单元测试

npm test

代码检查

npm run lint

相关文档

系统要求

  • Node.js >= 16

许可证

MIT