# 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](https://docs.sfox.com/rest-api/account-management/get-all-balances) 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 %}
