Your Ledger should track transactions made by your product. To do this, you need to represent every account you hold at external financial systems in your Ledger.
This section covers the second of two steps to record transactions in your Ledger:
- Sync transactions from external financial systems into FRAGMENT using Links.
- Once synced, you can record them in your Ledger using reconcileTx.
a. Linked Ledger Accounts
#To represent external accounts in your Ledger, create a Ledger Account and link it to an External Account.
Any Ledger Line that posts to a linked Ledger Account must match a synced transaction in its corresponding External Account.
To link a Ledger Account to an External Account, set linkedAccount on a Ledger Account in your Schema:
{
"chartOfAccounts": {
"accounts": [
{
"key": "assets-root",
"name": "Assets",
"type": "asset",
"children": [{
"key": "operating",
"name": "Operating Bank",
"linkedAccount": {
"linkId": "some-link-id",
"externalId": "bank-account-1"
}
}]
}
]
}
}The linkId comes from the Link you create in the dashboard. The externalId is the ID of the account at your external financial system.
The CLI automatically replaces variables with ${ENV_VAR} syntax when running fragment store-schema. This lets you use different External Accounts per environment:
{
"chartOfAccounts": {
"accounts": [
{
"key": "assets-root",
"name": "Assets",
"type": "asset",
"children": [{
"key": "operating",
"name": "Operating Bank",
"linkedAccount": {
"linkId": "${BANK_LINK_ID}",
"externalId": "${BANK_ACCOUNT_ID}"
}
}]
}
]
}
}To link a Ledger Account template to an External Account, parameterize the linkedAccount field:
{
"chartOfAccounts": {
"accounts": [
{
"key": "assets-root",
"name": "Assets",
"type": "asset",
"children": [{
"key": "operating",
"name": "Operating Bank",
"template": true,
"linkedAccount": {
"linkId": "{{BANK_LINK_ID}}",
"externalId": "{{BANK_ACCOUNT_ID}}"
}
}]
}
]
}
}These parameters will be required when posting a Ledger Entry to a linked Ledger Account. This can be useful if you're creating bank accounts per customer, for example.
b. Reconcile a Tx
#To reconcile a transaction from an external system, follow the same two-step process as when posting Ledger Entries:
- Define the structure of the Ledger Entry in your Schema.
- Post the Ledger Entry using the API.
Ledger Entries posting to a linked Ledger Account must specify the Tx from the External Account tied to the Ledger Account. This lets FRAGMENT know which transaction to reconcile:
{
"key": "schema-key",
"chartOfAccounts": {...},
"ledgerEntries": {
"types": [
{
"type": "user_funds_account_via_link",
"description": "Funding {{user_id}} for {{funding_amount}}",
"lines": [
{
"account": { "path": "assets/operating" },
"key": "funds_arrive_in_bank",
"tx": {
"externalId": "{{bank_transaction_id}}"
}
},
{
"account": { "path": "liabilities/users:{{user_id}}/available" },
"key": "increase_user_balance"
}
]
}
]
}
}Notes:
- Ledger Lines posting to a linked Ledger Account must match 1:1 with the Tx that's being reconciled.
bank_transaction_idrepresents the ID of the transaction at the external system.
Instead of calling addLedgerEntry, use the reconcileTx mutation:
mutation ReconcileTx(
$entry: LedgerEntryInput!
) {
reconcileTx(
entry: $entry
) {
... on ReconcileTxResult {
entry {
type
created
posted
}
lines {
amount
key
description
account {
path
}
}
}
... on Error {
code
message
}
}
}The parameters look similar to addLedgerEntry. Specify the Ledger Entry type that you are using and provide the parameters defined in the Schema:
{
"entry": {
"type": "user_funding",
"ledger": {
"ik": "quickstart-ledger"
},
"parameters": {
"txId": "tx_12345",
"customerId": "customer-1"
}
}
}ik and posted are optional when posting Ledger Entries with reconcileTx:
ik: theTx.externalIdis used to ensure that reconciling a transaction is idempotentposted: the timestamp of the Ledger Entry is taken from the Tx to ensure the Ledger mirrors the external system
c. Reconciling multiple Txs
#Book transfers are a common type of money movement which produce two Txs at your bank as part of one transaction.
To reconcile multiple Txs using reconcileTx:
{
"key": "schema-key",
"chartOfAccounts": {...},
"ledgerEntries": {
"types": [
{
"type": "user_funds_account_via_link",
"description": "Funding {{user_id}} for {{funding_amount}}",
"lines": [
{
"key": "funds_arrive_in_operating_bank",
"account": { "path": "assets/operating-bank-account" },
"tx": {
"externalId": "{{bank_transaction_id}}"
}
},
{
"key": "funds_leave_holding_bank",
"account": { "path": "assets/holding-bank-account" },
"tx": {
"externalId": "{{bank_transaction_id}}"
}
}
]
}
]
}
}Notes:
- Reconciling multiple Txs is only permitted if they have the same
postedtimestamp. - You can only reconcile multiple Txs within the same Link.
d. Unreconciled Txs
#Transactions synced to FRAGMENT but not reconciled to a Ledger are considered unreconciled.
You can query unreconciled transactions in a linked Ledger Account using the unreconciledTxs field on theLedgerAccount.
query GetUnreconciledTxs (
$ledgerAccount: LedgerAccountMatchInput!
) {
ledgerAccount(ledgerAccount: $ledgerAccount) {
id
unreconciledTxs {
nodes {
id
description
amount
externalId
externalAccountId
}
pageInfo {
endCursor
hasNextPage
}
}
}
}The variables specify the path of the linked Ledger Account you are querying and the Ledger it belongs to.
{
"ledgerAccount": {
"path": "assets/operating",
"ledger": {...}
}
}unreconciledTxs is eventually consistent, so if you've recently transacted at a Native Link or just synced manually for a Custom Link, you may not see the transaction in the query results immediately.
e. Clearing accounts
#Clearing accounts track temporary balances that go to zero when a workflow completes. Set clearing: true on a Ledger Account in your Schema. This setting is immutable once the Ledger is deployed. For example, add this account to chartOfAccounts.accounts:
{
"key": "receivables",
"type": "asset",
"template": true,
"clearing": true
}Use template: true to create a separate clearing account for each workflow. Supply a workflow or transaction ID in the account path when posting an entry:
{
"account": {
"path": "receivables:{{workflow_id}}"
},
"amount": "{{amount}}"
}The clearingStatus field on LedgerAccount indicates whether the account has an outstanding balance:
pending: At least one currency has a non-zero balance.cleared: All currencies have a zero balance.null: The account is not configured as a clearing account.
Use the clearingStatus filter to list accounts with either status. Status reads and filters are eventually consistent, even when balances are strongly consistent:
query GetClearingAccount($ledgerAccount: LedgerAccountMatchInput!) {
ledgerAccount(ledgerAccount: $ledgerAccount) {
path
clearingStatus
postedWindow {
earliest
latest
}
}
}The postedWindow field describes the range of Ledger Line posted timestamps across all currencies in the account:
earliest: The earliestpostedtimestamp.latest: The latestpostedtimestamp.
postedWindow is null for accounts without entries and accounts not configured for clearing. Reads and filters on these timestamps are eventually consistent. Combine clearingStatus with timestamp filters to find accounts that need investigation:
earliestPostedfilters by the earliest posting time.latestPostedfilters by the latest posting time, such as accounts with no recent activity.- Both timestamp filters require a
clearingStatusfilter in the same query.
f. Get a reconciliation report
#Use ledger.reconciliationReport to count pending clearing accounts and unreconciled transactions by age. Counts are eventually consistent. Age is measured from the time of the request, returned as the report's endTime. Configure the report with:
granularity: Usehourly,daily, ormonthly. Monthly intervals use calendar months.intervals: Request 1–7 intervals, including the open-ended oldest interval.clearingAccounts: Pass{}for all pending accounts, or restrict them withfilter.path.unreconciledTxs: Providelinksand optionally restrict transactions withfilter.posted. Requireslink.read.
This query requests both counts. Transactions are counted when they have not been reconciled to any account in this Ledger. A transaction reconciled to a different Ledger can therefore still count as unreconciled in this report. The selected Links can include External Accounts that are not linked to this Ledger:
query GetReconciliationReport($ledger: LedgerMatchInput!, $links: [LinkMatchInput!]!) {
ledger(ledger: $ledger) {
reconciliationReport(
granularity: daily
intervals: 3
clearingAccounts: {}
unreconciledTxs: { links: $links }
) {
endTime
intervals {
startTime
endTime
clearingAccountCount
unreconciledTxCount
}
}
}
}Choose the Ledger and Links to include. Provide 1–100 Link selectors, using a FRAGMENT ID or a Custom Link's creation IK:
{
"ledger": { "ik": "quickstart-ledger" },
"links": [{ "id": "some-link-id" }]
}Intervals are returned youngest first. With daily and intervals: 3, the age ranges are:
- Up to 24 hours.
- More than 24 hours, up to 48 hours.
- More than 48 hours.
startTimeisnull.
Omit either input to skip that count; the corresponding count field returns null. Clearing accounts are counted once per interval. Different pending currencies can place the same account in multiple intervals. To count only clearing accounts for a specific workflow, use a path filter:
query GetReceivablesReport($ledger: LedgerMatchInput!) {
ledger(ledger: $ledger) {
reconciliationReport(
granularity: daily
intervals: 3
clearingAccounts: { filter: { path: { matches: "receivables:*" } } }
) {
intervals {
startTime
endTime
clearingAccountCount
}
}
}
}Use an interval's startTime and endTime directly as inclusive bounds in the corresponding list query. Omit after when startTime is null:
- Pending clearing accounts: query
ledger.ledgerAccounts. Filter byclearingStatus: { equalTo: pending }andearliestPosted: { after: startTime, before: endTime }. - Unreconciled transactions: query
ledger.unreconciledTxswithposted: { after: startTime, before: endTime }.
Preserve the report's scope when retrieving records:
- Reuse
clearingAccounts.filter.pathasfilter.pathin the clearing-status query, if provided. - Pass the same
linkstoledger.unreconciledTxs. - Intersect any original
unreconciledTxs.filter.postedbounds with the interval bounds. - Use pagination to retrieve every record.