更新日志
| 版本 | 时间 | 内容 |
|---|---|---|
| V1.0.0 | 2022-04-26 | 初始版本 |
介绍
用户手册
在Matrixport我们提供数字资产理财、借贷服务。 有两种方式可以调用我们的API接口:Rest 和 Websocket。
- Rest 接口
Rest 接口分为公共接口和私有接口,公有接口无需鉴权可直接调用。私有接口,要求进行API私钥签名鉴权。请先在Matrixport网站的API页面申请私钥,设置权限后启动API开发。 私有接口权限:钱包、读取 各接口的类型及权限可通过API目录查询。 - Websocket 接口
Websocket接口是基于Websocket协议。用户需要先建立Websocket连接,然后发送请求订阅频道,随后bit.com会主动向用户推送数据。如果不想接收消息可取消订阅或者断开连接。 订阅频道分为公有频道和私有频道,公有频道无需鉴权可直接订阅。私有频道需要申请token且通过鉴权后订阅。 各频道的类型和权限可通过频道目录查询。
测试环境访问地址
REST API 地址:https://betaapi.bitexch.dev
WS API 地址: wss://betaws.bitexch.dev
生产环境访问地址
REST API 地址:https://api.bit.com
WS API 地址:wss://ws.bit.com
访问限制
为了保证系统运行效率,Matrixport实施API访问限流措施。公有接口按IP进行频率限制,私有接口按UID进行频率限制。当请求频率超限时,会返加“429 too many requests” 提示。
- Rest API 限制:
私有接口(钱包接口):每个UID 1次/秒;
鉴权
私有接口必填字段
- 用户必须把私钥key放在请求头部,如X-MatrixPort-Access-Key
- 用户必须在请求参数(GET请求: query string, POST请求:JSON Body)中添加timestamp字段(单位为毫秒),API 服务会检查时间戳,如果abs(server_timestamp - request_timestamp) > 5000 请求会被拒绝。
- timestamp字段 必须为整型,而非字符串类型。
- 用户必须在请求参数中添加signature字段(GET请求: query string, POST请求:JSON Body)
- 对POST请求,请求头Content-Type需设为 application/json
如果鉴权失败,会返回错误码412“AkID is valid”。
签名算法
#########
# Python code to calc BIT.COM API signature
#########
import hashlib
import hmac
def encode_list(self, item_list):
list_val = []
for item in item_list:
obj_val = self.encode_object(item)
list_val.append(obj_val)
sorted_list = sorted(list_val)
output = '&'.join(sorted_list)
output = '[' + output + ']'
return output
def encode_object(self, param_map):
sorted_keys = sorted(param_map.keys())
ret_list = []
for key in sorted_keys:
val = param_map[key]
if isinstance(val, list):
list_val = self.encode_list(val)
ret_list.append(f'{key}={list_val}')
elif isinstance(val, dict):
# call encode_object recursively
dict_val = self.encode_object(val)
ret_list.append(f'{key}={dict_val}')
elif isinstance(val, bool):
bool_val = str(val).lower()
ret_list.append(f'{key}={bool_val}')
else:
general_val = str(val)
ret_list.append(f'{key}={general_val}')
sorted_list = sorted(ret_list)
output = '&'.join(sorted_list)
return output
def get_signature(self, http_method, api_path, param_map):
str_to_sign = api_path + '&' + self.encode_object(param_map)
print('str_to_sign = ' + str_to_sign)
sig = hmac.new(self.secret_key.encode('utf-8'), str_to_sign.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
return sig
#########
# END
#########
- 请求参数:POST为JSON,其余部分为查询字符串
- 对签名进行编码,对于简单的json对象,请按字母顺序对参数进行排序,并把他们用“&”连接,如'param1=value1¶m2=value2', then get str_to_sign = api_path + '&' + 'param1=value1¶m2=value2'
- 对嵌套数组对象,对每个对象进行编码,并按字母顺序进行排序,使用“&”符号连接,并用[ ]括起来,如 str_to_sign = api_path + '&' + 'param1=value1&array_key1=[array_item1&array_item2]', 参见下面的例子
- 签名使用哈希算法,hex(hmac_sha256(str_to_sign, secret_key))
- 在请求参数中添加签名字段:对查询字符串,添加“&signature=YOUR_SIGNATURE”, 对JOSN请求体, 添加 {'signature':YOUR_SIGNATURE}
GET 请求示例:
*Secret Key: eabc3108-dd2b-43df-a98d-3e2054049b73
*HTTP method: GET
*API Path: /v1/margins
*Query string: price=8000&qty=30&instrument_id=BTC-PERPETUAL×tamp=1588242614000
*得到 str_to_sign = /v1/margins&instrument_id=BTC-PERPETUAL&price=8000&qty=30×tamp=1588242614000
> echo -n "/v1/margins&instrument_id=BTC-PERPETUAL&price=8000&qty=30×tamp=1588242614000" | openssl dgst -sha256 -hmac "eabc3108-dd2b-43df-a98d-3e2054049b73"
> e3be96fdd18b5178b30711e16d13db406e0bfba089f418cf5a2cdef94f4fb57d
sig = hex(hmac_sha256(str_to_sign, secret_key)) = e3be96fdd18b5178b30711e16d13db406e0bfba089f418cf5a2cdef94f4fb57d
*最后JSON请求体为: { "instrument_id": "BTC-27MAR20-9000-C", "order_type": "limit", "price": "0.021", "qty": "3.14", "side": "buy", "time_in_force": "gtc", "stop_price": "", "stop_price_trigger": "", "auto_price": "", "auto_price_type": "", "timestamp": 1588242614000, "signature": "e3be96fdd18b5178b30711e16d13db406e0bfba089f418cf5a2cdef94f4fb57d" }
POST 请求示例:
Secret Key: eabc3108-dd2b-43df-a98d-3e2054049b73
HTTP method: POST
API Path: /v1/orders
JSON body:
{
"instrument_id": "BTC-27MAR20-9000-C",
"order_type": "limit",
"price": "0.021",
"qty": "3.14",
"side": "buy",
"time_in_force": "gtc",
"stop_price": "",
"stop_price_trigger": "",
"auto_price": "",
"auto_price_type": "",
"timestamp": 1588242614000
}
得到 str_to_sign = /v1/orders&auto_price=&auto_price_type=&instrument_id=BTC-27MAR20-9000-C&order_type=limit&price=0.021&qty=3.14&side=buy&stop_price=&stop_price_trigger=&time_in_force=gtc×tamp=1588242614000
> echo -n "/v1/orders&auto_price=&auto_price_type=&instrument_id=BTC-27MAR20-9000-C&order_type=limit&price=0.021&qty=3.14&side=buy&stop_price=&stop_price_trigger=&time_in_force=gtc×tamp=1588242614000" | openssl dgst -sha256 -hmac "eabc3108-dd2b-43df-a98d-3e2054049b73"
> 34d9afa68830a4b09c275f405d8833cd1c3af3e94a9572da75f7a563af1ca817
sig = hex(hmac_sha256(str_to_sign, secret_key)) = 34d9afa68830a4b09c275f405d8833cd1c3af3e94a9572da75f7a563af1ca817
最后JSON请求体为:
{
"instrument_id": "BTC-27MAR20-9000-C",
"order_type": "limit",
"price": "0.021",
"qty": "3.14",
"side": "buy",
"time_in_force": "gtc",
"stop_price": "",
"stop_price_trigger": "",
"auto_price": "",
"auto_price_type": "",
"timestamp": 1588242614000,
"signature": "34d9afa68830a4b09c275f405d8833cd1c3af3e94a9572da75f7a563af1ca817"
}
POST 请求带 boolean 字段
例如以POST /v1/orders 为例 (post_only 字段),
在JSON BODY, true/false必须为小写,不加双引号 {"post_only": true}
在Query string: post_only=true
例子
request
curl -X POST "https://betaapi.bitexch.dev/v1/orders" -H "Content-Type: application/json" -H "X-MatrixPort-Access-Key: ak-df074cbc-dbf7-46f9-b07c-f4f51763ac7a" -d '{"instrument_id": "BTC-26JUN20-3500-P", "price": "15", "qty": "1", "side": "sell", "time_in_force": "gtc", "order_type": "limit", "post_only": true, "timestamp": 1592587664652, "signature": "cf2d8fe95b71764056a4f707e2388ce84a82ed2915cbe92b58f37c26ea0eda97"}'
得到 string to sign
- str_to_sign = /v1/orders&instrument_id=BTC-26JUN20-3500-P&order_type=limit&post_only=true&price=15&qty=1&side=sell&time_in_force=gtc×tamp=1592587664652
POST 请求带 array 字段
- 逻辑
for item in object_array:
str_list.add(encode(item))
str_list.sorted()
str_to_sign = '&'.join(str_list)
以 POST /v1/blocktrades 为例:
- In json body, use following format {"trades": [{"instrument_id": "BTC-25SEP20-9000-C", "price": "0.21", "qty": "50", "side": "sell"}, {"instrument_id": "BTC-PERPETUAL", "price": "9000", "qty": "500000", "side": "buy"}]}
私钥是 eabc3108-dd2b-43df-a98d-3e2054049b73
例子
request
curl -X POST "https://betaapi.bitexch.dev/v1/trades" -H "Content-Type: application/json" -H "X-MatrixPort-Access-Key: ak-df074cbc-dbf7-46f9-b07c-f4f51763ac7a" -d '{"label": "A0627-1", "role": "taker", "trades": [{"instrument_id": "BTC-25SEP20-9000-C", "price": "0.21", "qty": "50", "side": "sell"}, {"instrument_id": "BTC-PERPETUAL", "price": "9000", "qty": "500000", "side": "buy"}], "timestamp": 1593239722621, "signature": "9636f1850e33557c03a499bb5c1aed9a36be340f3dbfd22a3f066438b3987d6b"}'
得到 string to sign
- str_to_sign = /v1/trades&label=A0627-1&role=taker×tamp=1593239722621&trades=[instrument_id=BTC-25SEP20-9000-C&price=0.21&qty=50&side=sell&instrument_id=BTC-PERPETUAL&price=9000&qty=500000&side=buy]
接口目录
- 用户自定义字符串(label等)合法字符: [A-Z], [a-z], [0-9], "-", "_"
| Path | Method | Description | Scope | Rate Limit Type | Permission |
|---|---|---|---|---|---|
| /mapi/v1/wallet/withdraw | POST | 提现 | private | 钱包 | 钱包 |
| /mapi/v1/wallet/withdrawals | GET | 提现记录 | private | 查询 | 钱包 |
| /mapi/v1/wallet/deposits | POST | 充值记录 | private | 查询 | 钱包 |
| /mapi/v1/wallet/balance | POST | 查询余额 | private | 查询 | 钱包 |
钱包
提现
POST /mapi/v1/wallet/withdraw
curl -X POST "https://api.matrixport.com/mapi/v1/wallet/withdraw" -H "Content-Type: application/json" -H "X-MatrixPort-Access-Key: Your Access Key" -d '{"currency": "BTC", "address": "Your address", "amount": "1.2", "pwd": "Your password", "timestamp": 1589523989378, "signature": "signature"}'
返回数据
{
"code": 0,
"message": "",
"data": {
"withdraw_id": "b61c2b93-8a25-44d4-9715-023cce61dc50"
}
}
通过此接口,可从matrixport余额账户进行提现。
提现地址首先要Matrixport App提现页面 设置为白名单地址。
资金密码pwd字段需要通过base64(sha256(pwd))进行编码。例如,假设密码是123456,编码后的密码为“jZae727K08KaOmKSgOaGzww/XVqGr/PKEgIMkjrcbJI=”
请求参数
| 字段名称 | 数据类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
| currency | string | true | "" | 币种,如BTC |
| address | string | true | "" | 提现地址 |
| amount | string | true | "" | 提现金额 |
| pwd | string | true | "" | 资金密码 |
| chain | string | false | "" | 链名 提现USDT时,要提到USDTERC应填ETH,要提到USDTTRE应填TRX |
| tag | string | false | "" |
返回数据
| 字段名称 | 数据类型 | 说明 |
|---|---|---|
| withdraw_id | string | 提现订单ID, 可用于后续的查询 |
提现记录
GET /mapi/v1/wallet/withdrawals
curl -H "X-MatrixPort-Access-Key: Your Access Key" "https://api.matrixport.com/mapi/v1/wallet/withdrawals?currency=BTC&limit=10&offset=0×tamp=1589522687689&signature=signature"
返回数据
{
"code": 0,
"data": {
"items": [{
"address": "mfaFpdVCb6UFS5AXUhC8VGXgj9dnJ37nLP",
"amount": "0.001",
"code": 0,
"confirmations": 0,
"currency": "BTC",
"fee": "0.00001",
"state": "confirmed",
"transaction_id": "52e1537002f51acbf5f52b9dfeab6a9e7cc185a669cda2573e768420b0839523",
"created_at": 1608606000000,
"updated_at": 1608606000000,
"is_onchain": true
}, {
"address": "mfaFpdVCb6UFS5AXUhC8VGXgj9dnJ37nLP",
"amount": "0.11",
"code": 13100100,
"confirmations": 0,
"currency": "BTC",
"fee": "0.00001",
"state": "rejected",
"transaction_id": "",
"created_at": 1608606000000,
"updated_at": 1608606000000,
"is_onchain": false
}]
}
}
查询余额账户的提现记录。
请求参数
| 字段名称 | 数据类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
| currency | String | true | "" | 币种名称 |
| limit | int | false | 10 | 分页大小,最大50 |
| offset | int | false | 1 | 分页偏移 |
返回数据
| 字段名称 | 数据类型 | 说明 |
|---|---|---|
| code | int | 错误码,0代表正常,其他值代表失败 |
| state | string | 提现状态 pending/confirmed/rejected/failed |
| address | string | 提现目标地址 |
| amount | string | 提现金额 |
| confirmations | int | 确认数,如果是内部地址,由于不会上链,一直为0 |
| currency | string | 币种 |
| fee | string | 提现手续费 |
| transaction_id | string | 链上交易哈希 |
| created_at | int | 订单创建时间戳 |
| updated_at | int | 订单更新时间戳 |
| is_onchain | bool | 订单是否上链(内部地址提现不上链) |
充值记录
GET /mapi/v1/wallet/deposits
curl -H "X-MatrixPort-Access-Key: Your Access Key" "https://api.matrixport.com/mapi/v1/wallet/deposits?currency=BTC&limit=10&offset=0×tamp=1589522687689&signature=signature"
返回数据
{
"code": 0,
"data": {
"items": [{
"address": "mfaFpdVCb6UFS5AXUhC8VGXgj9dnJ37nLP",
"amount": "0.001",
"code": 0,
"confirmations": 0,
"currency": "BTC",
"state": "confirmed",
"transaction_id": "52e1537002f51acbf5f52b9dfeab6a9e7cc185a669cda2573e768420b0839523",
"created_at": 1608606000000,
"updated_at": 1608606000000,
"is_onchain": true
}]
}
}
查询提现记录
Query parameters
| 字段名称 | 数据类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
| currency | String | true | "" | 币种名称 |
| limit | int | false | 10 | 分页大小,最大50 |
| offset | int | false | 1 | 分页偏移 |
返回数据
| 字段名称 | 数据类型 | 说明 |
|---|---|---|
| code | int | 错误码,0代表正常,其他值代表失败 |
| state | string | 充值状态 mempool/rollback/pending/confirmed/rejected |
| address | string | 充值来源地址 |
| amount | string | 充值金额 |
| confirmations | int | 确认数,如果是内部地址,由于不会上链,一直为0 |
| currency | string | 币种 |
| transaction_id | string | 链上交易哈希 |
| created_at | int | 订单创建时间戳 |
| updated_at | int | 订单更新时间戳 |
| is_onchain | bool | 订单是否上链(内部地址充值不上链) |
查询余额
GET /mapi/v1/wallet/balance
curl -H "X-MatrixPort-Access-Key: Your Access Key" "https://api.matrixport.com/mapi/v1/wallet/balance?timestamp=1589522687689&signature=signature"
返回数据
{
"code": 0,
"data": {
"items": [{
"currency": "BTC",
"balance": "1.2",
"available_balance": "1.2",
"frozen_balance": "0",
"unconfirmed_balance": "0.5"
}]
}
}
查询余额账户的所有余额。
查询参数
无
返回数据
| 字段名称 | 数据类型 | 说明 |
|---|---|---|
| currency | string | 币种名称 |
| balance | string | 余额 |
| available_balance | string | 可用余额 |
| frozen_balance | string | 冻结余额 |
| unconfirmed_balance | string | unconfirmed 余额 |