Websocket API


Along with REST API Beldex offers WebSocket API allowing you to subscribe to the public and private channels that provide real time changes of the trading information.

We use Socket.io at the background of the websocket data provider. The examples in this document will be implemented in node.js using the socket.io-client package.

Base URL: https://ws.beldex.io

Subscription Methods


Method Example Request
state.subscribe { "method" : "state.subscribe", "params" : ["BTCUSDT"] }
depth.subscribe { "method" : "depth.subscribe", "params" : ["BTCUSDT",50,"0"] }
kline.subscribe { "method" : "kline.subscribe", "params" : ["BTCUSDT",60] }
deals.subscribe { "method" : "deals.subscribe", "params" : ["BTCUSDT"] }
order.subscribe { "method" : "order.subscribe", "params" : ["BDXBTC","ETHBTC","BDXUSDT"] }
asset.subscribe { "method" : "asset.subscribe", "params" : ["BDX","BTC"] }

Query Methods


Method Example Request
depth.query { "method" : "depth.query", "params" : ["BTCUSDT", 50, "0"] }
kline.query { "method" : "kline.query","params" : ["BTCUSDT",1575539107,1580723167,3600] }
order.query { "method" : "order.query","params" : ["BTCUSDT",0,50] }
asset.query { "method" : "asset.query","params" : ["BDX","BTC"] }

Sample Connection


This sample is based on nodejs client, you can use programming language of your choice to implement this.

const socket = require("socket.io-client")(https://ws.beldex.io/);
const pako   = require("pako");

socket.on("connect", function() {
    socket.emit("message", "{ "method" : "state.subscribe", "params" : ["BTCUSDT"] }");
    socket.on("message", function(data) {
        const result = JSON.parse(pako.inflate(data, { to: "string" }));
        console.log(result);
    });
});

socket.on("disconnect", function(){
    console.log("socket disconnected");
});

Authentication


api_key : api_key is the apiKey you have created from the userAccounts page on beldex.io

passphrase : The passphrase entered while creating the API key

timestamp : Must be number of seconds since unix-epoch in UTC Decimal values are allowed. The timestamp will expire for every 30 seconds. It is recommended to use the time endpoint to retrieve the server time if you find a large discrepancy between your server time and the API server.

sign : It is the authentication string. the description of generating a valid sign can be referred to in the Authentication section.

Example of timestamp : const timestamp = new Date().getTime();

Example of sign :

const crypto = require("crypto");

createSignature(timestamp, method, url, secret) {       
        const hmac = crypto.createHmac("sha256",secret);
        const signature = hmac.update(timestamp+method+url).digest("base64");
        return signature;
    }

createSignature("1420674445.201","POST","/users/ws/verify","SE****");

method is preset as GET

requestPath is preset as /users/ws/verify

If the login fails, the connection will be automatically cut.

Response


All the websocket response are in gzip format. You need to unzip all the response.

Refer : npm pako is used in node js for unzip data. If you use other programming language you can use appropriate unzip module to decompress the data from response.

Login


Login Subscription format :

{ "method":"LOGIN","params":["<api_key>","<passphrase>","<timestamp>","<sign>"] }

Example :

{ "method":"LOGIN","params":["085737c7-29d7-4248-9eac-3c188208cf03","PASSPHARSE","1577750400","GCMqVTyuXyCXwngfxbT1HUjEzi7B1m/wK0UkvJcJOmQ="] }

Refer Authentication section for steps to create signature string

Response :

{ "error": null, "result": { "status": "success" } }

Depth Query


Returns the depth data(buy and sell) for a given market pair

Request
{ "method" : "depth.query", "params" : [pair, limit, offset] }
Sample Request
 { "method" : "depth.query", "params" : ["BTCUSDT", 50, "0"] }
Respone Parameters :
Parameters Parameter Type Description
p Integer Price
a Integer Amount
t Integer Total

Response :

{
"method": null,
"params":[
"",
"bids": [{"p": 9393.9, "a": 0.42097453, "t": 3954.59263737},
 {"p": 9393.8, "a": 0.005, "t": 46.969},
 {"p": 9393.5, "a": 1, "t": 9393.5}]
"asks": [{"p": 9394, "a": 3.01476031, "t": 28320.65835214},
 {"p": 9394.4, "a": 0.00266782, "t": 25.06256821},
 {"p": 9394.5, "a": 0.00135279, "t": 12.70878565}]
 ]
}

Depth Subscription


Returns the updated depth data whenever a change happens in the order book

Request
{ "method" : "depth.subscribe", "params" : [pair, limit, offset] }
Sample Request
 { "method" : "depth.subscribe", "params" : ["BTCUSDT",50,"0"] }
Respone Parameters :
Parameters Parameter Type Description
p Integer Price
a Integer Amount
t Integer Total

Response :

{
"method": "depth.update",
"params":[
true,
"bids": [{"p": 9393.9, "a": 0.42097453, "t": 3954.59263737},
 {"p": 9393.8, "a": 0.005, "t": 46.969},
 {"p": 9393.5, "a": 1, "t": 9393.5}]
"asks": [{"p": 9394, "a": 3.01476031, "t": 28320.65835214},
 {"p": 9394.4, "a": 0.00266782, "t": 25.06256821},
 {"p": 9394.5, "a": 0.00135279, "t": 12.70878565}],
"BTCUSDT"]
}

Deals Subscription


Returns deals data whenever an order has been matched for the given pair.

Request
{ "method" : "deals.subscribe", "params" : [pair] }
Sample Request
 { "method" : "deals.subscribe", "params" : ["BTCUSDT"] }
Respone Parameters :
Parameters Parameter Type Description
p String Price
s String Side id (1: sell, 2: buy) e.g. 1
a String Amount
t String Time

Response :

{
"method": "deals.update",
"params": [
"BTCUSDT",
[{"p": "9394.4", "id": "8073040", "s": "buy", "a": "0.00134133", "t": 1580710402270}]
]
}

Kline Query


Returns array of KLine data for the given market, date range and interval.

Request
{ "method" : "kline.query","params" : [pair,start_time,end_time,interval] }
Sample Request
 { "method" : "kline.query","params" : ["BDXBTC",1575539107,1580723167,3600] }

Response :

{
"result": [
[ 1577286000, // time
"0.00008",  // open price
"0.00008",  // close price
"0.00008",  // highest price
"0.00008",  // lowest price
"1",        // volume
"0.00008",  // amount
"BDXBTC" ]    // market pair
]
}

Kline Subscription


Returns array of KLine updated data for the given market and interval.

Request
{ "method" : "kline.subscribe","params" : [pair,interval] }
Sample Request
 { "method" : "kline.subscribe", "params" : ["BDXBTC",60] }

Response :

{ "method": "kline.update",
  "params": 
   [ [ 1580898360,    // time
       "0.00000731",  // open price
       "0.00000731",  // close price
       "0.00000731",  // highest price
       "0.00000731",  // lowest price
       "0",           // volume
       "0",           // amount
       "BDXBTC" ] ]   // market pair
  }

Order History


Returns Order History between the given date range.

Require Authentication: Yes

Request
{ "method" : "order.history", "params" : [pair,start_time,end_time,offset,limit] }
Sample Request
{ "method" : "order.history", "params" : ["BDXBTC",1580636703,1580723103,0,50] }
Respone Parameters :
Parameters Parameter Type Description
id Integer Order Id
ctime String Created time
source String Source
ftime String Modified time
type String Type
user Integer User Id
price String Return price
market String Market pair
side Integer Side id (1: sell, 2: buy) e.g. 1
deal_money String Deal money
amount String Amount
taker_fee String Taker fees
maker_fee String Maker fees
deal_stock String Deal stock
deal_fee String Deal fees

Response :

{
"result": {
"offset": 0,
"limit": 50,
"records": [{
"id": 91,
"ctime": 1580704838.493078,
"source": "Beldex source 1580704838425",
"ftime": 1580704838.493083,
"type": 1,
"price": "0.00000812",
"market": "BDXBTC",
"side": 1,
"deal_money": "0.00274166928",
"amount": "337.644",
"taker_fee": "0.001",
"maker_fee": "0.001",
"deal_stock": "337.644",
"deal_fee": "0.00000274166928"
},
{
"id": 90,
"ctime": 1580704755.196429,
"source": "Beldex source 1580704755128",
"ftime": 1580704755.19756,
"type": 1,
"price": "0.001",
"market": "BDXBTC",
"side": 2,
"deal_money": "0.3",
"amount": "300",
"taker_fee": "0.001",
"maker_fee": "0.001",
"deal_stock": "300",
"deal_fee": "0.3"
}]
}
}

Order Query


Returns the list of orders for the given pair, offset and limit.

Require Authentication: Yes

Request
{ "method" : "order.query", "params" : [pair,offset,limit] }
Sample Request
 { "method" : "order.query","params" : ["BDXBTC",0,50] }
Respone Parameters :
Parameters Parameter Type Description
id Integer Order Id
market String Market pair
side Integer Side id (1: sell, 2: buy) e.g. 1
user Integer User Id
deal_stock String Deal Stock
source String Source
price String Return price
type String Type id (1: deposit, 2: withdraw) e.g. 1
ctime String Created time
mtime String Modified time
deal_money String Deal money
amount String Amount
taker_fee String Taker fees
maker_fee String Maker fees
left String Remain balance
deal_fee String Deal fees

Response :

{
"result":
{
"limit": 50,
"offset": 0,
"total": 2,
"records": [
  {
    "id": 82,
    "market": "BDXBTC",
    "side": 1,
    "deal_stock": "0",
    "source": "beldex exchange",
    "price": "0.1",
    "type": 1,
    "ctime": 1580281466.169867,
    "mtime": 1580281466.169867,
    "deal_money": "0",
    "amount": "100",
    "taker_fee": "0.1",
    "maker_fee": "0.005",
    "left": "100",
    "deal_fee": "0"
  }
],
}
}

Order Subscription


Return the user's orders for the given pair.

Require Authentication: Yes

Request :

{ "method" : "order.subscribe", "params" : [pairs] }

Request :

{ "method" : "order.subscribe", "params" : ["BDXBTC"] }
Respone Parameters :
Parameters Parameter Type Description
id Integet Order Id
market String Market Pair
left String Remain Balance
maket_fee String Maker fee
side String Side id (1: sell, 2: buy) e.g. 1
deal_stock String Deal Stock
mtime String Modified time
source String Source
taker_fee String Taker fee
amount String Amount
price String Price
type String Type
deal_money String Deal Money
ctime String Created time
deal_fee String Deal Fee

Response :

{
"method": "order.update",
"params": [
3,      // 1: PUT, 2: UPDATE, 3: FINISH
{
"id": 311,
"market": "BDXBTC",
"left": "0",
"maker_fee": "0.001",
"side": 1,
"deal_stock": "120",
"mtime": 1581141178.888747,
"source": "Beldex source 1581141178860",
"taker_fee": "0.001",
"amount": "120",
"price": "0.00000802",
"type": 1,
"deal_money": "0.000966",
"ctime": 1581141178.88874,
"deal_fee": "9.66000000e-7"
}]
}

State Subscription


Return latest price, high, low, volume for the given pair.

Request
{ "method" : "state.subscribe", "params" : [pairs] }
Sample Request
 { "method" : "state.subscribe", "params" : ["BDXBTC"] }
Respone Parameters :
Parameters Parameter Type Description
l String Last price
d String Deal price
p String Period in seconds
v String Volume
o String Open price
c String Close price
lo String Lowest price
h String Highest price
cp String Change percentage
cv String Close volume

Response :

{ "method": "state.update",
  "params": 
   [ "BDXBTC",
     { "l": "0.00000801",
       "d": "0.30605477928",
       "p": 86400,
       "v": "1048.644",
       "o": "0.001",
       "c": "0.00000801",
       "lo": "0.00000801",
       "h": "0.001",
       "cp": "-99.19900000",
       "cv": "-0.00099199" } 
    ]
}

Asset Query


Returns user asset balances (available and freeze).

Require Authentication: Yes

Request
{ "method" : "asset.query","params" : [List of assets] }
Sample Request
 { "method" : "asset.query","params" : ["BDX","BTC"] }
Respone Parameters :
Parameters Parameter Type Description
avilable String Available Balance
freeze String Freeze Balance(balances in order)

Response :

{
  "result": 
   { "BDX": { "available": "0", "freeze": "0" },
     "BTC": { "available": "0.0012987", "freeze": "0" } 
   } 
}

Asset Subscription


Return asset balances when your balance change.

Require Authentication: Yes

Request
{ "method" : "asset.subscribe","params" : [List of assets] }
Sample Request
 { "method" : "asset.subscribe", "params" : ["BDX","BTC"] }
Respone Parameters :
Parameters Parameter Type Description
avilable String Available Balance
freeze String Freeze Balance

Response :

{
"method": "asset.update",
"params":[
"BTC": {"available": "0.69278123", "freeze": "0.000966"}
]
}

results matching ""

    No results matching ""