跳到主要内容

接入说明

接入url

响应示例

{
"success": true,
"code": 0,
"data": {
"symbol": "BTC_USD",
"fairPrice": 8000,
"timestamp": 1587442022003
}
}

{
"success": false,
"code":500,
"message": "系统内部错误!"
}

相应API接受GET、POST或DELETE类型的请求,post请求的Content-Type为: application/json。

参数以json格式发送(参数命名规则为驼峰命名),get请求以requestParam形式发送(参数命名规则为'_'隔开)。

鉴权方式

java示例

/**
* 获取get请求参数字符串
*
* @param param get/delete请求参数map
* @return
*/
public static String getRequestParamString(Map<String, String> param) {
if (MapUtils.isEmpty(param)) {
return "";
}
StringBuilder sb = new StringBuilder(1024);
SortedMap<String, String> map = new TreeMap<>(param);
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = StringUtils.isBlank(entry.getValue()) ? "" : entry.getValue();
sb.append(key).append('=').append(urlEncode(value)).append('&');
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}

public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("UTF-8 encoding not supported!");
}
}

/**
* 签名
*/
public static String sign(SignVo signVo) {
if (signVo.getRequestParam() == null) {
signVo.setRequestParam("");
}
String str = signVo.getAccessKey() + signVo.getReqTime() + signVo.getRequestParam();
return actualSignature(str, signVo.getSecretKey());
}

public static String actualSignature(String inputStr, String key) {
Mac hmacSha256;
try {
hmacSha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secKey =
new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
hmacSha256.init(secKey);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("No such algorithm: " + e.getMessage());
} catch (InvalidKeyException e) {
throw new RuntimeException("Invalid key: " + e.getMessage());
}
byte[] hash = hmacSha256.doFinal(inputStr.getBytes(StandardCharsets.UTF_8));
return Hex.encodeHexString(hash);
}

@Getter
@Setter
public static class SignVo {
private String reqTime;
private String accessKey;
private String secretKey;
private String requestParam; //get请求参数根据字典顺序排序,使用&拼接字符串,post为json字符串
}
  1. 对于公共接口,不需要签名。

  2. 对于私有接口,需要在header中传入ApiKey、Request-Time、Signature、Content-Type 必须指定为application/json、Recv-Window(可选)参数,Signature为签名字符串,签名规则如下:

1)签名时需要先获得请求参数字符串,无参时为"":

对于GET/DELETE请求,按字典序拼接业务参数以&间隔,并最终获得签名目标串(在批量操作的API中,若参数值中有逗号等特殊符号,这些符号在签名时需要做URL encode)。

对于POST请求,签名参数为json字符串(无需进行字典排序)。

2)获得参数字符串后,再拼接签名目标串,规则为:accessKey+时间戳+获取到的参数字符串

3)使用HMAC SHA256算法对目标串进行签名,并最终将签名作为参数携带到header中

注意:

1)参与签名的业务参数为null时,不参与签名,对于path参数,也不参与签名;注意get请求将参数拼接至url上时,如果参数为null, 后台解析时,会解析成"",固当POST请求,某参数为null时,不要传该参数,或者签名时,将该参数的值设置为"",否则会出现验签失败。

2)请求时将签名时用到的Request-Time的值放入header的Request-Time参数中,获得的签名字符串放入header的Signature参数中,将APIKEY的Access Key放在header的ApiKey参数中,其余业务参数按正常传递即可。

3)获得的签名字符串不需要进行base64进行编码。

时间安全

所有签名接口均需要传入header参数Request-Time,即以毫秒表示的时间戳字符串,服务端接收到请求后会验证请求发出的时间 范围。若接受请求时,收到的req_time小于或大于服务端时间10秒(默认值)以上(该时间窗口值可以通过发送可选header参数 Recv-Window来自定义,其最大值为60,不推荐使用30秒以上的recv_window),则认为该请求无效

创建API key

用户可以在MEXC站点个人中心,创建API key,其包括两个部分,Access keyAPI的访问秘钥,Secret key对应的秘钥,用于签名计算及验证。

您可以点击 这里 创建API Key。

创建 API Key 时可以选择绑定 IP 地址,未绑定 IP 地址的 API Key 有效期为90天。(强烈建议绑定 IP 地址)

这两个密钥与账号安全紧密相关,无论何时都请勿向其它人透露。