# Create rules

URL: https://docs.hanzo.ai/docs/openapi/books/post-books-rules

> Creates or updates one auto-categorization rule, keyed by its pattern — writing a pattern that already exists REPLACES that row's category and priority.

`POST /v1/books/rules`

> [Books →](https://docs.hanzo.ai/docs/openapi/books) · [All API references →](https://docs.hanzo.ai/docs/openapi) · [Get started →](https://docs.hanzo.ai/docs/guides)

|               |                                        |
| ------------- | -------------------------------------- |
| **Address**   | `https://api.hanzo.ai/v1/books/rules`  |
| **Method**    | `POST`                                 |
| **Operation** | `post_books_rules`                     |
| **Auth**      | `Authorization: Bearer $HANZO_API_KEY` |

Creates or updates one auto-categorization rule, keyed by its pattern —
writing a pattern that already exists REPLACES that row's category and priority. The
category is normalized to a real COA expense account, and anything unrecognized becomes
5900 Uncategorized rather than a guessed real account. It answers the row exactly as
stored, so the caller sees the normalization. A rule overrides a vendor's default
category, so this is the standing instruction that decides how a future bill classifies.

## Request

3 fields, body `application/json` (required).

| Field      | In   | Type            | Required | Description                                                                |
| ---------- | ---- | --------------- | -------- | -------------------------------------------------------------------------- |
| `category` | body | string          | —        | Category is the COA expense account a matching bill books to.              |
| `pattern`  | body | string          | —        | Pattern is the merchant substring the rule matches on, case-insensitively. |
| `priority` | body | integer (int64) | —        | Priority breaks ties: when several patterns match, the highest wins.       |

## Response

| Status    | Body            | Meaning |
| --------- | --------------- | ------- |
| `200`     | books.Rule      | ok      |
| `default` | problem-details | refused |

`200` body — 3 fields.

| Field      | In   | Type            | Always | Description                                                                |
| ---------- | ---- | --------------- | ------ | -------------------------------------------------------------------------- |
| `category` | body | string          | —      | Category is the COA expense account a matching bill books to.              |
| `pattern`  | body | string          | —      | Pattern is the merchant substring the rule matches on, case-insensitively. |
| `priority` | body | integer (int64) | —      | Priority breaks ties: when several patterns match, the highest wins.       |

Failure carries the platform error shape — see [Errors](https://docs.hanzo.ai/docs/errors).

## Examples

**CLI**

```bash
hanzo books rules create
```

**SDK**

**.ts**

```ts
import { Configuration, BooksApi } from 'hanzoai';

const api = new BooksApi(new Configuration({ accessToken: process.env.HANZO_API_KEY }));
const { data } = await api.postBooksRules({ category: "<category>", pattern: "<pattern>" });
```

**.py**

```python
from hanzoai.cloud import ApiClient, Configuration
from hanzoai.cloud.api import BooksApi

client = ApiClient(Configuration(access_token=os.environ["HANZO_API_KEY"]))
result = BooksApi(client).post_books_rules(category="<category>", pattern="<pattern>")
```

**.go**

```go
cfg := hanzoai.NewConfiguration()
cfg.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("HANZO_API_KEY"))
client := hanzoai.NewAPIClient(cfg)

resp, _, err := client.BooksAPI.PostBooksRules(context.Background()).Execute()
if err != nil {
	return err
}
```

**.rs**

```rust
use hanzo_client::apis::{configuration::Configuration, books_api};

let mut cfg = Configuration::new();
cfg.bearer_access_token = std::env::var("HANZO_API_KEY").ok();

let result = books_api::post_books_rules(&cfg, Default::default()).await?;
```

**.java**

```java
import ai.hanzo.cloud.ApiClient;
import ai.hanzo.cloud.api.BooksApi;

ApiClient client = new ApiClient();
client.setBearerToken(System.getenv("HANZO_API_KEY"));

var result = new BooksApi(client).postBooksRules();
```

**HTTP**

```bash
curl -X POST https://api.hanzo.ai/v1/books/rules \
  -H "Authorization: Bearer $HANZO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
       "category": "<category>",
       "pattern": "<pattern>"
     }'
```

**MCP**

MCP reaches **books** through the `books` tool, which names its 20 operations with its own verbs — this one among them, under a name only MCP declares. `describe` explains any of them:

```bash
curl -X POST https://api.hanzo.ai/v1/mcp \
  -H "Content-Type: application/json" \
  -d '{
       "jsonrpc": "2.0",
       "id": 1,
       "method": "tools/call",
       "params": {
         "name": "describe",
         "arguments": {
           "op": "list_book_accounts"
         }
       }
     }'
```

---

[Books API](https://docs.hanzo.ai/docs/openapi/books) · [All Hanzo APIs](https://docs.hanzo.ai/docs/openapi) · [Interactive reference](https://docs.hanzo.ai/docs/openapi/reference)
