# Integrity

## Check Integrity

Validates the row-level integrity of all data across accounts, journal entries, ledger, and ledger archive. This is an audit endpoint — use it periodically or on demand.

```
GET /integrity
```

### Response — `200 OK`

```json
{
  "account": {
    "total": 1000,
    "valid": 1000,
    "invalid": 0,
    "violations": []
  },
  "journal": {
    "total": 5000,
    "valid": 5000,
    "invalid": 0,
    "violations": []
  },
  "ledger": {
    "total": 10000,
    "valid": 10000,
    "invalid": 0,
    "violations": []
  },
  "ledger_archive": {
    "total": 384,
    "valid": 384,
    "invalid": 0,
    "violations": []
  }
}
```

### What Gets Validated

The integrity check performs the following validations in order:

| # | Entity         | Validation                                                                                  |
| - | -------------- | ------------------------------------------------------------------------------------------- |
| 1 | Account        | Row hash verification — detects field alteration                                            |
| 2 | Journal Entry  | Row hash verification — detects field alteration                                            |
| 3 | Journal Entry  | Referenced accounts must exist — detects account deletion                                   |
| 4 | Ledger Archive | Row hash verification — detects field or data alteration                                    |
| 5 | Ledger         | Referenced accounts must exist — detects account deletion                                   |
| 6 | Ledger         | Referenced journal entries must exist — detects journal entry deletion                      |
| 7 | Ledger         | All fields must match journal entry data (including timestamps) — detects ledger alteration |

### Violations Response

When violations are found, the `violations` array contains the IDs of the affected rows:

```json
{
  "account": {
    "total": 1000,
    "valid": 998,
    "invalid": 2,
    "violations": [
      "019702a0-1234-7abc-8def-000000000001",
      "019702a0-1234-7abc-8def-000000000002"
    ]
  }
}
```

### Performance

* Runs on the **read replica** — no impact on write performance
* Processes data in batches of 5,000 rows
* Not a hot-path endpoint — designed for periodic auditing

### Use Cases

* **Periodic audit** — run daily or weekly to verify data integrity
* **Post-incident validation** — verify system health after issues
* **Pre-archive check** — the archive endpoint runs this automatically; you can also run it manually beforehand
* **Tamper detection** — identify rows that were modified directly in the database

### Example

```bash
curl http://<host>:8080/integrity \
  -H "X-Api-Key: ak_..." \
  -H "X-Api-Secret: sk_..."
```

{% hint style="warning" %}
If the integrity check detects violations in any account, transactions involving those accounts will be rejected with `INTEGRITY_VIOLATION` (409) until the issue is resolved.
{% endhint %}


---

# 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://entrytarget.gitbook.io/docs/api-reference/integrity.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.
