Let's start by adding an Account type, that define some properties listed in , and a a generic type Paginated<T> for paginated responses (see ).
For convenience, let's create a separated types.ts file where we'll define the types definition for the responses:
Click to show content of types.ts file
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:
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})`,
);
}
};