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

# Create Custom Rule

> Create a customer-owned AI_SIGNAL, REGEX, or REQUIRE_NEAR rule

Create a custom rule that belongs to your account. Supports `AI_SIGNAL`,
`REGEX`, and `REQUIRE_NEAR` rule types. Requires a full-access API key.

Created rules start active. They run on **Max** (`anchor_3_0_max`) until you
train an adapter that Flagship can check. Copy `rule_id` from the response for
`validation_scope.rules`. See [Enforce Content](https://docs.zerodrift.com/api-reference/validate/validate-content.md).

## Request Body

The body is a discriminated union on `type`. Shared fields:

**name** `string` *required*

Human-readable rule name

**type** `string` *required*

`AI_SIGNAL`, `REGEX`, or `REQUIRE_NEAR`

**action** `string` *required*

`fix` or `flag`

**severity** `string` *required*

`do_not_send` or `send_with_caution`

**suggested\_text** `string`

Editable suggested replacement or warning guidance. Omit to allow warning
fallback; `null` clears and suppresses warning fallback.

**fix\_note** `string`

Optional remediation guidance

### AI\_SIGNAL fields

**prompt** `string` *required*

AI instruction for the signal

**keywords** `array` *required*

Keyword gate (at least one string)

**window** `string` (default: document)

`sentence`, `paragraph`, or `document`

### REGEX fields

**regex\_pattern** `string` *required*

Raw Python regex (max 512 chars)

### REQUIRE\_NEAR fields

**anchor** `string` *required*

Raw Python regex for the anchor pattern (max 512 chars)

**near** `array` *required*

Raw Python regex patterns checked near the anchor

**distance\_chars** `integer` (default: 100)

Character distance around the anchor

**direction** `string` (default: either)

`before`, `after`, or `either`

**case\_insensitive** `boolean` (default: false)

Case-insensitive matching

**mode** `string` (default: document)

`document` or `page`

**min\_hits** `integer` (default: 1)

Minimum near-pattern hits required

## Response

Returns the created custom rule (`CustomRuleResponse`), including `rule_id`,
`source: "custom"`, `active`, timestamps, and type-specific fields.

## Errors

| Status | Meaning |
| - | - |
| `400` | Invalid custom rule payload |
| `403` | Full-access API key required |

## Example

Two request bodies for the same endpoint: a `REGEX` rule and an `AI_SIGNAL`
rule.

```bash cURL (REGEX) theme={null}
curl -X POST "https://api.zerodrift.ai/api/policies/rules" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Block SSN patterns",
    "type": "REGEX",
    "action": "flag",
    "severity": "do_not_send",
    "regex_pattern": "\\b\\d{3}-\\d{2}-\\d{4}\\b"
  }'
```

```bash cURL (AI_SIGNAL) theme={null}
curl -X POST "https://api.zerodrift.ai/api/policies/rules" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "No guaranteed returns",
    "type": "AI_SIGNAL",
    "action": "fix",
    "severity": "do_not_send",
    "prompt": "Flag language that guarantees investment returns.",
    "keywords": ["guarantee", "guaranteed returns"],
    "window": "sentence"
  }'
```


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