API 验证
请求
所有请求基于Https协议,请求头信息中Content-Type 需要统一设置为:'application/json’
生成api key
在对任何请求进行签名之前,您必须通过 【交易所】-> 【个人中心】->【API管理】创建一个API key。 创建key后,您将获得3个必须记住的信息:
API Key
Secret Key
API Key 和 Secret Key将由随机生成和提供
产生签名
必要 Headers
| header | description |
|---|---|
| X-CH-APIKEY | api管理的key |
| X-CH-TS | 当下时间戳 |
| X-CH-SIGN | 经过签名规则产生的字串 |
Http Method: GET
组合当下时间戳、HTTP method、API path、query parameters
1736303665297GET/path?key1=value1&key2=value2字串经过 HMAC-SHA256 处理
CryptoJS.HmacSHA256('1736303665297GET/path?key1=value1&key2=value2', secretKey)将sign放入header
范例
api key = APIKEY
secret key = SECRETKEY
timestamp = 1736839890257
http method = GET
request path = /fapi/v1/position/positions?futuresName=E-BTC-USDT生成待签名的字符串
1736839957714GET/fapi/v1/position/positions?futuresName=E-BTC-USDT字串经过 HmacSHA256 处理
65f0f94af4997e240baa569c312b36dcffc72f59f22fb546cbd5296cc12223bb将sign放入header
Http Method: POST
组合当下时间戳、http method、api path、request body
1736840154334POST/fapi/v1/cancel{"key1":"value1","key2":"value2"}字串经过 HmacSHA256 处理
CryptoJS.HmacSHA256('1736840154334POST/fapi/v1/cancel{"key1":"value1","key2":"value2"}', secretKey)将sign放入header
范例
api key = APIKEY
secret key = SECRETKEY
timestamp = 1736840154334
http method = POST
request path = /fapi/v1/cancel
request body = {"futuresName":"E-ETH-USDT","orderId":4564516456}生成待签名的字符串
1736840154334POST/fapi/v1/cancel{"futuresName":"E-ETH-USDT","orderId":4564516456}字串经过 HmacSHA256 处理
4c89486597292040d2aa16176fa85414b55d61a01e2e00355b2aae2c86e32405将sign放入header
javascript 范例
javascript
const timestamp = new Date().getTime();
const method = 'POST';
const timeKey = "time";
const apiKeyKey = "api_key";
const apiKey = "APIKEY";
const secretKey = "SECRETKEY";
const path = "/path";
let hash = `${timestamp}${method}${path}`;
const queryString = "key1=value1&key2=value2";
if(queryString!=null && queryString.length>0) {
hash+=`?${queryString}`;
}
const body = '{"key1":"value1","key2":"value2"}';
if(body!=null) {
hash+=(typeof body === 'object')?JSON.stringify(body):body;
}
//need to includ the library of CryptoJS
let hashBytesArr=CryptoJS.HmacSHA256(hash, secretKey);
let sign=CryptoJS.enc.Hex.stringify(hashBytesArr);
//TODO add sign, timestamp, apiKey to header