First Example
Let's configure a ledger to keep tracking of a simple checking account.
#!/usr/bin/env deno run -A
import {
BalancelynxService,
accountCreate,
ledgerCreate,
transactionCreate,
accountGetBalance,
commodityGet,
ledgerGetBaseCommodity,
OpenAPI,
} from "balancelynx";
// Set up the base URL for the OpenAPI, where server requests will be sent
OpenAPI.BASE = "http://localhost:8080";
// Create a new Ledger with a base commodity 'USD'.
// The precision is set to 2, meaning decimals are only counted up to 2 places (cents for USD).
const ldgr = await ledgerCreate({
base_commodity_name: "USD",
base_commodity_precision: 2,
});
const ldgr_id = ldgr.id;
const cmy_id = USD.id;
const USD = await ledgerGetBaseCommodity(ldgr_id);
const income = await accountCreate({ ldgr_id });
const checking = await accountCreate({ ldgr_id });
const expenses = await accountCreate({ ldgr_id });
await transactionCreate({
date: new Date().toISOString(),
desc: "I got paid $109",
postings: [
{ acct_id: income.id!, debit: 109, cmy_id },
{ acct_id: checking.id!, credit: 109, cmy_id },
],
});
await transactionCreate({
date: new Date().toISOString(),
desc: "Purchased something for $49",
postings: [
{ acct_id: checking.id!, debit: 49, cmy_id },
{ acct_id: expenses.id!, credit: 49, cmy_id },
],
});
const checking_balance = await accountGetBalance(checking.id!);
for (const { cmy_id, total } of checking_balance) {
const commodity = await commodityGet(cmy_id!);
console.log(`Your checking balance is ${total} ${commodity.name}`);
}