> ## Documentation Index
> Fetch the complete documentation index at: https://zerodrift.mintlify.site/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 and run on subsequent validations for your API key.

## Request Body

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

<ParamField body="name" type="string" required>
  Human-readable rule name
</ParamField>

<ParamField body="type" type="string" required>
  `AI_SIGNAL`, `REGEX`, or `REQUIRE_NEAR`
</ParamField>

<ParamField body="action" type="string" required>
  `fix` or `flag`
</ParamField>

<ParamField body="severity" type="string" required>
  `do_not_send` or `send_with_caution`
</ParamField>

<ParamField body="suggested_text" type="string">
  Editable suggested replacement or warning guidance. Omit to allow warning
  fallback; `null` clears and suppresses warning fallback.
</ParamField>

<ParamField body="fix_note" type="string">
  Optional remediation guidance
</ParamField>

### AI\_SIGNAL fields

<ParamField body="prompt" type="string" required>
  AI instruction for the signal
</ParamField>

<ParamField body="keywords" type="array" required>
  Keyword gate (at least one string)
</ParamField>

<ParamField body="window" type="string" default="document">
  `sentence`, `paragraph`, or `document`
</ParamField>

### REGEX fields

<ParamField body="regex_pattern" type="string" required>
  Raw Python regex (max 512 chars)
</ParamField>

### REQUIRE\_NEAR fields

<ParamField body="anchor" type="string" required>
  Raw Python regex for the anchor pattern (max 512 chars)
</ParamField>

<ParamField body="near" type="array" required>
  Raw Python regex patterns checked near the anchor
</ParamField>

<ParamField body="distance_chars" type="integer" default="100">
  Character distance around the anchor
</ParamField>

<ParamField body="direction" type="string" default="either">
  `before`, `after`, or `either`
</ParamField>

<ParamField body="case_insensitive" type="boolean" default="false">
  Case-insensitive matching
</ParamField>

<ParamField body="mode" type="string" default="document">
  `document` or `page`
</ParamField>

<ParamField body="min_hits" type="integer" default="1">
  Minimum near-pattern hits required
</ParamField>

## 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

<CodeGroup>
  ```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"
    }'
  ```
</CodeGroup>
