Foxbit websocket API 2.0

Introduction to WebSocket and how to use

If you want to see the documentation of the other apis, click on the button below:

See all available API's

In the examples described in this documentation, we use NodeJs and ws lib.

• Install the latest stable version of NodeJs on: https://nodejs.org/en/download/

• Open Terminal (Mac), Cmd (Windows) or Bash (Linux) and enter the command:

In the same folder you will save the scripts exemplified below

npm install ws

ref link: https://www.npmjs.com/package/ws

GitHub – WebSocket Example

You can download the example directly from the repository on GitHub:

• https://github.com/foxbit-group/foxbit-api-samples

Connecting Websocket

With NodeJs:

//Instantiate Websocket module
const WebSocket=require('ws');

 

//WebSocketAPI Address
var wsAddress='wss://api.foxbit.com.br/';

//Setup WebSocket client Address
var ws=new WebSocket(wsAddress);

WS Rate-Limit

The Rate Limit of the WS API is divided into three topics, these being connection, concurrent connection and messages.

#CommonPartnersBlock time
Connections per period 1 connection per second3 connections per second1s
Connections concurrent 10 connections concurrent 20 connections concurrentN/A
Messages per period 750 messages per 60s2100 messages per 60s60s
  • The limits on the WS API are based on the IPs
  • The block time is the blocking time after reaching the limit
  • Blocking for concurrent connections is done automatically, i.e. when one connection is closed another one becomes available.

Message Fame example

Object in JSON-formatted format used as a frame to send and receive data:

var messageFrame={
 'm': 0, //MessageType (0_Request / 1_Reply / 2_Subscribe / 3_Event / Error)
 'i': 0, //SequenceNumber (long integer)
 'n': '', //Endpoint (integer)
 'o': '' //Payload (string)
}

Payload

Object in JSON-formatted format which contains mandatory (and different) fields for sending data to the triggered  endpoint.

Each endpoint has a specific package, see payload format in documentation.

//Example of building the payload for the *getL2Snapshot* endpoint

var payload={

 'OMSId': 1,
 'InstrumentId': 1;

};

Events

Set the Open event to handle the connection return.

Each endpoint has a specific package, see payload format in documentation.

//Event Open Connection

ws.on('open', function open(){

 console.log('Conected');
});

Set event to receive socket message:

//Event Receiving Message

ws.on('message', function incoming(data){

  //data contains the response payload
 console.log(data);
});

Define event for error handling:

//Event Error Message

ws.on('error', function(){

 console.log('Error');
});

Define event handle connection termination:

//Event Close Message

ws.on('close', function close(){

 console.log('Connection Closed');
});

Sending Data

To send data to the server you must use the “messageFrame” object described above, use the “n” field to inform the Endpoint to trigger and the “o” field to send the payload.

//Indicate to which endpoint it will be sent.

messageFrame.n='WebAuthenticateUser'

//Add the payload for the indicated endpoint
messageFrame.o=JSON.stringify(payload);


ws.send(JSON.stringify(messageFrame), function ack(error){

 //If you find errors, send
 console.log(error);

});

Data sent, if no error is returned, the message ends in the message event.

Authentication

Endpoints PRIVATE need authentication to be accessed.

AuthenticateUser authenticates a user in current WebSession using APIKEY previously defined.

You need to call it to use methods that require an active SessionToken.

AuthenticateUser can be called whenever authentication is required.

Request:

{
 'APIKey': 'APIKey',
 'Signature': 'Signature',
 'UserId': 'UserId',
 'Nonce': 'Nonce',
}

>>> Obs. 1: Nonce=Date.now();
>>> Obs. 2: Signature=crypto.HmacSHA256(nonce + userId +APIKey, APISecret).toString(crypto.enc.Hex);

Return when authentication succeeds:

{
  'Authenticated': true
  'SessionToken': 'SessionToken'
  'UserId': 123
}

Unsuccessful authentication:

{
  'Authenticated': false
}

Operations

  • SEND AuthenticateUser

    PUBLIC - AuthenticateUser

    Operation IDAuthenticateUser
    • #PUBLIC

    Accepts the following message:

    AuthenticateUser
    Message IDAuthenticateUser

    AuthenticateUser a user using the API Key and signature.

    object

    Retry example

    object
    • #PUBLIC

    Examples

  • SEND CancelAllOrders

    PRIVATE - CancelAllOrders

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDCancelAllOrders
    • #PRIVATE

    Accepts the following message:

    CancelAllOrders
    Message IDCancelAllOrders

    Cancels all open matching orders for the specified instrument.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND CancelOrder

    PRIVATE - CancelOrder

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDCancelOrder
    • #PRIVATE

    Accepts the following message:

    CancelOrder
    Message IDCancelOrder

    Cancels an open order that has been placed but has not yet been fully executed.
    A user with Trading permission can cancel an order only for an account with which he is associated; a user with Operator permission can cancel an order for any account.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetAccountInfo

    PRIVATE - GetAccountInfo

    Operation IDGetAccountInfo
    • #PRIVATE

    Accepts the following message:

    GetAccountInfo
    Message IDGetAccountInfo

    Returns detailed information about one specific account and existing on a specific Order Management System.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetAccountPositions

    PRIVATE - GetAccountPositions

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoints for account listing and transaction limits for the latest updates and improvements.
    Operation IDGetAccountPositions
    • #PRIVATE

    Accepts the following message:

    GetAccountPositions
    Message IDGetAccountPositions

    Retrieves a list of positions (balances) for a specific user account running under a specific Order Management System. The trading day runs from UTC Midnight to UTC Midnight.
    Users with Trading or AccountReadOnly permission must specify an account ID with which they're associated on the Order Management System. Users with Operator permission can specify other accounts.

    array<any>

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetAccountTrades

    PRIVATE - GetAccountTrades

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetAccountTrades
    • #PRIVATE

    Accepts the following message:

    GetAccountTrades
    Message IDGetAccountTrades

    Requests the details on up to 200 past trade executions for a single specific account and Order Management System, starting at index i, where i is an integer identifying a specific execution in reverse order; that is, the most recent execution has an index of 0, and increments by one as trade executions recede into the past.
    Users with Trading or AccountReadOnly permission may access trade information only for accounts with which they are associated; users with Operator permission may access trade information for any account.
    The operator of the trading venue determines how long to retain an accessible trading history before archiving.
    Only the last three months can be listed in the trades search.

    array<any>

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetDepositTickets

    PRIVATE - GetDepositTickets

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetDepositTickets
    • #PRIVATE

    Accepts the following message:

    GetDepositTickets
    Message IDGetDepositTickets

    Retrieves deposit tickets.

    array<any>

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetInstrument

    PUBLIC - GetInstrument

    Operation IDGetInstrument
    • #PUBLIC

    Accepts the following message:

    GetInstrument
    Message IDGetInstrument

    Retrieves an instruments. An instrument is a pair of exchanged products (or fractions of them) such as US dollars and BitCoin.

    object

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND GetInstruments

    PUBLIC - GetInstruments

    Operation IDGetInstruments
    • #PUBLIC

    Accepts the following message:

    GetInstruments
    Message IDGetInstruments

    Retrieves a list of instruments available on the exchange. An instrument is a pair of exchanged products (or fractions of them) such as US dollars and BitCoin.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND GetOpenOrders

    PUBLIC - GetOpenOrders

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetOpenOrders
    • #PUBLIC

    Accepts the following message:

    GetOpenOrders
    Message IDGetOpenOrders

    Returns an array of 0 or more orders that have not yet been filled (open orders) for a single account on a specific Order Management System. The call returns an empty array if an account has no open orders.
    A user with Trading or AccountReadOnly permission must be associated with the account named by AccountId; a user with Operator permissoin can get open orders for any account.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND GetOrderHistory

    PRIVATE - GetOrderHistory

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetOrderHistory
    • #PRIVATE

    Accepts the following message:

    GetOrderHistory
    Message IDGetOrderHistory

    Returns a complete list of all orders, both open and executed, for a specific account on the specified Order Management System.
    The account named in the request must be associated with the calling user.

    array<any>

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetOrderStatus

    PRIVATE - GetOrderStatus

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetOrderStatus
    • #PRIVATE

    Accepts the following message:

    GetOrderStatus
    Message IDGetOrderStatus

    Retrieves the status information for a single order.
    A user with Trading permission can retrieve status information for accounts and orders with which the user is associated; a user with Operator permission can retreive status information for any account or order ID.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetProducts

    PUBLIC - get-products

    Operation IDget-products
    • #PUBLIC

    Accepts the following message:

    get-products
    Message IDget-products

    Returns an array of products and currencies available on the exchange. A product is an asset that is tradable or paid out.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND GetL2Snapshot

    PUBLIC - GetL2Snapshot

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetL2Snapshot
    • #PUBLIC

    Accepts the following message:

    GetL2Snapshot
    Message IDGetL2Snapshot

    Provides a current Level 2 snapshot of a specific instrument trading on an Order Management System to a user-determined market depth.
    The Level 2 snapshot allows the user to specify the level of market depth information on either side of the bid and ask. Depth in this call is 'depth of market', the number of buyers and sellers at greater or lesser prices in the order book for the instrument.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND GetTickerHistory

    PUBLIC - GetTickerHistory

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetTickerHistory
    • #PUBLIC

    Accepts the following message:

    GetTickerHistory
    Message IDGetTickerHistory

    Requests a ticker history (high, low, open, close, volume, bid, ask, ID) of a specific instrument from a given date to the present. You will need to format the returned data per your requirements.
    Because permission is Public, any user can retrieve the ticker history for any instrument on the OMS.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND GetTradesHistory

    PRIVATE - GetTradesHistory

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetTradesHistory
    • #PRIVATE

    Accepts the following message:

    GetTradesHistory
    Message IDGetTradesHistory

    Retrieves a list of trades for a specified account, order ID, user, instrument, or starting and ending time stamp. The returned list begins at start index i, where i is an integer identifying a specific trade in reverse order; that is, the most recent trade has an index of 0. “Depth” is the count of trades to report backwards from StartIndex.
    Users with Trading permission can retrieve trade history for accounts with which they are associated; users with Operator permission can retrieve trade history for any account.
    Caution: You must coordinate StartIndex, Depth, StartTimeStamp, and EndTimeStamp to retrieve the historical information you need. As the diagram shows, it is possible to specify values (for example, EndTimeStamp and Depth) that can exclude information you may want (the red areas).
    Note: In this call, “Depth” refers not to the depth of the order book, but to the count of trades to report. The owner of the trading venue determines how long to retain order history before archiving.

    array<any>

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetUserInfo

    PRIVATE - GetUserInfo

    Operation IDGetUserInfo
    • #PRIVATE

    Accepts the following message:

    GetUserInfo
    Message IDGetUserInfo

    Returns an object with the user data.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetUserPermissions

    PRIVATE - GetUserPermissions

    Operation IDGetUserPermissions
    • #PRIVATE

    Accepts the following message:

    GetUserPermissions
    Message IDGetUserPermissions

    Returns the permissions for use.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND GetWithdrawTickets

    PRIVATE - GetWithdrawTickets

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDGetWithdrawTickets
    • #PRIVATE

    Accepts the following message:

    GetWithdrawTickets
    Message IDGetWithdrawTickets

    Returns a limited array of withdrawal tickets from the specified Order Management System and account.
    GetWithdrawTickets is usable with Operator and Trading permissions. It returns tickets from a logged account.

    array<any>

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND Logout

    PUBLIC - Logout

    Operation IDLogout
    • #PUBLIC

    Accepts the following message:

    Logout
    Message IDLogout

    LogOut ends the current websocket session.

    object

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND SendOrder

    PRIVATE - SendOrder

    This WebSocket channel is deprecated and will no longer receive new features. Please use the corresponding REST endpoint for the latest updates and improvements.
    Operation IDSendOrder
    • #PRIVATE

    Accepts the following message:

    SendOrder
    Message IDSendOrder

    Creates an order.
    Anyone submitting an order should also subscribe to the various market data and event feeds, or call GeOpenOrders or GetOrderStatus to monitor the status of the order. If the order is not in a state to be executed, GetOpenOrders will not return it.
    A user with Trading permission can create an order only for those accounts and instruments with which the user is associated; a user with Operator permissions can create an order for any account and instrument.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND SubscribeAccountEvents

    PRIVATE - SubscribeAccountEvents

    Operation IDSubscribeAccountEvents
    • #PRIVATE

    Accepts the following message:

    SubscribeAccountEvents
    Message IDSubscribeAccountEvents

    Subscribes the user to notifications about the status of account-level events: orders, trades, position updates, deposits, and withdrawals for a specific account on the Order Management System (OMS). The subscription reports all events associated with a given account; there is no filter at the call level to subscribe to some events and not others.

    object

    Reply example

    object
    • #PRIVATE

    Examples

  • SEND SubscribeLevel1

    PUBLIC - SubscribeLevel1

    Operation IDSubscribeLevel1
    • #PUBLIC

    Accepts the following message:

    SubscribeLevel1
    Message IDSubscribeLevel1

    Retrieves the latest Level 1 Ticker information and then subscribes the user to ongoing Level 1 market data event updates for one specific instrument.
    The SubscribeLevel1 call responds with the Level 1 response shown below. The OMS then periodically sends in the same format as this response Leve1UpdateEvent information when best-bid/best-offer issue, until you send the UnsubscribeLevel1 call.
    Only a user with Operator permission can issue Level1MarketData permission using the call *AddUserMarketDataPermission.

    object

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND SubscribeLevel1Markets

    PUBLIC - SubscribeLevel1Markets

    Operation IDSubscribeLevel1Markets
    • #PUBLIC

    Accepts the following message:

    SubscribeLevel1Markets
    Message IDSubscribeLevel1Markets

    Retrieves the latest Level 1 Ticker information and then subscribes the user to ongoing Level 1 market data event updates for one specific instrument.
    The SubscribeLevel1 call responds with the Level 1 Markets response shown below. The OMS then periodically sends in the same format as this response Leve1UpdateEvent information when best-bid/best-offer issue, until you send the UnsubscribeLevel1 call.
    Only a user with Operator permission can issue Level1MarketData permission using the call *AddUserMarketDataPermission.

    object

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND SubscribeLevel2

    PUBLIC - SubscribeLevel2

    Operation IDSubscribeLevel2
    • #PUBLIC

    Accepts the following message:

    SubscribeLevel2
    Message IDSubscribeLevel2

    Retrieves the latest Level 2 Ticker information and then subscribes the user to Level 2 market data event updates for one specific instrument. Level 2 allows the user to specify the level of market depth information on either side of the bid and ask. The SubscribeLevel2 call responds with the Level 2 response shown below. The OMS then periodically sends Level2UpdateEvent information in the same format as this response until you send the UnsubscribeLevel2 call.
    Only a user with Operator permission can issue a Level2MarketData permission using the call AddUserMarketDataPermission.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND SubscribeTicker

    PUBLIC - SubscribeTicker

    Operation IDSubscribeTicker
    • #PUBLIC

    Accepts the following message:

    SubscribeTicker
    Message IDSubscribeTicker

    Subscribes a user to a Ticker Market Data Feed for a specific instrument and interval. SubscribeTicker sends a response object as described below, and then periodically returns a TickerDataUpdateEvent that matches the content of the response object.
    Only a user with Operator permission can issue a Level1MarketData permission using the call AddUserMarketDataPermission.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND SubscribeTrades

    PUBLIC - SubscribeTrades

    Operation IDSubscribeTrades
    • #PUBLIC

    Accepts the following message:

    SubscribeTrades
    Message IDSubscribeTrades

    Subscribes an authenticated user to the Trades Market Data Feed for a specific instrument. Each trade has two sides: Buy and Sell.
    SubscribeTrades returns the response documented here for your immediate information, then periodically sends the OrderTradeEvent documented in SubscribeAccountEvents. Only a user with Operator permission can issue the permission Level1MarketData using the call AddUserMarketDataPermission.

    array<any>

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND UnsubscribeLevel1

    PUBLIC - UnsubscribeLevel1

    Operation IDUnsubscribeLevel1
    • #PUBLIC

    Accepts the following message:

    UnsubscribeLevel1
    Message IDUnsubscribeLevel1

    Unsubscribes the user from a Level 1 Market Data Feed subscription.

    object

    Retry example

    object
    • #PUBLIC

    Examples

  • SEND UnsubscribeLevel2

    PUBLIC - UnsubscribeLevel2

    Operation IDUnsubscribeLevel2
    • #PUBLIC

    Accepts the following message:

    UnsubscribeLevel2
    Message IDUnsubscribeLevel2

    Unsubscribes the user from a Level 2 Market Data Feed subscription.

    object

    Retry example

    object
    • #PUBLIC

    Examples

  • SEND UnsubscribeTicker

    PUBLIC - UnsubscribeTicker

    Operation IDUnsubscribeTicker
    • #PUBLIC

    Accepts the following message:

    UnsubscribeTicker
    Message IDUnsubscribeTicker

    Unsubscribes the user from a Ticker Market Data Fee.

    object

    Reply example

    object
    • #PUBLIC

    Examples

  • SEND UnsubscribeTrades

    PUBLIC - UnsubscribeTrades

    Operation IDUnsubscribeTrades
    • #PUBLIC

    Accepts the following message:

    UnsubscribeTrades
    Message IDUnsubscribeTrades

    Unsubscribes the user from a Trades Market Data Feed.

    object

    Retry example

    object
    • #PUBLIC

    Examples