sFOX API
Sign InOpen Account
sFOX API
sFOX API
  • Introduction
    • Welcome
    • Getting Started
    • Systems & Operations
    • Rate Limits
  • REST API
    • REST Endpoints
    • Authentication
    • Rate Limits
    • Account Management
      • Get All Balances
      • Get All Currencies
      • Get All Currency Pairs
      • Get All Transactions
      • Get Fees
    • Orders
      • Order Types
      • Create Order
      • Cancel Order
      • Cancel Multiple Orders
      • Cancel All Orders
      • Amend Order
      • Get Single Order
      • Get All Open Orders
      • Get All Done Orders
      • Get All Trades
    • Request for Quote (RFQ)
    • Post-Trade Settlement (PTS)
      • Get Account Risk Metrics
      • Get All Positions
      • Get All Funding Transactions
      • Get All Funding Rates
      • Get All Risk Modes
      • Create Transfer
    • Shorting
      • Get Account Risk Metrics
      • Get All Positions
    • Transfers
      • Get All Deposit Addresses
      • Get Single Deposit Address
      • Create Deposit Address
      • Deposit From Bank Account
      • Withdraw From Account
      • Get Single Withdrawal Fee
      • Create Transfer
    • Custody (sFOX SAFE)
      • Get Whitelisted Addresses
      • Add Whitelisted Address
      • Delete Whitelisted Address
      • Get Approval Rules
      • Create Approval Rule
      • Edit Approval Rule
      • Get Approval Requests
      • Respond to Approval Request
    • Staking
      • Get Staking Currencies
      • Get All Staking Transactions
      • Create Stake
      • Cancel Stake
      • Unstake
    • Market Data
      • Get Candlesticks
      • Get Volume Analytics
      • Get Order Estimate
      • Get Order Book
    • Reporting
      • Get Orders Report
      • Get Monthly Summary
      • Get Portfolio Valuation
      • Get All Transactions
  • WebSocket API
    • Introduction
    • Connecting
    • Rate Limits
    • Authentication
    • Subscribing & Unsubscribing
    • Message Format
    • Market Data
      • Order Book
      • Trades
      • Ticker
    • Orders & Account Data
      • Orders
      • Trades
      • Balances
      • Post-Trade Settlement
  • FIX API
    • FIX Order Entry
    • FIX Market Data
    • QuickFIX Guide
  • Errors
    • Error Codes
Powered by GitBook
On this page
  • Subscribing
  • Balances Message
  • Example Code
  1. WebSocket API
  2. Orders & Account Data

Balances

PreviousTradesNextPost-Trade Settlement

Last updated 5 months ago

The private.user.balances feed provides you with real-time updates on changes to your account balances, including Web3 wallet balances updates.

Updates reflect your balances inclusive of completed, partially completed, or pending transactions, orders, and transfers. For example, a given balance update does reflect outstanding and/or partially filled orders.

Subscribing

Feed name

private.user.balances

// Request -> balances feed
{
    "type": "subscribe", 
    "feeds": ["private.user.balances"]
}

Balances Message

There are 2 types of balances message payloads:

  • Account balances: Your trading/custody account balances (Refer to for payload and balance object details).

  • Web3 wallet balances: Your account's Web3 wallet balances (see below for payload details). Web3 wallet messages can be identified by the type field.

Web3 Wallet Message

Key
Description

address

The web3 wallet address

balance

The total balance of this currency in your web3 wallet

network

The token network

type

Specifies that this is a Web3 balance message. (web3)

// Balances feed message (account balances)
{
  "sequence": 10,
  "recipient": "private.user.balances",
  "timestamp": 1649899310281716289,
  "payload": [{
    "currency": "btc",
    "balance": "0.17572419",
    "available": "0.17572419",
    "held": "0",
    "trading_wallet": "0.17572419",
    "collateral_wallet": "0",
    "borrow_wallet": "0",
    "lending_wallet": "0"
  }, {
    "currency": "usd",
    "balance": "100",
    "available": "100",
    "held": "0",
    "trading_wallet": "100",
    "collateral_wallet": "0",
    "borrow_wallet": "0",
    "lending_wallet": "0"
  }]
}
// Balances feed message (web3 wallet balances)
{
  "sequence": 11,
  "recipient": "private.user.balances",
  "timestamp": 1649899310281716289,
  "payload": {
    "address": "0x0bCdB57ae247F434C91b1d4521fFd6601f7e8999",
    "balance": "250.5",
    "currency": "USDC",
    "network": "Ethereum",
    "type": "web3"
  }
}

Example Code

const WebSocket = require('ws')

const ws = new WebSocket('wss://ws.sfox.com/ws')

ws.on('open', function() {
  const authMessage = {
   type: 'authenticate',
   apiKey: '<API_KEY>'
  }
  
  ws.send(JSON.stringify(authMessage))
})

// After successful authentication, subscribe to balances feed
const subscribeMsg = {
  type: 'subscribe',
  feeds: ['private.user.balances']
}

ws.send(JSON.stringify(subscribeMsg))
import asyncio
import json

import websockets


async def main(uri):
    async with websockets.connect(uri) as ws:
        await ws.send(json.dumps({
            "type": "authenticate",
            "apiKey": "<API_KEY>"
        }))

        # After successful authentication message
        await ws.send(json.dumps({
            "type": "subscribe",
            "feeds": ["private.user.balances"]
        }))
        async for msg in ws:
            print(msg)


asyncio.run(main("wss://ws.sfox.com/ws"))
Get All Balances