Mercury: Create a categorization rule
Mercury books.createBooksCategorizationRule: arguments, response, policy and SDK examples.
← Books operations · Get started
Operation mercury.createbookscategorizationrule
Provider route: POST /books/rules. Alter supplies the connection’s credentials and routing.
Call through Alter
Section titled “Call through Alter”Install @alter-ai/sdk @alter-ai/mercury with npm, or alter-sdk-mercury with pip. Supply your connected customer’s grant ID and the arguments described below.
import { App } from "@alter-ai/sdk";import { MercuryIntegration } from "@alter-ai/mercury";
async function execute(input: Parameters<MercuryIntegration["books"]["createBooksCategorizationRule"]>[0]) { const app = new App({ apiKey: process.env.ALTER_API_KEY! }); try { const provider = new MercuryIntegration(app, { grantId: process.env.PROVIDER_GRANT_ID! }); const call = await provider.books.createBooksCategorizationRule(input); const result = await call.wait(); return result.bodyJson(); } finally { await app.close(); }}import osfrom alter_sdk import Appfrom alter_sdk_mercury import MercuryIntegration
async def execute(arguments): async with App(api_key=os.environ["ALTER_API_KEY"]) as app: provider = MercuryIntegration(app, {"grant_id": os.environ["PROVIDER_GRANT_ID"]}) call = await provider.books.create_books_categorization_rule(arguments) result = await call.wait() return result.body_json()| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | object | Root | Additional keys: not allowed |
query | object | Optional | Additional keys: not allowed |
query.booksId | string | Optional | The Books instance to act on. Required for agent organization tokens; defaults to the token organization’s instance. Format: uuid |
body | Model | Optional | Expand Create a categorization rule — request body — body |
Referenced models: Create a categorization rule — request body — body
Response
Section titled “Response”Response media type: application/json. The table describes the pinned successful response. Referenced models link to their complete fields.
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | Model | Root | Expand Create a categorization rule — response body |
Referenced models: Create a categorization rule — response body
Schemas
Section titled “Schemas”18 more schemas are linked from the tables above, on the schema pages.
Create a categorization rule — response body
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | object | Root | The created categorization rule. |
rule | JSON | Required within parent | |
rule (allOf 1) | Model | All variants apply | Expand List categorization rules — response body — rules item |
rule (allOf 2) | JSON | All variants apply | The created rule, as the list endpoint returns it. |
Referenced models: List categorization rules — response body — rules item
Create a categorization rule — request body — body
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | object | Root | A categorization rule to create. There is no priority field: Books places the new rule among the existing ones by how specific its conditions are. Additional keys: not allowed |
conditions | array | Required within parent | The conditions a transaction must match, from 1 to 10. Every condition must match (logical AND); there is no OR, so an "any of" rule is written as separate rules. The organization may not already have a rule with the same conditions. Minimum items: 1 |
conditions[] | Model | Each item | Expand Create a categorization rule — request body — body conditions item |
outputs | array | Required within parent | What the rule applies to a matching transaction: at most one output of each kind, so today exactly one `setLedger`. Its ledger must be one the caller may edit in this Books instance. Minimum items: 1 |
outputs[] | Model | Each item | Expand Create a categorization rule — request body — body outputs item |
Referenced models: Create a categorization rule — request body — body conditions item, Create a categorization rule — request body — body outputs item
List categorization rules — response body — rules item
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | object | Root | A categorization rule: when a transaction matches all its `conditions`, Books applies its `outputs`, such as categorizing the transaction to a ledger. Only rules that Books can represent are listed: a rule with a condition or output outside this vocabulary (for example, an accounting-integration rule that sets a Mercury custom category), or whose GL account no longer maps to a Books ledger, is omitted. |
conditions | array | Required within parent | The conditions a transaction must match. At least one; every condition must match (logical AND). There is no OR: an "any of" rule is written as separate rules. Minimum items: 1 |
conditions[] | Model | Each item | Expand List categorization rules — response body — rules item conditions item |
glCodeName | JSON | Optional | |
glCodeName (anyOf 1) | string | Alternative | The display name of the general ledger (GL) account the rule categorizes to. Absent when the rule sets none. |
glCodeName (anyOf 2) | null | Alternative | |
id | string | Required within parent | The rule's stable identifier. Format: uuid |
lastApplied | JSON | Optional | |
lastApplied (anyOf 1) | JSON | Alternative | |
lastApplied (anyOf 1) (allOf 1) | string | All variants apply | Format: yyyy-mm-ddThh:MM:ssZ |
lastApplied (anyOf 1) (allOf 2) | JSON | All variants apply | The last time the rule matched a transaction. Absent if it never has. |
lastApplied (anyOf 2) | null | Alternative | |
outputs | array | Required within parent | What the rule applies to a matching transaction. At least one. Minimum items: 1 |
outputs[] | Model | Each item | Expand List categorization rules — response body — rules item outputs item |
status | JSON | Required within parent | |
status (allOf 1) | string | All variants apply | Known values: active, inactive, invalid |
status (allOf 2) | JSON | All variants apply | Whether the rule currently categorizes transactions. |
Referenced models: List categorization rules — response body — rules item conditions item, List categorization rules — response body — rules item outputs item
Create a categorization rule — request body — body conditions item
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | JSON | Root | A condition a transaction must match for a categorization rule to apply. `tag` says which condition it is (`amount`, `description`, `counterparty`, `debitCard`, `creditCard`, `user`, or `customCategory`) and `contents` holds its fields. A rule's conditions must all match; there is no OR, so an "any of" rule is written as separate rules. |
$ (oneOf 1) | object | Alternative | Additional keys: not allowed |
$ (oneOf 1).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: amount contents |
$ (oneOf 1).tag | string | Required within parent | One of: amount |
$ (oneOf 2) | object | Alternative | Additional keys: not allowed |
$ (oneOf 2).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: description contents |
$ (oneOf 2).tag | string | Required within parent | One of: description |
$ (oneOf 3) | object | Alternative | Additional keys: not allowed |
$ (oneOf 3).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: counterparty contents |
$ (oneOf 3).tag | string | Required within parent | One of: counterparty |
$ (oneOf 4) | object | Alternative | Additional keys: not allowed |
$ (oneOf 4).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: debit Card contents |
$ (oneOf 4).tag | string | Required within parent | One of: debitCard |
$ (oneOf 5) | object | Alternative | Additional keys: not allowed |
$ (oneOf 5).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: credit Card contents |
$ (oneOf 5).tag | string | Required within parent | One of: creditCard |
$ (oneOf 6) | object | Alternative | Additional keys: not allowed |
$ (oneOf 6).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: user contents |
$ (oneOf 6).tag | string | Required within parent | One of: user |
$ (oneOf 7) | object | Alternative | Additional keys: not allowed |
$ (oneOf 7).contents | Model | Required within parent | Expand Create a categorization rule — request body — body / conditions / item / tag: custom Category contents |
$ (oneOf 7).tag | string | Required within parent | One of: customCategory |
Referenced models: Create a categorization rule — request body — body / conditions / item / tag: amount contents, Create a categorization rule — request body — body / conditions / item / tag: counterparty contents, Create a categorization rule — request body — body / conditions / item / tag: credit Card contents, Create a categorization rule — request body — body / conditions / item / tag: custom Category contents, Create a categorization rule — request body — body / conditions / item / tag: debit Card contents, Create a categorization rule — request body — body / conditions / item / tag: description contents, Create a categorization rule — request body — body / conditions / item / tag: user contents
Create a categorization rule — request body — body outputs item
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | object | Root | What a categorization rule does to a transaction it matches. `tag` says which output it is and `contents` holds its fields; today the only output is `setLedger`, which categorizes the transaction to the ledger `contents.ledgerId`. Additional keys: not allowed |
contents | Model | Required within parent | Expand Create a categorization rule — request body — body outputs item contents |
tag | string | Required within parent | One of: setLedger |
Referenced models: Create a categorization rule — request body — body outputs item contents
List categorization rules — response body — rules item outputs item
| Field | Type | Presence | Description and constraints |
|---|---|---|---|
$ | object | Root | What a categorization rule does to a transaction it matches. `tag` says which output it is and `contents` holds its fields; today the only output is `setLedger`, which categorizes the transaction to the ledger `contents.ledgerId`. |
contents | Model | Required within parent | Expand List categorization rules — response body — rules item outputs item contents |
tag | string | Required within parent | Known values: setLedger |
Referenced models: List categorization rules — response body — rules item outputs item contents
Policy, approvals and errors
Section titled “Policy, approvals and errors”SDK function policy ID: mercury.createbookscategorizationrule. Catalog operation: mercury:createBooksCategorizationRule. Rules can name either ID; function rules distinguish managed calls and conservatively cover raw calls sharing the route. Existing grant permissions and policy rules apply. wait() returns an immediate result or waits for a required approval; it never approves the request itself. Persist the approval ID to resume after a restart.
Provider errors retain their response; JSON decoding raises ProviderAPIError for provider failures. ManagedOperationError identifies contract/input/retention errors. See error handling and provider-specific behavior. Sensitive input may be refused before approval storage. A decoding error never resubmits a write.