---
title: "Inbound rules"
description: "Durable rules that act on mail as it arrives, without your code running: what a rule matches, the four actions, the order they run in, what keeps generated mail from looping, and the history of every run."
url: https://doorman.fyi/docs/rules
markdown: https://doorman.fyi/docs/rules.md
area: "Guides"
section: "Automation and trust"
---

# Inbound rules

Durable rules that act on mail as it arrives, without your code running: what a rule matches, the four actions, the order they run in, what keeps generated mail from looping, and the history of every run.

Source: https://doorman.fyi/docs/rules

## What a rule is

A rule belongs to one inbox and says: when a message like this arrives, do that. It is stored in the API and runs there, so it acts on mail while your agent is asleep, restarting or not yet written. A rule can label a thread, answer the sender from a template, forward the message, or move it to the trash.

An inbox holds at most 25 rules, and a rule at most 5 actions. Rules are for the fixed part of handling mail. Anything that needs judgment belongs in a handler: [The agent runtime](https://doorman.fyi/docs/agent.md).

## Creating a rule

[`POST /v1/inboxes/{inboxId}/rules`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-create) takes a `name`, what to match under `when`, and the actions under `then`. A new rule is enabled and applies to the next message that arrives.

curl:
```sh
curl -s -X POST "https://api.doorman.fyi/v1/inboxes/inb_01jc4q7w9k2m5n8p3r6t1v4x7z/rules" \
  -H "authorization: Bearer $DOORMAN_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "name": "Invoice triage",
    "when": {
      "category": "personal",
      "subjectContains": "invoice"
    },
    "then": [
      {
        "type": "label",
        "label": "billing"
      }
    ]
  }'
```

TypeScript:
```ts
const rule = await client.inboxes.rules.create("inb_01jc4q7w9k2m5n8p3r6t1v4x7z", {
  name: "Invoice triage",
  when: { category: "personal", subjectContains: "invoice" },
  then: [{ type: "label", label: "billing" }],
});
```

Python:
```python
rule = client.inboxes.rules.create("inb_01jc4q7w9k2m5n8p3r6t1v4x7z", {
    "name": "Invoice triage",
    "when": {"category": "personal", "subjectContains": "invoice"},
    "then": [{"type": "label", "label": "billing"}],
})
```

Command line:
```sh
doorman inboxes rules create inb_01jc4q7w9k2m5n8p3r6t1v4x7z --name "Invoice triage" \
  --when '{"category":"personal","subjectContains":"invoice"}' \
  --then '[{"type":"label","label":"billing"}]'
```

201 Created:
```json
{
  "id": "rul_01jc5f1a2b3c4d5e6f7g8h9j0k",
  "inboxId": "inb_01jc4q7w9k2m5n8p3r6t1v4x7z",
  "name": "Invoice triage",
  "enabled": true,
  "when": {
    "category": "personal",
    "subjectContains": "invoice"
  },
  "then": [
    {
      "type": "label",
      "label": "billing"
    }
  ],
  "createdAt": "2026-11-05T10:05:00.000Z",
  "updatedAt": "2026-11-05T10:05:00.000Z"
}
```

## What a rule matches

Every condition given under `when` has to hold for the rule to match. A condition that is left out is not checked.

**Conditions: RuleWhen**
All supplied conditions must match. At least one condition is required.
- `category` ("personal" | "automated" | "marketing" | "bounce" | "spam" | "malicious", optional)
- `sender` (string (email), optional): Exact normalized From address. A match is not proof of sender identity.
- `subjectContains` (string, optional): Case-insensitive subject substring, not a regular expression.
- `minRiskLevel` ("low" | "medium" | "high" | "critical", optional): The message's risk level is this or higher. A message that was not assessed never matches.
- `senderStanding` ("trusted" | "known" | "new" | "blocked", optional): The sender's standing when the message arrived. A message that was not assessed never matches.

- A sender match compares the `From` address. It is not proof of who sent the message: mail can claim any sender.
- `subjectContains` is a plain substring of at most 200 characters, matched without regard to case. It is not a pattern.
- The risk and standing conditions read the [risk assessment](https://doorman.fyi/docs/security.md#preview-and-rules) the message was given. A message that was not assessed never matches them.

## What a rule does

**Actions: RuleAction**
One of:

(inline)
- `type` ("label")
- `label` (ThreadLabel)

(inline)
- `type` ("auto_reply")
- `text` (string): Plain text, with optional {{sender}}, {{subject}} and {{inbox}} substitutions. No code, HTML or remote templates.

(inline)
- `type` ("forward")
- `to` (string (email))

(inline)
- `type` ("trash")

- `label`: Adds one [label](https://doorman.fyi/docs/sdk.md#metadata) to the message's thread.
- `auto_reply`: Answers the sender with a plain-text template of at most 10,000 characters. `{{sender}}`, `{{subject}}` and `{{inbox}}` are replaced, once. A template runs no code and loads nothing.
- `forward`: Sends the message's sender, subject and plain text to one address, without its attachments, as a new thread.
- `trash`: Moves the thread to the trash.

## Order, and mail that does not loop

- Rules are tried oldest first. Every enabled rule that matches runs, and its actions run in the order written. A rule holds at most one action that sends mail.
- For one inbound message at most 1 action that sends mail is carried out, across all the rules that matched: the first one in order. Where a guard refuses it, no later one takes its place.
- An auto-reply or a forward is sent only for mail that is personal, safe to answer automatically and not rated suspicious, and never to the inbox itself. An auto-reply goes to the message's `Reply-To`, or to its sender.
- Generated mail counts against the inbox's [sending limits](https://doorman.fyi/docs/sending.md#limits) like any other send.
- What a rule will do to a message is fixed when the message arrives. Editing or deleting the rule afterwards cancels what had not started, and cannot recall mail that already left.

> Note: A rule that sends mail does so without anyone reading the message first. Give an `auto_reply` or a `forward` the narrowest `when` that does the job.

## The history of a rule

[`GET /v1/inboxes/{inboxId}/rules/{ruleId}/runs`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-runs) lists what the rule did, newest first: one run for each message it matched, with the event it ran for, how many actions were carried out, and a reason that holds no mail content. The history stays readable after the rule is deleted.

- `pending`: The rule matched and its actions are waiting to run.
- `running`: The actions are being carried out now.
- `completed`: At least one action was carried out. `skippedActions` counts the ones that were not, and `lastError` holds the reason for the last of them.
- `failed`: An action failed, and the ones after it did not run. The actions before it stay done. `lastError` is the send's error code, or `execution_failed`.
- `skipped`: No action was carried out, and `lastError` says why.
- `uncertain`: A send may or may not have left: the run was interrupted, or the provider's answer was lost. It is not sent again, because the provider may already have accepted it.

**Why an action was skipped**

| `lastError` | An action was skipped because |
| --- | --- |
| `outbound_action_limit` | Another rule's action was the one chosen to send mail for this message. |
| `auto_reply_blocked` | The message is not clean personal mail, so nothing is sent for it. |
| `self_or_invalid_recipient` | The mail would have gone to the inbox itself, or to an address that is not valid. |
| `message_unavailable` | The message is no longer there to answer or forward. |
| `rule_or_inbox_unavailable` | The rule was changed, disabled or deleted, or the inbox or the account is no longer active. |
| `thread_unchanged` | The thread already had the label, or was already in the trash. |

TypeScript:
```ts
for await (const run of client.inboxes.rules.iterateRuns("inb_01jc4q7w9k2m5n8p3r6t1v4x7z", "rul_01jc5f1a2b3c4d5e6f7g8h9j0k")) {
  console.log(run.eventId, run.status, run.completedActions, run.lastError);
}
```

Python:
```python
for run in client.inboxes.rules.iterate_runs("inb_01jc4q7w9k2m5n8p3r6t1v4x7z", "rul_01jc5f1a2b3c4d5e6f7g8h9j0k"):
    print(run["eventId"], run["status"], run["completedActions"], run["lastError"])
```

## Who may manage rules

Rules are managed with an account key or a console session. An inbox key gets `403 forbidden`. An app a person installed needs `rules:manage`, `mail:read`, `mail:write`, `mail:send` together, because a rule reads mail, changes threads and sends. A rule an app made stops matching when the app's installation ends. See [Permissions and account boundaries](https://doorman.fyi/docs/oauth.md#scopes).

## The operations

| Operation | TypeScript | Python | MCP tool | Command |
| --- | --- | --- | --- | --- |
| [`GET /inboxes/{inboxId}/rules`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-list) | [List inbound rules](https://doorman.fyi/docs/sdk/reference/inboxes.md#inboxes-rules-list) | [List inbound rules](https://doorman.fyi/docs/python/reference/inboxes.md#inboxes-rules-list) | [List inbound rules](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-list) | [List inbound rules](https://doorman.fyi/docs/cli/commands/inboxes.md#inboxes-rules-list) |
| [`POST /inboxes/{inboxId}/rules`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-create) | [Create an inbound rule](https://doorman.fyi/docs/sdk/reference/inboxes.md#inboxes-rules-create) | [Create an inbound rule](https://doorman.fyi/docs/python/reference/inboxes.md#inboxes-rules-create) | [Create an inbound rule](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-create) | [Create an inbound rule](https://doorman.fyi/docs/cli/commands/inboxes.md#inboxes-rules-create) |
| [`GET /inboxes/{inboxId}/rules/{ruleId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-get) | [Read an inbound rule](https://doorman.fyi/docs/sdk/reference/inboxes.md#inboxes-rules-get) | [Read an inbound rule](https://doorman.fyi/docs/python/reference/inboxes.md#inboxes-rules-get) | [Read an inbound rule](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-get) | [Read an inbound rule](https://doorman.fyi/docs/cli/commands/inboxes.md#inboxes-rules-get) |
| [`PATCH /inboxes/{inboxId}/rules/{ruleId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-update) | [Update an inbound rule](https://doorman.fyi/docs/sdk/reference/inboxes.md#inboxes-rules-update) | [Update an inbound rule](https://doorman.fyi/docs/python/reference/inboxes.md#inboxes-rules-update) | [Update an inbound rule](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-update) | [Update an inbound rule](https://doorman.fyi/docs/cli/commands/inboxes.md#inboxes-rules-update) |
| [`DELETE /inboxes/{inboxId}/rules/{ruleId}`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-delete) | [Delete an inbound rule](https://doorman.fyi/docs/sdk/reference/inboxes.md#inboxes-rules-delete) | [Delete an inbound rule](https://doorman.fyi/docs/python/reference/inboxes.md#inboxes-rules-delete) | [Delete an inbound rule](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-delete) | [Delete an inbound rule](https://doorman.fyi/docs/cli/commands/inboxes.md#inboxes-rules-delete) |
| [`GET /inboxes/{inboxId}/rules/{ruleId}/runs`](https://doorman.fyi/docs/reference/inboxes.md#inboxes-rules-runs) | [List rule executions](https://doorman.fyi/docs/sdk/reference/inboxes.md#inboxes-rules-runs) | [List rule executions](https://doorman.fyi/docs/python/reference/inboxes.md#inboxes-rules-runs) | [List rule executions](https://doorman.fyi/docs/mcp/tools/inboxes.md#inboxes-rules-runs) | [List rule executions](https://doorman.fyi/docs/cli/commands/inboxes.md#inboxes-rules-runs) |
