API 路径、字段名、事件名和代码示例保持英文,与接口完全一致,可直接复制使用。
身份验证
每个发往 /v1/* 的请求都要用您的 API 密钥(secret)做 HMAC-SHA256 签名。密钥只能保存在您的服务器上。
在商户后台的开发者页面自行创建 API 密钥(需要 2FA):您会得到密钥 ID(key id)和密钥(secret)。如果密钥丢失,所有者可以在同一页面通过 2FA 再次查看;每次查看都会记录,并通知全部所有者。
必需的请求头
| 请求头 | 值 |
|---|---|
X-Key-Id | 您的 API 密钥 ID,例如 ak_3f9c… |
X-Timestamp | 当前 Unix 时间,单位为毫秒(13 位数字) |
X-Signature | 对待签名字符串做 HMAC-SHA256,结果用十六进制编码(64 个字符) |
Content-Type | 请求带 body 时为 application/json |
待签名字符串
用换行符(\n)连接以下四个值:
{X-Timestamp}
{METHOD}
{path including query string}
{raw request body}- METHOD为大写:
GET或POST。 - Path与您实际发送的完全一致,包括查询字符串,例如
/v1/quote?chain=tron&token=USDT&direction=in&amount=50000000。 - Body是您实际发送的 JSON 原始字节。
GET请求使用空字符串(此时待签名字符串以换行符结尾)。 - Key是发放给您的 API 密钥(secret)原文,按 UTF-8 文本使用。不要做 base64 解码。
签名示例
import crypto from "node:crypto"; const BASE = process.env.ICN_BASE_URL; // given to you at onboarding const KEY_ID = process.env.ICN_KEY_ID; // ak_... const SECRET = process.env.ICN_SECRET; // use the string exactly as issued export async function icn(method, path, body) { const raw = body === undefined ? "" : JSON.stringify(body); const ts = Date.now().toString(); // milliseconds const signature = crypto .createHmac("sha256", SECRET) .update(`${ts}\n${method}\n${path}\n${raw}`) .digest("hex"); const res = await fetch(BASE + path, { method, headers: { "X-Key-Id": KEY_ID, "X-Timestamp": ts, "X-Signature": signature, ...(raw && { "Content-Type": "application/json" }), }, body: raw || undefined, // send exactly the bytes you signed }); return { status: res.status, body: await res.json() }; }
import hashlib, hmac, json, os, time import requests BASE = os.environ["ICN_BASE_URL"] KEY_ID = os.environ["ICN_KEY_ID"] # ak_... SECRET = os.environ["ICN_SECRET"] # use the string exactly as issued def icn(method, path, body=None): raw = "" if body is None else json.dumps(body, separators=(",", ":")) ts = str(int(time.time() * 1000)) # milliseconds msg = f"{ts}\n{method}\n{path}\n{raw}".encode() signature = hmac.new(SECRET.encode(), msg, hashlib.sha256).hexdigest() headers = {"X-Key-Id": KEY_ID, "X-Timestamp": ts, "X-Signature": signature} if raw: headers["Content-Type"] = "application/json" res = requests.request(method, BASE + path, data=raw.encode() or None, headers=headers) return res.status_code, res.json()
# Sign: timestamp \n METHOD \n path(+query) \n body TS=$(date +%s%3N) BODY='{"chain":"tron","player_ref":"player_1024"}' SIG=$(printf '%s\n%s\n%s\n%s' "$TS" POST /v1/addresses "$BODY" \ | openssl dgst -sha256 -hmac "$ICN_SECRET" -hex | sed 's/^.* //') curl -X POST "$ICN_BASE_URL/v1/addresses" \ -H "X-Key-Id: $ICN_KEY_ID" \ -H "X-Timestamp: $TS" \ -H "X-Signature: $SIG" \ -H "Content-Type: application/json" \ -d "$BODY"
规则
- 时间戳与我们的时钟相差不得超过 5 分钟。请让您的服务器保持 NTP 同步。
- 每个签名只能使用一次。重试时,请用新的时间戳重新签名。
- 只接受来自为该密钥登记的 IP 地址(CIDR)的请求。
- 请求 body 最大 64 KB。
所有身份验证失败都返回相同的响应,不会透露是哪一项检查未通过:
401 Unauthorized
{
"error": { "code": "unauthorized", "message": "authentication failed" }
}如果收到 401,请按以下顺序检查:时钟偏差、签名时使用的 path 和 body 是否与发送的完全一致、密钥、您服务器的出口 IP。