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

# Matching rules

> Create reconciliation rules in the app or with POST /reconciliation_rules: conditions, tolerances, transforms and the order rules run in.

A matching rule tells End Close when a record on Side A and a record on Side B are the same transaction. Each rule is a set of conditions that compare a field on one side to a field on the other, such as "amount equals amount and invoice ID equals invoice ID", and End Close matches every pair of unmatched records that satisfies them.

This page covers one-to-one rules, where one record matches one record. To sum several records on one side against a single record on the other, such as charges against a payout, see [Rollup rules](https://endclose.com/docs/guides/rollup-rules.md).

## Prerequisites

* A reconciliation with data streams on Side A and Side B. See [Reconciliations](https://endclose.com/docs/concepts/reconciliations.md).
* Records on both sides, so you can see which fields they share.
* For the API, an [API key](https://endclose.com/docs/api-reference/authentication.md) for the environment and the reconciliation's id (`GET /reconciliations`).

## How rules match records

* **All conditions must hold.** A one-to-one rule matches a pair only when its whole condition tree is true. Conditions are combined with AND, and you can nest an OR group.
* **Rules run in priority order.** Lower `priority` values run first. A record matched by one rule is no longer available to the rules after it, so put your strictest rules first.
* **Each record matches once.** A record is in at most one match per reconciliation.
* **Two exact conditions, one of them on amount.** A one-to-one rule needs a condition on `amount` and at least two `eq` conditions without a tolerance. End Close refuses a rule that has fewer, because it would pair too many records.

### When rules run

On a continuous reconciliation, End Close matches each record against your one-to-one rules as it arrives, and saving an active rule starts a run over the records already there. On a batch reconciliation, rules run when you choose **Run** in the app or call `POST /reconciliations/{id}/run`. Rollup rules run separately; see [Rollup rules](https://endclose.com/docs/guides/rollup-rules.md#when-rollup-rules-run).

## Create a rule in the app

**1. Open the reconciliation's rules**

Go to **Reconciliations**, open the reconciliation and choose the **Rules** tab. A reconciliation with no rules offers **Generate rules**, which looks at your records and proposes rules for you to review.

**2. Start a one-to-one rule**

Choose **New rule**, then **One-to-one**.

**3. Add match conditions**

Under **Match conditions**, pick a Side A field, an operator and a Side B field for each condition. Add a tolerance to a condition when the values can differ slightly, for example a date that lands a day or two later on the bank side.

**4. Describe and save**

Give the rule an optional description, such as "Stripe charge to ledger entry by charge ID", and save it.

**5. Set the order**

If the reconciliation has more than one rule, choose **Reorder** and drag the rules into the order they should run.

## Create a rule with the API

Send the rule's condition tree as `filters`. In every condition, `key` names the Side A field and `value` names the Side B field to compare it with.

This rule matches a ledger entry (Side A) to a processor charge (Side B) when the amounts are equal, the ledger's `metadata.charge_id` equals the charge's `external_id`, and the dates are no more than two days apart:

```bash cURL theme={"dark"}
curl -X POST https://api.endclose.com/v1/reconciliation_rules \
  -H "X-API-Key: $ENDCLOSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "reconciliation_id": "'"$RECONCILIATION_ID"'",
    "description": "Ledger entry to charge by charge ID",
    "priority": 0,
    "filters": {
      "operand": "and",
      "rules": [
        { "key": "amount", "operand": "eq", "value": "amount" },
        {
          "key": "metadata.charge_id",
          "operand": "eq",
          "value": "external_id",
          "transforms": { "key": "lowercase", "value": "lowercase" }
        },
        {
          "key": "date",
          "operand": "eq",
          "value": "date",
          "tolerance_amount": 2,
          "tolerance_unit": "day"
        }
      ]
    }
  }'
```

The response is the saved rule with `"status": "active"` and `"source": "user"`. A rule that duplicates an existing active rule, or whose conditions are a subset of one, is refused with `422` and a message naming the rule it overlaps.

To change a rule, send only the fields you want to change to `PATCH /reconciliation_rules/{id}`. To list a reconciliation's active rules in the order they run, call `GET /reconciliation_rules?reconciliation_id=...`.

## Rule reference

### Fields you can compare

| Field | What it holds |
| - | - |
| `amount` | The record's amount, in cents |
| `date` | The record's date |
| `currency` | ISO currency code |
| `direction` | `credit` or `debit` |
| `description` | The record's description |
| `external_id` | The id the record had in its source system |
| `metadata.<key>` | Any metadata key on the record, for example `metadata.invoice_id` |

### Operators

| `operand` | Matches when |
| - | - |
| `eq` | The values are equal, or within the tolerance if one is set |
| `neq` | The values differ |
| `gt`, `gte`, `lt`, `lte` | The Side A value is greater than, at least, less than, or at most the Side B value |
| `contains` | The Side B value contains the Side A value (case-insensitive) |
| `in`, `not_in` | The Side B value is, or is not, in the Side A list |

### Tolerances

Tolerances apply to `eq` conditions. A condition with a tolerance doesn't count toward the two exact conditions a rule needs.

| `tolerance_unit` | Use on | Matches when |
| - | - | - |
| `dollar` | Amounts | The amounts are within `tolerance_amount` dollars of each other (`0.50` is 50 cents) |
| `percent` | Amounts | The Side B amount is within `tolerance_amount` percent of the Side A amount |
| `day` | Dates | The dates are within `tolerance_amount` days of each other |
| `month` | Dates | Both dates fall in the same calendar month (`tolerance_amount` is ignored) |

### Transforms

`transforms` normalizes values before they're compared. Set `key` for the Side A field and `value` for the Side B field. Each takes one transform, or an array applied in order with the first innermost.

| Transform | Effect |
| - | - |
| `lowercase`, `uppercase` | Changes the case |
| `alphanumeric_only` | Keeps letters and digits only |
| `digits_only` | Keeps digits only |
| `first_n_chars` | Keeps the first `n` characters: `{ "name": "first_n_chars", "args": { "n": 8 } }` |

### Different currencies

When two records are in different currencies, an `amount eq amount` condition without transforms compares them after converting both to USD at the rate on or before each record's date. Records in the same currency are compared as they are.

## FAQ

> [accordion] Why was my rule refused with a 422?
>
> The message says which check failed. The usual causes are a one-to-one rule with no condition on `amount`, fewer than two `eq` conditions without a tolerance, an unknown transform, or conditions that duplicate an existing active rule.

> [accordion] Why didn't a pair match when the rule looks right?
>
> Check whether an earlier rule already matched one of the records, since each record matches once and rules run in priority order. Then check the field values exactly: case, spaces and prefixes all count unless a transform removes them.

> [accordion] What are proposed rules?
>
> Rules that **Generate rules** suggests start as `proposed`, with a `confidence` and a `sample_match_rate` from a dry run on your records. They don't match anything until you accept them and they become `active`.

> [accordion] Can I match records by hand instead?
>
> Yes. Select the records in the reconciliation and choose **Reconcile**, or create the match with `POST /reconciliation_matches`. See [Rules and matches](https://endclose.com/docs/concepts/rules-and-matches.md).

## Related

> [card] [Rollup rules](https://endclose.com/docs/guides/rollup-rules.md)
>
> Sum several records on one side against one record on the other.

> [card] [Rules and matches](https://endclose.com/docs/concepts/rules-and-matches.md)
>
> How rules, matches and adjustments fit together.

> [card] [Review exceptions](https://endclose.com/docs/guides/review-exceptions.md)
>
> Work through the records your rules leave unmatched.

> [card] [Reconciliations](https://endclose.com/docs/concepts/reconciliations.md)
>
> Sides, data streams and how a reconciliation runs.


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