# Flows

<div class="api-grid" markdown>
<div class="api-main" markdown>

<span class="api-method api-method--get">GET</span> <span class="api-url">`https://api.pro.anboto.xyz/api/v2/portfolio/flows`</span>

The flows the daily PnL subtracts, deduplicated and signed: `net_flow_usd` is a flow's contribution to `net_flows` (positive in, negative out; 0 with `counted_in_pnl: false` for transfers within one account and for unknown directions). When the asset could not be priced, `usd_value` and `net_flow_usd` are null (`counted_in_pnl: false`; transfers within one account stay 0) and the flow is counted in `totals.unpriced_count`. The window is half-open, [startMs, endMs): a flow at exactly endMs is not returned, so the next window starts at `to_ms`. Oldest first; pass `next_cursor` back as `cursor` for the next page; `totals` always cover the whole window. **ApiKeyAuth** required.

### Query params

<div class="api-params" markdown>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`accounts` <span class="api-param__type">string</span></div>
<div class="api-param__desc" markdown="span">Comma-separated accounts: `binance` = every account on Binance (main and subaccounts), `binance:` = the main account only, `binance:Sub1` = that subaccount. Omit for every account of the team. At most 20 items. Unlike /api/v2/trading/*, a missing subaccount means all accounts. Sign the raw value, e.g. `accounts=binance:,bybit:Sub1`.</div>
<div class="api-param__meta" markdown="span">Example: `binance`</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`startMs` <span class="api-param__type">integer</span></div>
<div class="api-param__desc" markdown="span">Start, ms since epoch (inclusive). Defaults to 30 days before endMs. Must be before endMs</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`endMs` <span class="api-param__type">integer</span></div>
<div class="api-param__desc" markdown="span">End, ms since epoch (exclusive: a flow at exactly endMs is not returned). Defaults to now; at most 4102444800000 (2100-01-01). The window spans at most 366 days</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`types` <span class="api-param__type">string</span></div>
<div class="api-param__desc" markdown="span">Comma-separated flow types: deposit, withdrawal, internal_transfer. Default all</div>
<div class="api-param__meta" markdown="span">Example: `deposit,withdrawal`</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`external` <span class="api-param__type">boolean</span></div>
<div class="api-param__desc" markdown="span">true: only flows that cross the team's boundary (deposits, withdrawals); false: only transfers inside the team. Default both</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`limit` <span class="api-param__type">integer</span></div>
<div class="api-param__desc" markdown="span">Flows per page, 1 to 500 (default 500)</div>
<div class="api-param__meta" markdown="span">Default: `500`</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`cursor` <span class="api-param__type">string</span></div>
<div class="api-param__desc" markdown="span">`next_cursor` of the previous page</div>
</div>
</div>

### Headers

| Header | | |
|---|---|---|
| `X-API-KEY` | string | <span class="api-req">required</span> |
| `X-TIMESTAMP` | int64 ms | <span class="api-req">required</span> |
| `X-SIGN` | string | <span class="api-req">required</span> |
| `X-RECV-WINDOW` | int, default 5000 | |

### Responses

| | |
|---|---|
| <span class="api-status api-status--ok">200</span> | The flows |
| <span class="api-status api-status--err">400</span> | Invalid parameter, {"error": "..."} |
| <span class="api-status api-status--err">401</span> | Unauthorized: missing or unknown API key |
| <span class="api-status api-status--err">429</span> | Rate limit exceeded, retry later |
| <span class="api-status api-status--err">503</span> | The portfolio service is unavailable, {"error": "..."} |

### Response fields — [PortfolioFlowsResponse](schemas.md#portfolioflowsresponse)

<div class="api-params" markdown>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`from_ms` <span class="api-param__type">integer</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Start of the window, ms since epoch (inclusive)</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`to_ms` <span class="api-param__type">integer</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">End of the window, ms since epoch (exclusive): a flow at exactly to_ms is not in this window. Pass it as startMs to read the next window</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows` <span class="api-param__type">array[[FlowRecord](schemas.md#flowrecord)]</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Flows of this page, oldest first</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].id` <span class="api-param__type">string</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Stable identifier of the flow</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].time` <span class="api-param__type">integer</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Event time, ms since epoch</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].exchange` <span class="api-param__type">string</span> <span class="api-req">required</span></div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].subaccount` <span class="api-param__type">string</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">"" is the main account</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].type` <span class="api-param__type">enum</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">`deposit`, `withdrawal`, `internal_transfer`</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].direction` <span class="api-param__type">enum</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Direction for this account — `in`, `out`, `internal`, `unknown`</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].external` <span class="api-param__type">boolean</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">True for flows that cross the team's boundary (deposits and withdrawals)</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].asset` <span class="api-param__type">string</span> <span class="api-req">required</span></div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].qty` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Absolute amount of the asset, with every stored decimal digit</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].usd_value` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">USD value at the event time; null when the asset could not be priced</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].net_flow_usd` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Signed contribution to the total-scope net_flows of the daily PnL (positive in, negative out). 0 for transfers within one account (whatever the price) and for unknown directions. null when the asset could not be priced: the flow is then not in the USD totals and is counted in totals.unpriced_count. counted_in_pnl is false whenever net_flow_usd is 0 for those reasons or null</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].counted_in_pnl` <span class="api-param__type">boolean</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Whether the flow moves net_flows of the daily PnL: true when net_flow_usd is not null and direction is in or out</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`flows[].source` <span class="api-param__type">string</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Exchange history the flow was read from, e.g. binance_deposit, binance_sub_transfer</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`totals` <span class="api-param__type">[FlowTotals](schemas.md#flowtotals)</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Sums of net_flow_usd over the whole window and selection</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`totals.deposits_usd` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Sum of deposits (≥ 0)</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`totals.withdrawals_usd` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Sum of withdrawals, signed (≤ 0)</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`totals.net_external_usd` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">deposits_usd + withdrawals_usd</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`totals.net_internal_usd` <span class="api-param__type">number</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Net of internal transfers counted in PnL (non-zero when the selection is part of the team)</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`totals.unpriced_count` <span class="api-param__type">integer</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Flows whose net_flow_usd is null (unpriced); they are not in the USD sums</div>
</div>
<div class="api-param" markdown>
<div class="api-param__head" markdown="span">`next_cursor` <span class="api-param__type">string</span> <span class="api-req">required</span></div>
<div class="api-param__desc" markdown="span">Pass as `cursor` to read the next page; null on the last page</div>
</div>
</div>

</div>
<div class="api-code" markdown>

=== "Shell"

    ```bash
    curl -X GET "$BASE/api/v2/portfolio/flows?accounts=string" \
      -H "X-API-KEY: $API_KEY" -H "X-TIMESTAMP: $ts" \
      -H "X-SIGN: $sign"
    ```

=== "Python"

    ```python
    params = {"accounts": ..., "startMs": ..., "endMs": ..., "types": ..., "external": ..., "limit": ..., "cursor": ...}
    r = requests.get(f"{BASE}/api/v2/portfolio/flows",
        params=params
        headers=signed_headers_query(params))
    print(r.json())
    ```

=== "Rust"

    ```rust
    let resp = client
        .get(format!("{BASE}/api/v2/portfolio/flows?accounts=string"))
        .headers(signed_headers_query(&params))
        .send()?;
    println!("{}", resp.text()?);
    ```

=== "Go"

    ```go
    req, _ := http.NewRequest("GET", BASE+"/api/v2/portfolio/flows?accounts=string", nil)
    for k, v := range signedHeadersQuery(req.URL.Query()) {
    	req.Header.Set(k, v)
    }
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
    	log.Fatal(err)
    }
    defer resp.Body.Close()
    io.Copy(os.Stdout, resp.Body)
    ```

=== "Java"

    ```java
    HttpResponse<String> resp = client.send(
        signedGet("/api/v2/portfolio/flows?accounts=string"),
        HttpResponse.BodyHandlers.ofString());
    ```

=== "TypeScript"

    ```typescript
    const res = await fetch(`${BASE}/api/v2/portfolio/flows?accounts=string`, {
      method: "GET",
      headers: signedHeadersQuery(params),
    });
    const data: PortfolioFlowsResponse = await res.json();
    ```

=== "Kotlin"

    ```kotlin
    val request = signedGet("/api/v2/portfolio/flows?accounts=string")
    val response = client.send(request, HttpResponse.BodyHandlers.ofString())
    println(response.body())
    ```

=== "C++"

    ```cpp
    cpr::Response r = cpr::Get(
        cpr::Url{BASE + "/api/v2/portfolio/flows?accounts=string"},
        signedHeadersQuery(params));
    std::cout << r.text << std::endl;
    ```

=== "JavaScript"

    ```javascript
    const res = await fetch(`${BASE}/api/v2/portfolio/flows?accounts=string`, {
      method: "GET",
      headers: signedHeadersQuery(params),
    });
    console.log(await res.json());
    ```

<div class="api-response" markdown>

**Response** <span class="api-status api-status--ok">200</span>

```json
{
  "from_ms": 0,
  "to_ms": 0,
  "flows": [
    {
      "id": "string",
      "time": 0,
      "exchange": "string",
      "subaccount": "string",
      "type": "deposit",
      "direction": "in",
      "external": true,
      "asset": "string",
      "qty": 0.0,
      "usd_value": 0.0,
      "net_flow_usd": 0.0,
      "counted_in_pnl": true,
      "source": "string"
    }
  ],
  "totals": {
    "deposits_usd": 0.0,
    "withdrawals_usd": 0.0,
    "net_external_usd": 0.0,
    "net_internal_usd": 0.0,
    "unpriced_count": 0
  },
  "next_cursor": "string"
}
```

</div>
</div>
</div>
