> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lerian.studio/llms.txt
> Use this file to discover all available pages before exploring further.

# Complete an account statement with facts

> Read the entries of an account from Midaz, then ask System Facts what happened to each transaction: the Pix that settled it, its parties and its end-to-end id.

An account statement starts in the ledger. [Build an account statement](https://docs.lerian.studio/en/products/midaz/building-account-statements.md) shows how to read the operations of an account from Midaz. This guide adds one step: for each transaction of the statement, you ask System Facts for the facts of that transaction.

The ledger stays the source of the statement. Facts add detail to a row. A row without facts is still a correct row.

## Before you start

***

* Your deployment runs System Facts with its worker, and Midaz and Pix JD publish their events. See [Where facts come from](https://docs.lerian.studio/en/platform/system-facts/what-is-system-facts.md#where-facts-come-from).
* Your application has an application credential for System Facts. See [Machine-to-machine access](https://docs.lerian.studio/en/platform/access-manager/using-access-manager.md#machine-to-machine-access).

## Which transactions have facts

***

System Facts records a Midaz fact when a transaction reaches `APPROVED`. The fact type is `midaz.transaction_committed`.

| Transaction in Midaz | Fact in System Facts |
| :- | :- |
| Created as `APPROVED`, not as a revert | One fact with `action: direct`. |
| `PENDING`, then committed | No fact while the transaction is pending. One fact with `action: commit` after the commit. |
| `PENDING`, then canceled | No Midaz fact. |
| Revert of a transaction | The revert is a new transaction, and its fact sits under the id of that new transaction. See [Reverted transactions](#reverted-transactions). |
| `NOTED` | No fact. |

System Facts records the fact after Midaz publishes the event of the transaction, so a lookup made right after the transaction can find nothing yet. A transaction whose event Midaz did not publish has no fact, for example a transaction posted while event publication was off.

## Find the facts of each transaction

***

**1. Read the operations of the account**

Use [List Operations by Account](https://docs.lerian.studio/en/reference/products/midaz/v2/get-all-operations-by-account.md) to read the statement period. Each operation carries `transactionId`, `organizationId` and `ledgerId`.

One transaction can write more than one operation on the same account. Collect each `transactionId` once.

**2. Find the flow of each transaction**

Call [Find the flow of a reference](https://docs.lerian.studio/en/reference/platform/system-facts/find-the-flow-of-a-reference.md) with the reference type `lerian.midaz.transaction_id`. Send the `organizationId` and the `ledgerId` of the operation as scope keys.

```bash theme={null}
curl -G "$SYSTEM_FACTS_URL/v1/flows/by-reference" \
  -H "Authorization: Bearer $SYSTEM_FACTS_BEARER_TOKEN" \
  --data-urlencode "referenceType=lerian.midaz.transaction_id" \
  --data-urlencode "referenceValue=0199d2b3-0f1e-7d2c-9b3a-4c5d6e7f8a02" \
  --data-urlencode "organizationId=0199c1a2-4b3d-7e5f-8a61-2b3c4d5e6f70" \
  --data-urlencode "ledgerId=0199c1a2-5c4e-7f60-9b72-3c4d5e6f7a81"
```

Follow these rules:

* Send the transaction id exactly as Midaz returns it. System Facts compares the value character by character, and an upper-case copy of the id does not match.
* Send both scope keys on every lookup.
* Send no query parameter other than the two scope keys and the parameters that the operation lists. System Facts reads an unknown parameter as a scope key, and the lookup then answers `404`.

**3. Read the facts of the flow**

The response is the flow that holds the facts of the transaction. `facts.items` lists the facts in the order of `occurredAt`.

This example is a Pix received through Pix JD. The flow holds the Midaz fact and the Pix JD fact.

```json theme={null}
{
  "flowAggregationId": "0199d2b3-2c4d-7e5f-9a6b-7c8d9e0f1a03",
  "createdAt": "2026-05-18T14:23:12Z",
  "updatedAt": "2026-05-18T14:23:12Z",
  "facts": {
    "items": [
      {
        "factId": "6f1d2c3b-4a59-5e68-9b7a-0c1d2e3f4a5b",
        "producer": "midaz",
        "namespace": "lerian.midaz.transactions",
        "type": "midaz.transaction_committed",
        "schemaVersion": "1.0.0",
        "occurredAt": "2026-05-18T14:23:11Z",
        "subject": {
          "type": "midaz.transaction",
          "id": "0199d2b3-0f1e-7d2c-9b3a-4c5d6e7f8a02"
        },
        "references": [
          {
            "type": "lerian.midaz.transaction_id",
            "value": "0199d2b3-0f1e-7d2c-9b3a-4c5d6e7f8a02",
            "scope": {
              "organizationId": "0199c1a2-4b3d-7e5f-8a61-2b3c4d5e6f70",
              "ledgerId": "0199c1a2-5c4e-7f60-9b72-3c4d5e6f7a81"
            }
          }
        ],
        "data": {
          "action": "direct",
          "status": "APPROVED",
          "direction": "INFLOW",
          "amount": "150",
          "assetCode": "BRL",
          "description": "Pix received",
          "createdAt": "2026-05-18T14:23:11Z",
          "operations": [
            {
              "id": "0199d2b3-1a2b-7c3d-8e4f-5a6b7c8d9e01",
              "type": "DEBIT",
              "amount": "150",
              "assetCode": "BRL",
              "accountId": "0199c1a2-6d5f-7071-ac83-4d5e6f7a8be1",
              "external": true,
              "feeLeg": false
            },
            {
              "id": "0199d2b3-1a2b-7c3d-8e4f-5a6b7c8d9e02",
              "type": "CREDIT",
              "amount": "150",
              "assetCode": "BRL",
              "accountId": "0199c1a2-6d5f-7071-ac83-4d5e6f7a8b92",
              "external": false,
              "feeLeg": false
            }
          ]
        },
        "protectedFields": [
          {
            "path": "/description",
            "sensitivity": "encrypted",
            "protected": false
          }
        ],
        "ingestedAt": "2026-05-18T14:23:12Z"
      },
      {
        "factId": "a3b4c5d6-e7f8-5a9b-8c0d-1e2f3a4b5c6d",
        "producer": "plugin-br-pix-jd",
        "namespace": "lerian.pix.jd",
        "type": "pix.transaction_settled",
        "schemaVersion": "2.0.0",
        "occurredAt": "2026-05-18T14:23:12Z",
        "subject": {
          "type": "pix.transaction",
          "id": "7c9e2f41-3b5a-5d8c-9e0f-1a2b3c4d5e6f"
        },
        "references": [
          {
            "type": "lerian.pix.jd.end_to_end_id",
            "value": "E87654321202605181423abcdef12345",
            "scope": {}
          },
          {
            "type": "lerian.midaz.transaction_id",
            "value": "0199d2b3-0f1e-7d2c-9b3a-4c5d6e7f8a02",
            "scope": {
              "organizationId": "0199c1a2-4b3d-7e5f-8a61-2b3c4d5e6f70",
              "ledgerId": "0199c1a2-5c4e-7f60-9b72-3c4d5e6f7a81"
            }
          }
        ],
        "data": {
          "transactionId": "7c9e2f41-3b5a-5d8c-9e0f-1a2b3c4d5e6f",
          "endToEndId": "E87654321202605181423abcdef12345",
          "transactionType": "CASH_IN",
          "amount": "150.00",
          "currency": "BRL",
          "createdAt": "2026-05-18T14:23:10Z",
          "payer": {
            "name": "Joao Exemplo Pereira",
            "document": "***.654.321-**",
            "personType": "PERSON",
            "ispb": "87654321"
          }
        },
        "protectedFields": [
          {
            "path": "/payer/document",
            "sensitivity": "masked",
            "protected": true
          },
          {
            "path": "/payer/name",
            "sensitivity": "encrypted",
            "protected": false
          }
        ],
        "ingestedAt": "2026-05-18T14:23:13Z"
      }
    ],
    "limit": 100
  }
}
```

A flow can hold the facts of more than one Midaz transaction. Use only the facts that carry the id that you looked up in a `lerian.midaz.transaction_id` reference.

**4. Add the detail to the statement row**

Take from the facts the detail that the ledger operation does not carry.

| Statement detail | Fact | Field in `data` |
| :- | :- | :- |
| Money entered or left the ledger | `midaz.transaction_committed` | `direction`: `INFLOW`, `OUTFLOW`, `INTERNAL` or `MIXED` |
| Legs that Fees Engine added | `midaz.transaction_committed` | `operations[].feeLeg` |
| Pix end-to-end id | `pix.transaction_settled` | `endToEndId`. On a refund, `endToEndId` is the id of the original payment, and `refundEndToEndId`, when the fact carries it, is the id of the refund. |
| Kind of Pix transaction | `pix.transaction_settled` | `transactionType`: `CASH_IN`, `CASH_OUT`, `INTRA_PSP`, `REFUND_SENT`, `REFUND_RECEIVED` or `UNKNOWN` |
| Counterparty | `pix.transaction_settled` | `payer` for a Pix received, `payee` for a Pix sent |

The contract of each fact type defines the fields of its facts. Read it with [Read a contract version](https://docs.lerian.studio/en/reference/platform/system-facts/read-a-contract-version.md).

## Protected values in the facts

***

The contracts of these facts protect personal values.

| Fact | Encrypted fields | Masked fields |
| :- | :- | :- |
| `midaz.transaction_committed` | `description` | None |
| `pix.transaction_settled`, `pix.transaction_failed` | `description`, and the `name`, `account` and `key` of `payer` and `payee` | The `document` of `payer` and `payee` |

A caller that holds `facts:reveal` reads an encrypted field in clear form, and `protectedFields` then shows `protected: false` for that field. Any other caller reads a mask. A masked field never comes back in clear form.

An application credential created for System Facts holds `facts:reveal`. Your application therefore reads names, accounts and Pix keys in clear form. Handle the facts as personal data.

## When the lookup does not answer 200

***

| Answer | What it means | What to do |
| :- | :- | :- |
| `404` `SF-0010` | No fact carries this transaction id with this scope. | Show the row with the ledger data only. If you expected a fact, check the id and the two scope keys. If no transaction has a fact, check the conditions of [Before you start](#before-you-start). |
| `409` `SF-0011` | The lookup did not send the full scope, and more than one flow carries the id. | Send `organizationId` and `ledgerId`. The answer also lists the flows in `candidates[]`: read one with [Read a flow](https://docs.lerian.studio/en/reference/platform/system-facts/read-a-flow.md). |
| `429` `SF-0095` | The request is over the rate limit. | Wait for the `Retry-After` delay, then continue. |
| `503` | The service cannot answer now. | Retry later with the same request. |

A `404` does not mean that the transaction is missing from the ledger. See [Which transactions have facts](#which-transactions-have-facts).

The [error list](https://docs.lerian.studio/en/reference/platform/system-facts/system-facts-error-list.md) describes every code.

## Reverted transactions

***

A revert appears on the statement as the operations of a new transaction. Look up the id of that new transaction: the fact of the revert carries that id as its reference, not the id of the original transaction.

A fact with `action: revert` names the transaction that it reverts in `data.parentTransactionId`.

## Read fewer facts

***

* **Filter.** Send `type` or `namespace` to read only some facts of the flow, for example `type=pix.transaction_settled`. Each parameter takes a comma-separated list.
* **Page.** A page holds 100 facts by default, and `factsLimit` takes 1 to 500. When the response carries `facts.nextCursor`, send it as `factsCursor` with the same filters to read the next page.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.