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
  1. WebSocket API

Subscribing & Unsubscribing

PreviousAuthenticationNextMessage Format

Last updated 5 months ago

Once to the WebSocket API you can subscribe to various feeds using the subscribe or unsubscribe commands.

These commands should be JSON with the following properties:

Property
Type
Description

type

string

Command you are sending to the WebSocket (subscribe or unsubscribe).

feeds

string[]

List of the feeds that should be subscribed/unsubscribed to/from, respectively.

Subscribing

To begin receiving feed messages, you must send a subscribe message to the server indicating which feeds to receive. This message is mandatory—you may be disconnected if no subscribe has been received.

To ensure optimal performance, we suggest maintaining a separate Websocket connection for market data subscriptions (, , ) to ensure high-frequency market data doesn't impact the delivery of your time-sensitive account level updates, such as or messages.

Subscribe Message

// Request -> Subscribe to feed(s)
{
    "type": "subscribe", 
    "feeds": ["<feed 1>", "<feed 2>", ...]
}

Once a subscribe message is received the server responds with a subscribe message that lists all feeds you are subscribed to. Subsequent subscribe messages add to the list of subscriptions.

// Response -> Subscribe request
{
    "type": "success",
    "sequence": 2,
    "timestamp": 1727392002151355776,
    "payload": {
        "action": "subscribe",
        "feeds": [
            "<feed 1>",
            "<feed 2>",
            ...
        ]
    },
    "action": "subscribe"
}

Example Code

const subscribeMsg = {
  type: 'subscribe',
  feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(subscribeMsg));
subscribe_msg = {
    "type": "subscribe",
    "feeds": ["ticker.sfox.btcusd"],
}

await ws.send(json.dumps(subscribe_msg))

Unsubscribing

To unsubscribe from a feed or feeds, send an unsubscribe message. The structure is equivalent to subscribe messages.

Unsubscribe Message

// Request -> Unsubscribe to feed(s)
{
    "type": "unsubscribe", 
    "feeds": ["<feed 1>", "<feed 2>", ...]
}

Once a unsubscribe message is received the server responds with an unsubscribe message that lists all feeds you are unsubscribed from.

// Response -> Subscribe request
{
    "type": "success",
    "sequence": 4,
    "timestamp": 1727394717752870042,
    "payload": {
        "action": "unsubscribe",
        "feeds": [
            "<feed 1>",
            "<feed 2>",
            ...
        ]
    },
    "action": "unsubscribe"
}

Example Code

const unsubscribeMsg = {
  type: 'unsubscribe',
  feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(unsubscribeMsg))
unsubscribe_msg = {
    "type": "unsubscribe",
    "feeds": ["ticker.sfox.btcusd"],
}

await ws.send(json.dumps(unsubscribe_msg))
connected
orderbooks
tickers
trades
balances
open-orders