开放平台 API 文档 Open Platform API Documentation

面向第三方企业客户 / 集成商,提供设备数据拉取、设备控制与事件回调推送能力。 For third-party enterprises and integrators: device data, device control, and event callback.

进入开放平台Enter Portal

一、概述1. Overview

开放平台通过标准 REST API 提供以下能力:The Open Platform provides the following capabilities via REST API:

  • 设备数据拉取:设备列表、实时状态、历史事件、告警/事件记录。Device data: device list, real-time status, history events, alarm/event records.
  • 设备控制:远程控制、参数修改、控制结果回执。Device control: remote control, parameter update, command result.
  • 事件回调:设备实时数据与告警通过 HTTPS 回调主动推送。Callback: real-time data and alarms pushed to your server over HTTPS.

接口根地址:Base URL: https://www.aptfirst.com/openapi/v1

二、接入流程2. Quick Start

  1. 联系商务开通租户,获取 AppKey / AppSecret。Contact us to create a tenant and get AppKey / AppSecret.
  2. 由平台为租户配置可访问/可控制的设备范围。We configure the accessible/controllable device scope for your tenant.
  3. 按下方鉴权规则对每个请求签名后调用接口。Sign every request according to the rules below and call the APIs.
  4. 如需被动接收事件,在平台配置回调地址。Configure a callback URL to receive events passively.

三、鉴权说明3. Authentication

每个请求需携带以下请求头:Each request must carry the following headers:

Header 说明Description
X-App-KeyAppKeyAppKey
X-Timestamp毫秒时间戳Unix timestamp in milliseconds
X-Nonce随机串,5 分钟内不重复Random string, unique within 5 minutes
X-Sign签名结果Signature

签名串(按顺序以换行拼接):Canonical string (joined by newline):

AppKey + "\n" + Timestamp + "\n" + Nonce + "\n" + METHOD + "\n" + path + "\n" + query + "\n" + body

签名算法:Signature:

X-Sign = base64( HMAC-SHA256( AppSecret, stringToSign ) )

Python 示例:Python example:

import base64, hashlib, hmac

app_key = "<AppKey>"
app_secret = "<AppSecret>"
timestamp = "1731300000000"
nonce = "abc123"
method = "GET"
path = "/openapi/v1/devices"
query = ""
body = ""

string_to_sign = "\n".join([app_key, timestamp, nonce, method, path, query, body])
sign = base64.b64encode(
    hmac.new(app_secret.encode(), string_to_sign.encode(), hashlib.sha256).digest()
).decode()
print(sign)

注意:签名串中的 query 为按参数名升序排序后的查询串(如 a=1&b=2),无查询参数时为空字符串;body 为请求体原文,GET 请求为空字符串。Note: query in the canonical string is the query sorted by parameter name (e.g. a=1&b=2), empty when absent; body is the raw request body, empty for GET.

Java

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class SignDemo {
    public static String sign(String appKey, String appSecret, String timestamp,
                              String nonce, String method, String path,
                              String query, String body) throws Exception {
        String stringToSign = appKey + "\n" + timestamp + "\n" + nonce + "\n"
                + method + "\n" + path + "\n" + query + "\n" + body;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] raw = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(raw);
    }
}

JavaScript (Node.js)

const crypto = require("crypto");

function sign(appKey, appSecret, timestamp, nonce, method, path, query, body) {
  const stringToSign = [appKey, timestamp, nonce, method, path, query, body].join("\n");
  return crypto.createHmac("sha256", appSecret).update(stringToSign, "utf8").digest("base64");
}

Go

package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "strings"
)

func sign(appKey, appSecret, timestamp, nonce, method, path, query, body string) string {
    stringToSign := strings.Join([]string{appKey, timestamp, nonce, method, path, query, body}, "\n")
    mac := hmac.New(sha256.New, []byte(appSecret))
    mac.Write([]byte(stringToSign))
    return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}

PHP

function sign($appKey, $appSecret, $timestamp, $nonce, $method, $path, $query, $body) {
    $stringToSign = implode("\n", array($appKey, $timestamp, $nonce, $method, $path, $query, $body));
    return base64_encode(hash_hmac("sha256", $stringToSign, $appSecret, true));
}

C# (.NET)

using System;
using System.Security.Cryptography;
using System.Text;

public static string Sign(string appKey, string appSecret, string timestamp,
                          string nonce, string method, string path,
                          string query, string body)
{
    var stringToSign = string.Join("\n", appKey, timestamp, nonce, method, path, query, body);
    using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(appSecret)))
    {
        return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
    }
}

四、统一返回与错误码4. Response & Error Codes

统一返回结构:Unified response:

{ "code": 0, "message": "success", "data": { }, "requestId": "..." }
code含义Meaning
0成功Success
10001签名错误Signature error
10002时间戳超时(±5 分钟)Timestamp expired (±5 min)
10003nonce 重复Duplicate nonce
10004密钥无效/禁用/过期Invalid/disabled/expired key
10005无权限No permission
10006调用频率超限Rate limited
10007参数错误Invalid parameter
10008设备离线Device offline
10009设备不存在或未授权Device not found / unauthorized
10010内部错误Internal error

五、接口列表5. Endpoints

以下接口均需携带签名头(见“鉴权说明”)。路径中的 {deviceNumber} 为设备编号;所有时间字段均为毫秒时间戳。All endpoints below require the signature headers (see Authentication). {deviceNumber} in paths is the device number; all time fields are Unix milliseconds.

GET /tenant/info

查询当前租户信息。Get current tenant info.

响应字段Field 类型Type 说明Description
tenantCodestring租户编码Tenant code
companyNamestring企业名称Company name
contactNamestring联系人Contact
contactPhonestring联系电话Phone
contactEmailstring联系邮箱Email
countrystring国别/地区Country/region
scenestring对接场景Integration scenario
statusint1 启用Enabled / 2 禁用Disabled / 3 冻结Frozen / 4 过期Expired
expireTimelong到期时间戳(毫秒),0 表示长期Expiry timestamp (ms); 0 means no expiry
GET /devices?pageNo=1&pageSize=20

查询已授权设备列表(分页)。List authorized devices (paged).

参数Param 位置In 类型Type 必填Required 说明Description
pageNoqueryint否页码,默认 1Page number, default 1
pageSizequeryint否每页条数,默认 20Page size, default 20

响应 data.list 元素字段:Items in data.list:

{
  "deviceNumber": "G2024032847", "deviceName": "门磁A",
  "deviceTypeId": "18", "deviceTypeName": "网关",
  "online": true
}
GET /devices/{deviceNumber}/status

查询设备实时状态。Get device real-time status.

参数Param 位置In 类型Type 必填Required 说明Description
deviceNumberpathstring是设备编号Device number
{
  "deviceNumber": "G2024032847",
  "online": true,
  "status": { "lastReportAt": 1731300000000 },
  "updatedAt": 1731300000000
}
GET /devices/{deviceNumber}/events?pageNo=1&pageSize=20

查询设备历史事件(分页)。Get device history events (paged).

参数Param 位置In 类型Type 必填Required 说明Description
deviceNumberpathstring是设备编号Device number
pageNoqueryint否页码,默认 1Page number, default 1
pageSizequeryint否每页条数,默认 20Page size, default 20
{
  "eventId": "5093", "deviceNumber": "G2024032847",
  "eventCode": "426", "eventType": "other",
  "eventTime": 1731300000000, "data": { }
}

eventType 可选值:eventType options: alarm(报警)(Alarm) / fault(故障)(Fault) / arming_disarming(布防/撤防)(Arming/Disarming) / other(其它)(Other)

GET /alarms?pageNo=1&pageSize=20&deviceNumber=xxx

查询授权设备的事件记录(所有类型,分页)。Get authorized devices' event records (all types, paged).

参数Param 位置In 类型Type 必填Required 说明Description
pageNoqueryint否页码,默认 1Page number, default 1
pageSizequeryint否每页条数,默认 20Page size, default 20
deviceNumberquerystring否按设备编号过滤,不传表示全部授权设备Filter by device number; omit for all authorized devices
{
  "alarmId": "5093", "deviceNumber": "G2024032847",
  "deviceName": "门磁A", "alarmType": "other",
  "eventCode": "426", "eventQualifier": 1, "partitionNumber": 0,
  "eventName": "门开启", "content": "{\"v\":1}",
  "alarmTime": 1731300000000, "handled": false

alarmType 可选值同 eventType:alarmType options same as eventType: alarm / fault / arming_disarming / other

POST /devices/report

设备上行上报(HTTP 接入设备上报状态/事件)。Device uplink report (HTTP-connected devices report status/events).

字段Field 类型Type 必填Required 说明Description
deviceNumberstring是设备编号Device number
eventTypestring否事件类型(可选,平台依据 eventCode 自动推导,忽略传入值)Event type (optional; auto-derived from eventCode, the value is ignored):alarm/fault/arming_disarming/other
eventCodestring是平台事件码(数字字符串),如 426,须存在于下方事件码表,否则返回 10007Platform event code (numeric string), e.g. 426; must exist in the event code table below, otherwise 10007 is returned
eventQualifierint否事件限定符Event qualifier:1触发/报警/布防/打开(默认)Trigger/Alarm/Arm/Open (default) / 3恢复/正常/撤防/关闭Restore/Normal/Disarm/Close
partitionNumberint否防区号,默认 0Partition number, default 0
contentstring否事件明细(JSON 字符串/文本),写入事件内容 eventDetailEvent detail (JSON string/text), stored as eventDetail
pushTimelong否上报时间戳(毫秒),缺省为服务器时间Report timestamp (ms); defaults to server time
POST /openapi/v1/devices/report
{ "deviceNumber": "G2024032847", "eventCode": "426",
  "eventQualifier": 1, "partitionNumber": 0, "content": "{\"v\":1}" }
// 响应
{ "code": 0, "message": "success", "data": true, "requestId": "..." }

限流:设备上报按租户套餐 rateLimitQps 限流(以 AppKey 计,该租户下所有设备共享配额),超限返回 10006。Rate limit: device report is limited by the tenant package rateLimitQps (counted per AppKey; all devices of the tenant share the quota), excess returns 10006.

GET /devices/{deviceNumber}/commands?timeout=20

设备长轮询拉取待执行命令;有命令立即返回,否则阻塞至 timeout 后返回空列表。Device long-polling to fetch pending commands; returns immediately when a command exists, otherwise blocks until timeout and returns an empty list.

参数Param 位置In 类型Type 必填Required 说明Description
deviceNumberpathstring是设备编号Device number
timeoutqueryint否等待秒数,默认 0(立即返回),最大 30Wait seconds, default 0 (immediate), max 30
{
  "commandId": "73420bd8...", "commandCode": "doorbell",
  "params": "{\"num\":2}", "createTime": 1731300000000
}

commandCode 可选值同控制接口的 command(见下)。commandCode options are the same as command in the control API (below).

POST /devices/{deviceNumber}/commands/{commandId}/result

设备上报命令执行回执。Device reports command execution result.

参数Param 位置In 类型Type 必填Required 说明Description
deviceNumberpathstring是设备编号Device number
commandIdpathstring是命令 IDCommand ID
statusbodystring是执行结果Result:success(成功)(Success) / failed(失败)(Failed)
resultbodystring否执行结果明细(JSON 字符串)Result detail (JSON string)
POST /openapi/v1/devices/G2024032847/commands/73420bd8.../result
{ "status": "success", "result": "{\"ok\":true}" }
// 响应
{ "code": 0, "message": "success", "data": true, "requestId": "..." }
POST /devices/{deviceNumber}/control

下发设备控制指令(写入命令队列,由设备长轮询拉取执行)。Issue a device control command (enqueued; the device picks it up via long-polling).

字段Field 类型Type 必填Required 说明Description
commandstring是指令码Command code(可选值options): arm(布防)(Arm) / disarm(撤防)(Disarm) / alarm_bell(打开警铃)(Open alarm bell) / alarm_bell_close(关闭警铃)(Close alarm bell) / doorbell(打开门铃)(Open doorbell) / doorbell_close(关闭门铃)(Close doorbell)
paramsobject否指令参数(JSON 对象)Command params (JSON object)
POST /openapi/v1/devices/G2024032847/control
{ "command": "doorbell", "params": { "num": 2 } }
// 响应
{ "commandId": "73420bd8...", "status": "accepted" }
GET /control/{commandId}/result

查询控制结果回执。Get control result.

参数Param 位置In 类型Type 必填Required 说明Description
commandIdpathstring是命令 IDCommand ID
{
  "commandId": "73420bd8...", "status": "success",
  "result": { "ok": true }, "at": 1731300000000
}

status 可选值:status options: pending(待执行)(Pending) / success(成功)(Success) / failed(失败)(Failed) / timeout(超时)(Timeout)

事件码表Event Code Table

上报接口 eventCode 使用下表数值;eventType 由平台依据事件码自动推导,传入值会被忽略。Use the numeric eventCode below in the report API; eventType is auto-derived from the code, the passed value is ignored.

类型Type 事件码Code 名称Name
alarm100医疗事件
alarm110火警
alarm111烟雾报警/正常
alarm113水流警报/恢复
alarm120遥控紧急按键
alarm123警铃
alarm126用户警铃
alarm134警报/恢复
alarm140红外探头警报/恢复
alarm151气体探测器警报/恢复
fault301电源故障/恢复
fault305设备重启
fault311电池低压/恢复
fault380设备故障/恢复
arming_disarming403自动布防/撤防
arming_disarming407手机布防/撤防
arming_disarming409布防/撤防
arming_disarming410遥控器按键布防/撤防
other171振动/停止
other426门磁打开/关闭
other531添加设备
other532删除设备
other533添加设备失败
other570防区旁路
other600门磁振动/停止
other700网关升级
other800设备初始化
other1000设备离线

六、回调推送6. Callback

在平台配置回调地址后,设备实时数据与告警会以 HTTPS POST 推送到该地址,并携带与请求相同的签名头(X-App-Key / X-Timestamp / X-Nonce / X-Sign)。After configuring a callback URL, real-time data and alarms are POSTed to it over HTTPS, carrying the same signature headers (X-App-Key / X-Timestamp / X-Nonce / X-Sign).

可选订阅事件类型(不订阅表示全部):Subscribable event types (subscribing to none means all):

取值Value 说明Description
alarm报警Alarm
fault故障Fault
arming_disarming布防/撤防Arming/Disarming
other其它Other

推送体示例:Payload example:

{
  "eventId": "...",
  "eventType": "alarm",
  "deviceNumber": "G2024032847",
  "data": { "eventCode": "900", "eventType": "alarm", "content": "...", "pushTime": 1731300000000 },
  "ts": 1731300000000
}

失败重试 3 次(指数退避),同一 eventId 不重复推送。Failed pushes are retried up to 3 times (exponential backoff); the same eventId is not delivered twice.

七、常见问题7. FAQ

Q: 报 10001 签名错误?Q: 10001 signature error?

A: 请核对签名串顺序、path(不含域名与 query)、body 原文,以及 AppSecret 是否正确。A: Verify the canonical string order, path (no domain/query), raw body, and AppSecret.

Q: 报 10006 频率超限?Q: 10006 rate limited?

A: 超出该密钥的秒/分/时限流阈值,请降低调用频率或联系商务调高配额。A: You exceeded the per-key per-second/minute/hour limit; slow down or contact us to raise the quota.

Q: 报 10009 设备不存在或未授权?Q: 10009 device not found / unauthorized?

A: 该设备未授权给您的租户,请联系平台为您的租户配置设备授权范围。A: The device is not authorized for your tenant; contact us to configure the device scope.