# 3. Fetch accounts balances

### Types definition ​ <a href="#types-definition" id="types-definition"></a>

#### Export Account & Paginated types ​ <a href="#export-account-paginated-types" id="export-account-paginated-types"></a>

Let's start by adding an `Account` type, that define some properties listed in [Account response](about:/revault/reference/accounts-findById#response-schema), and a a generic type `Paginated<T>` for paginated responses (see [Page-based pagination](about:/revault/docs/overview#page-based-pagination)).

For convenience, let's create a separated `types.ts` file where we'll define the types definition for the responses:

<details>

<summary>Click to show content of <code>types.ts</code> file</summary>

```tsconfig
export type Account = {
  // unique identifier
  id: string;

  // account name
  name: string;

  // account currency (see GET /assets/currencies)
  currency: string;

  // account index
  index: number;

  balance: {
    // total balance on blockchain
    total: string;

    // pending balance on Vault (e.g: pending transactions)
    pending: string;
  };
};

export type Paginated<T> = {
  // the current page
  page: number;
  // the next page (or null if there is none)
  next: number | null;
  // the previous page (or null if there is none)
  prev: number | null;
  // the max count of items per page
  pageSize: number;
  // the total count of items
  total: number;
  // the page data
  results: T[];
};
```

</details>

#### Import types in the main script [​](broken://pages/SoeRJn8Xvm2VYOeQvoZV) <a href="#import-types-in-the-main-script" id="import-types-in-the-main-script"></a>

From our `main.ts` file, let's import the newly created types:

{% tabs %}
{% tab title="main.ts" %}

```tsconfig
import "dotenv/config";
import axios from "axios";

import { Account, Paginated } from "./types"; 

async function main() {
  ...
```

{% endtab %}
{% endtabs %}

### Fetch accounts ​ <a href="#fetch-accounts" id="fetch-accounts"></a>

#### Call the `/accounts` endpoint ​ <a href="#call-the-accounts-endpoint" id="call-the-accounts-endpoint"></a>

Update the `main.ts` script again to add a call to `/accounts`:

{% tabs %}
{% tab title="main.ts" %}

```tsconfig
  console.log(`Successfully logged as ${user.name}`);

  const accountsResponse = await client.get<Paginated<Account>>("/accounts"); 
  for (const account of accountsResponse.data.results) { 
    console.log( 
      `${account.name} (${account.currency}#${account.index}): ${account.balance.total} (pending: ${account.balance.pending})`, 
    ); 
  } 
};
```

{% endtab %}
{% endtabs %}

#### Run the script ​ <a href="#run-the-script" id="run-the-script"></a>

```bash
npm run dev
```

It should list the first page of accounts:

```
> vault-bot@1.0.0 dev /tmp/vault-bot
> tsx main.ts

Successfully logged as BobAPI
holesky0 (ethereum_holesky#0): 48199096638237596474 (pending: 0)
cold storage (ethereum#1): 1579746080436000 (pending: 0)
```

{% hint style="info" %}
Don't hesitate to tweak the output and make it yours, depending on which account information you need.
{% endhint %}

### Loop in all pages ​ <a href="#loop-in-all-pages" id="loop-in-all-pages"></a>

#### Adapt the script to pagination ​ <a href="#adapt-the-script-to-pagination" id="adapt-the-script-to-pagination"></a>

There is a little problem in our script: it only fetches the first page of results (maximum of 30 items by page).

Let's update our script so it will loop until there is no more next page:

main.ts

ts

{% tabs %}
{% tab title="main.ts" %}

```tsconfig
  //delete this section//
  console.log(`Successfully logged as ${user.name}`);

  const accountsResponse = await client.get<Paginated<Account>>("/accounts"); 
  for (const account of accountsResponse.data.results) { 
    console.log( 
      `${account.name} (${account.currency}#${account.index}): ${account.balance.total} (pending: ${account.balance.pending})`, 
    ); 
  } 
  // start with page 1 //
  let page: number | null = 1; 

  while (page !== null) { 
    const endpoint: string = `/accounts?page=${page}`; 
    const accountsResponse = await client.get<Paginated<Account>>(endpoint); 
    for (const account of accountsResponse.data.results) { 
      console.log( 
        `${account.name} (${account.currency}#${account.index}): ${account.balance.total} (pending: ${account.balance.pending})`, 
      ); 
    } 
    // point to next page (or `null` if there is none) //
    page = accountsResponse.data.next; 
  } 
};
```

{% endtab %}
{% endtabs %}

#### Verify that it works ​ <a href="#verify-that-it-works" id="verify-that-it-works"></a>

```bash
npm run dev
```

It should looks in all pages (you might not see any difference if you have less than `30` accounts):

```
> vault-bot@1.0.0 dev
> tsx main.ts

Successfully logged as BobAPI
holesky0 (ethereum_holesky#0): 48198749137607615474 (pending: 0)
cold storage (ethereum#1): 1579746080436000 (pending: 0)
```


---

# Agent Instructions: 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:

```
GET https://help.enterprise.ledger.com/api-documentation-v2/guides/balance-reporting-bot/3.-fetch-accounts-balances.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
