# POST /domains/{name}/mail/filters

First published 3 Oct 2026 (release 0.1.0) · Unchanged since publication

UrbeStack release 0.2.35 API operation in the mail group.

Create a mail filter for the domain or one of its mailboxes

- Who may call it: administrator, reseller, customer
- Token scope, any of: mail, all
- Answers: 201 Created

scope defaults to domain. To create a mailbox filter, set scope to account and supply the mailbox's local part in local. Domain filters run before each mailbox's filters. Supply a name of 1 to 64 printable ASCII characters.

Supply 1 to 20 rule objects in rules. Each rule has field, op and value. field can be from, to, subject, header or body. Use header to match any of the usual message headers. op can be contains, not-contains, equals, begins, ends or matches. The matches operator uses shell-style patterns with * and ?. value must contain 1 to 256 characters without ASCII control characters. match defaults to all, which requires every rule to match. Use any to require at least one match.

The action object contains an action field. For folder and redirect, also supply target. folder delivers the message to the target folder, creates it if needed and ends filtering for the message. The folder name must contain 1 to 128 characters without ASCII control characters. discard drops the message. redirect sends a copy to the target email address and keeps the original. For a mailbox filter, the target email address must differ from the mailbox's own. stop skips the remaining filters in the current list.

Filters run in position order, starting at 1. Use position to insert the filter and shift later filters down. position cannot exceed 100. Omit position or give a position past the end to append the filter. The API returns 400 for invalid filter settings. UrbeStack compiles the filter with Pigeonhole before saving it and reports any compilation error. The 201 response contains the new filter in filter.

Administrators, the domain's owner and the owner's reseller can call this operation with a token that has the mail scope.

## As a command

- [urbestack-mail-filter-add](https://urbestack.com/docs/commands/urbestack-mail-filter-add.md): Adds a mail filter to an account (--address) or a domain (--name).

## Parameters

- `name` (path, DomainName, required)

## Request body

JSON, the type [MailFilterCreate](https://urbestack.com/docs/api/types/MailFilterCreate.md).

- `action` (MailFilterAction, required)
- `name` (string, up to 64 characters, required)
- `rules` (array of MailFilterRule, required)
- `local` (LocalPart): The part of an email address before the @ is stored in lowercase.
- `match` (string, one of all, any)
- `position` (integer, 1 to 100)
- `scope` (string, one of domain, account): Use domain to run the filter before all mailbox filters in the domain. Use account to run it for the mailbox specified by local.

## Example

Set $API to the base URL and $TOKEN to the token, as the [API overview](https://urbestack.com/docs/api.md) shows.

```
curl -sS -X POST "$API/domains/example.com/mail/filters" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "action": {
      "action": "folder",
      "target": "https://example.com/new"
    },
    "local": "info",
    "match": "all",
    "name": "alice",
    "position": 1,
    "rules": [
      {
        "field": "from",
        "op": "contains",
        "value": 8443
      }
    ],
    "scope": "domain"
  }'
```

Response, 201 Created:

```
{
  "filter": {
    "action": {
      "action": "folder",
      "target": "https://example.com/new"
    },
    "address": "info@example.com",
    "created_at": "2026-09-09T10:15:00Z",
    "domain": "example.com",
    "id": 1,
    "name": "alice",
    "position": 1,
    "rules": {}
  }
}
```

## Errors

- 400: <what> must be a whole number from 0 to <max>
- 400: <what> must be text without control characters, 1 to <max> characters
- 400: A filter needs one to twenty rules
- 400: A rule is an object with field, op and value
- 400: A rule's field is one of from, to, subject, header, body
- 400: A rule's operator is one of contains, not-contains, equals, begins, ends, matches
- 400: At most <FILTER_MAX> filters here
- 400: Domain name required
- 400: Domain name too long
- 400: Invalid IPv4 address
- 400: Invalid IPv6 address
- 400: Invalid domain name
- 400: Invalid mail address
- 400: Invalid mail address: local part over 64 characters
- 400: Invalid mail address: no local part
- 400: Invalid mail address: the local part may hold letters, digits, dots, _ + and -
- 400: Invalid port
- 400: Invalid setting key
- 400: The action is an object with action and, for folder and redirect, target
- 400: The action is folder, discard, redirect or stop
- 400: match is all or any
- 401: Authentication required
- 404: Setting '<key>' not found
