面向第三方企业客户 / 集成商,提供设备数据拉取、设备控制与事件回调推送能力。 For third-party enterprises and integrators: device data, device control, and event callback.
开放平台通过标准 REST API 提供以下能力:The Open Platform provides the following capabilities via REST API:
接口根地址:Base URL: https://www.aptfirst.com/openapi/v1
AppKey / AppSecret。Contact us to create a tenant and get AppKey / AppSecret.每个请求需携带以下请求头:Each request must carry the following headers:
| Header | 说明Description |
|---|---|
X-App-Key | AppKeyAppKey |
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.
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);
}
}
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");
}
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))
}
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));
}
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)));
}
}
统一返回结构:Unified response:
{ "code": 0, "message": "success", "data": { }, "requestId": "..." }
| code | 含义Meaning |
|---|---|
| 0 | 成功Success |
| 10001 | 签名错误Signature error |
| 10002 | 时间戳超时(±5 分钟)Timestamp expired (±5 min) |
| 10003 | nonce 重复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 |
以下接口均需携带签名头(见“鉴权说明”)。路径中的 {deviceNumber} 为设备编号;所有时间字段均为毫秒时间戳。All endpoints below require the signature headers (see Authentication). {deviceNumber} in paths is the device number; all time fields are Unix milliseconds.
/tenant/info
查询当前租户信息。Get current tenant info.
| 响应字段Field | 类型Type | 说明Description |
|---|---|---|
tenantCode | string | 租户编码Tenant code |
companyName | string | 企业名称Company name |
contactName | string | 联系人Contact |
contactPhone | string | 联系电话Phone |
contactEmail | string | 联系邮箱Email |
country | string | 国别/地区Country/region |
scene | string | 对接场景Integration scenario |
status | int | 1 启用Enabled / 2 禁用Disabled / 3 冻结Frozen / 4 过期Expired |
expireTime | long | 到期时间戳(毫秒),0 表示长期Expiry timestamp (ms); 0 means no expiry |
/devices?pageNo=1&pageSize=20
查询已授权设备列表(分页)。List authorized devices (paged).
| 参数Param | 位置In | 类型Type | 必填Required | 说明Description |
|---|---|---|---|---|
pageNo | query | int | 否 | 页码,默认 1Page number, default 1 |
pageSize | query | int | 否 | 每页条数,默认 20Page size, default 20 |
响应 data.list 元素字段:Items in data.list:
{
"deviceNumber": "G2024032847", "deviceName": "门磁A",
"deviceTypeId": "18", "deviceTypeName": "网关",
"online": true
}
/devices/{deviceNumber}/status
查询设备实时状态。Get device real-time status.
| 参数Param | 位置In | 类型Type | 必填Required | 说明Description |
|---|---|---|---|---|
deviceNumber | path | string | 是 | 设备编号Device number |
{
"deviceNumber": "G2024032847",
"online": true,
"status": { "lastReportAt": 1731300000000 },
"updatedAt": 1731300000000
}
/devices/{deviceNumber}/events?pageNo=1&pageSize=20
查询设备历史事件(分页)。Get device history events (paged).
| 参数Param | 位置In | 类型Type | 必填Required | 说明Description |
|---|---|---|---|---|
deviceNumber | path | string | 是 | 设备编号Device number |
pageNo | query | int | 否 | 页码,默认 1Page number, default 1 |
pageSize | query | int | 否 | 每页条数,默认 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)
/alarms?pageNo=1&pageSize=20&deviceNumber=xxx
查询授权设备的事件记录(所有类型,分页)。Get authorized devices' event records (all types, paged).
| 参数Param | 位置In | 类型Type | 必填Required | 说明Description |
|---|---|---|---|---|
pageNo | query | int | 否 | 页码,默认 1Page number, default 1 |
pageSize | query | int | 否 | 每页条数,默认 20Page size, default 20 |
deviceNumber | query | string | 否 | 按设备编号过滤,不传表示全部授权设备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
/devices/report
设备上行上报(HTTP 接入设备上报状态/事件)。Device uplink report (HTTP-connected devices report status/events).
| 字段Field | 类型Type | 必填Required | 说明Description |
|---|---|---|---|
deviceNumber | string | 是 | 设备编号Device number |
eventType | string | 否 | 事件类型(可选,平台依据 eventCode 自动推导,忽略传入值)Event type (optional; auto-derived from eventCode, the value is ignored):alarm/fault/arming_disarming/other |
eventCode | string | 是 | 平台事件码(数字字符串),如 426,须存在于下方事件码表,否则返回 10007Platform event code (numeric string), e.g. 426; must exist in the event code table below, otherwise 10007 is returned |
eventQualifier | int | 否 | 事件限定符Event qualifier:1触发/报警/布防/打开(默认)Trigger/Alarm/Arm/Open (default) / 3恢复/正常/撤防/关闭Restore/Normal/Disarm/Close |
partitionNumber | int | 否 | 防区号,默认 0Partition number, default 0 |
content | string | 否 | 事件明细(JSON 字符串/文本),写入事件内容 eventDetailEvent detail (JSON string/text), stored as eventDetail |
pushTime | long | 否 | 上报时间戳(毫秒),缺省为服务器时间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.
/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 |
|---|---|---|---|---|
deviceNumber | path | string | 是 | 设备编号Device number |
timeout | query | int | 否 | 等待秒数,默认 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).
/devices/{deviceNumber}/commands/{commandId}/result
设备上报命令执行回执。Device reports command execution result.
| 参数Param | 位置In | 类型Type | 必填Required | 说明Description |
|---|---|---|---|---|
deviceNumber | path | string | 是 | 设备编号Device number |
commandId | path | string | 是 | 命令 IDCommand ID |
status | body | string | 是 | 执行结果Result:success(成功)(Success) / failed(失败)(Failed) |
result | body | string | 否 | 执行结果明细(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": "..." }
/devices/{deviceNumber}/control
下发设备控制指令(写入命令队列,由设备长轮询拉取执行)。Issue a device control command (enqueued; the device picks it up via long-polling).
| 字段Field | 类型Type | 必填Required | 说明Description |
|---|---|---|---|
command | string | 是 | 指令码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)
|
params | object | 否 | 指令参数(JSON 对象)Command params (JSON object) |
POST /openapi/v1/devices/G2024032847/control
{ "command": "doorbell", "params": { "num": 2 } }
// 响应
{ "commandId": "73420bd8...", "status": "accepted" }
/control/{commandId}/result
查询控制结果回执。Get control result.
| 参数Param | 位置In | 类型Type | 必填Required | 说明Description |
|---|---|---|---|---|
commandId | path | string | 是 | 命令 IDCommand ID |
{
"commandId": "73420bd8...", "status": "success",
"result": { "ok": true }, "at": 1731300000000
}
status 可选值:status options:
pending(待执行)(Pending) /
success(成功)(Success) /
failed(失败)(Failed) /
timeout(超时)(Timeout)
上报接口 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 |
|---|---|---|
alarm | 100 | 医疗事件 |
alarm | 110 | 火警 |
alarm | 111 | 烟雾报警/正常 |
alarm | 113 | 水流警报/恢复 |
alarm | 120 | 遥控紧急按键 |
alarm | 123 | 警铃 |
alarm | 126 | 用户警铃 |
alarm | 134 | 警报/恢复 |
alarm | 140 | 红外探头警报/恢复 |
alarm | 151 | 气体探测器警报/恢复 |
fault | 301 | 电源故障/恢复 |
fault | 305 | 设备重启 |
fault | 311 | 电池低压/恢复 |
fault | 380 | 设备故障/恢复 |
arming_disarming | 403 | 自动布防/撤防 |
arming_disarming | 407 | 手机布防/撤防 |
arming_disarming | 409 | 布防/撤防 |
arming_disarming | 410 | 遥控器按键布防/撤防 |
other | 171 | 振动/停止 |
other | 426 | 门磁打开/关闭 |
other | 531 | 添加设备 |
other | 532 | 删除设备 |
other | 533 | 添加设备失败 |
other | 570 | 防区旁路 |
other | 600 | 门磁振动/停止 |
other | 700 | 网关升级 |
other | 800 | 设备初始化 |
other | 1000 | 设备离线 |
在平台配置回调地址后,设备实时数据与告警会以 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.
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.