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。