# Get CAPS balances

CAPS is the Ternoa blockchain token. Transactions made on the Ternoa blockchain are carried out in CAPS. It is used in particular for the creation of NFTs, Encryption, and storage of data over time.

### Prerequisites

Before getting started, make sure you have the following ready:

1. [Create a Ternoa](/getting-started/wallets/ternoa-wallet.md) account with [Alphanet CAPS](broken://pages/ej9gceaLIhCN2PgUBh42)
2. Install and set up your editor of choice (we will use Visual Studio Code \[VSC] in this tutorial)
3. Install [NodeJS v.14+](https://nodejs.org/en/download/) & NPM
4. [Install & initialize Ternoa-JS](/build-1/javascript/ternoa-js-library-utilities/installation-and-initialization.md)

### How to get the total CAPS balance of an address

This example shows how to get the total CAPS balance of an address using `getTotalBalance`. The total balance corresponds to the sum of the free balance and the reserve balance. The `balanceToNumber` helper is used to format balances from a BN (big number) to a humanized value.

```typescript
import { balanceToNumber, getTotalBalance } from "ternoa-js";

// Public address of the known account we want to use
const ADDRESS = "5Cf8PBw7QiRFNPBTnUoks9Hvkzn8av1qfcgMtSppJvjYcxp6";

const main = async () => {
	try {
		const totalBalanceBN = await getTotalBalance(ADDRESS);
		const totalBalance = balanceToNumber(totalBalanceBN);
		console.log(`The total balance of ${ADDRESS} is:`, totalBalance);
	} catch (e) {
		console.error(e);
	}
};

main();
```

### How to get the transferrable CAPS balance of an address

This example shows how to get the transferrable CAPS balance of an address using `getTransferrableBalance`. The transferrable balance corresponds to the liquid balance of an account; we do not take into account the staked balance nor the balance locked on the governance. The `balanceToNumber` helper is used to format balances from a BN (big number) to a humanized value.

```typescript
import { balanceToNumber, getTransferrableBalance } from "ternoa-js";

// Public address of the known account we want to use
const ADDRESS = "5Cf8PBw7QiRFNPBTnUoks9Hvkzn8av1qfcgMtSppJvjYcxp6";

const main = async () => {
	try {
		const transferrableBalanceBN = await getTransferrableBalance(ADDRESS);
		const transferrableBalance = balanceToNumber(transferrableBalanceBN);
		console.log(
			`The transferrable balance of ${ADDRESS} is:`,
			transferrableBalance
		);
	} catch (e) {
		console.error(e);
	}
};

main();
```

### How to subscribe to CAPS balance changes

This example shows how to retrieve balance updates.

```typescript
import { balanceToNumber, getBalances, getRawApi } from "ternoa-js";

const ADDRESS = "5Cf8PBw7QiRFNPBTnUoks9Hvkzn8av1qfcgMtSppJvjYcxp6";

const main = async () => {
	try {
		const api = getRawApi();

		// Retrieve the initial balance.
		let previousFree = (await getBalances(ADDRESS)).free;
		console.log(`Initial balance: ${balanceToNumber(previousFree)}`);

		// Here we subscribe to any balance changes and update the on-screen value
		api.query.system.account(
			ADDRESS,
			({ data: { free: currentFree } }: any) => {
				// Calculate the delta
				const change = currentFree.sub(previousFree);

				// Only display positive value changes (Since we are pulling `previous` above already,
				// the initial balance change will also be zero)
				if (!change.isZero()) {
					console.log(`New balance: ${balanceToNumber(currentFree)}`);
					console.log(`Change of ${balanceToNumber(change)}`);

					previousFree = currentFree;
				}
			}
		);
	} catch (e) {
		console.error(e);
	}
};

main();
```

### Support

If you face any trouble, feel free to reach out to our community engineers in our [Discord](https://discord.gg/fUmBkPpnRu).


---

# 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://docs.ternoa.network/build-1/javascript/nft-features-and-pallets/tokens/caps/get-caps-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.
