3. Fetch accounts balances

Now let's focus in listing all accounts in the workspace to retrieve their balances.

Types definition ​

Export Account & Paginated types ​

Let's start by adding an Account type, that define some properties listed in Account responsearrow-up-right, and a a generic type Paginated<T> for paginated responses (see Page-based paginationarrow-up-right).

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

chevron-rightClick to show content of types.ts filehashtag
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[];
};

Import types in the main script

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

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

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

async function main() {
  ...

Fetch accounts ​

Call the /accounts endpoint ​

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

Run the script ​

It should list the first page of accounts:

circle-info

Don't hesitate to tweak the output and make it yours, depending on which account information you need.

Loop in all pages ​

Adapt the script to pagination ​

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

Verify that it works ​

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

Last updated