> For the complete documentation index, see [llms.txt](https://docs.sfox.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sfox.com/websocket-api/orders-and-account-data/balances.md).

# Balances

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

{% hint style="info" %}
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.
{% endhint %}

### **Subscribing**

<table data-header-hidden><thead><tr><th width="100"></th><th>Feed Name</th></tr></thead><tbody><tr><td>Feed name</td><td><strong><code>private.user.balances</code></strong> </td></tr></tbody></table>

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

### **Balances Message**

{% hint style="info" %}
There are 2 types of balances message payloads:&#x20;

* **Account balances:** Your trading/custody account balances (Refer to [Get All Balances](/rest-api/account-management/get-all-balances.md) 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.
  {% endhint %}

#### **Web3 Wallet Message**

<table><thead><tr><th width="174.71345029239765">Key</th><th>Description</th></tr></thead><tbody><tr><td><strong><code>address</code></strong></td><td>The web3 wallet address</td></tr><tr><td><strong><code>balance</code></strong></td><td>The total balance of this currency in your web3 wallet</td></tr><tr><td><strong><code>network</code></strong></td><td>The token network</td></tr><tr><td><strong><code>type</code></strong></td><td>Specifies that this is a Web3 balance message. (<code>web3</code>)</td></tr></tbody></table>

{% tabs %}
{% tab title="Account balances message" %}

```json
// 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"
  }]
}
```

{% endtab %}

{% tab title="Web3 wallet balances message" %}

```json
// 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"
  }
}
```

{% endtab %}
{% endtabs %}

### Example Code

{% tabs %}
{% tab title="NodeJS" %}

```javascript
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))
```

{% endtab %}

{% tab title="Python" %}

```python
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"))
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sfox.com/websocket-api/orders-and-account-data/balances.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
