---
title: "Create a rule"
method: POST
path: "/email-rules"
tags: ["Email Rules"]
---

# Create a rule

`POST /email-rules`

## Request

- Base URL: `https://api.primitive.dev/v1`
- URL: `https://api.primitive.dev/v1/email-rules`
- Auth: HTTP bearer

Saves a rule as version 1, from a compile response or a tree you wrote. The tree is checked against the column contract. New rules start in observe, where what the rule matches is recorded and nothing is granted; draft is not evaluated. The caller is the rule's author, and the rule never reaches mail the author cannot read. Needs mail:read, a credential acting as a member with their full access (API keys, agent keys and narrowed tokens are refused), and the email rules feature on the organization.

## Headers

- `Idempotency-Key` string

## Request body

- CreateEmailRuleRequest
  - `name` string, required: A short name for the rule.
  - `state` 'draft' | 'observe': observe (the default) records what the rule would match and grants nothing; draft is not evaluated.
  - `english` string, required: The plain-English description the rule was compiled from.
  - `tree` object, required: The rule tree. Checked against the column contract on save.
    - `version` 1, required
    - `vocabulary` string, required: The column contract version the rule is written against, for example v1.
    - `when` object, required: The rule's conditions: a condition ({column, op, value?, min_confidence?}) or a group ({all: [...]}, {any: [...]} or {not: ...}).
    - `questions` string[], required: Yes/no questions of the rule's own, asked about an email only after its conditions match. An email matches only if every answer is yes.
  - `explanation` string, required: What the rule matches, in plain English.
  - `assumptions` string[]: Readings chosen where the description was open to more than one.
  - `warnings` string[]: Parts of the description the rule does not express.
  - `compiled_by` string: Who produced the tree, as the caller states it (not verified): the compile response's compiled_by, or member when you wrote or edited it yourself. Defaults to member.

## Response `201`

Create a rule

- object
  - `success` true, required
  - `data` CreateEmailRuleResult, required
    - `id` string, uuid, required
    - `name` string, required
    - `state` 'draft' | 'observe' | 'active', required
    - `current_version` integer, nullable, required
    - `author_user_id` string, uuid, required
    - `is_author` boolean, required: Whether the caller wrote the rule (under their current membership). Only the author edits a rule; anyone else who can see it may only disable or delete it.
    - `created_at` string, date-time, required
    - `updated_at` string, date-time, required
    - `current` object, nullable, required
      - `version` integer, required
      - `english` string, required
      - `tree` object, required
        - `version` 1, required
        - `vocabulary` string, required: The column contract version the rule is written against, for example v1.
        - `when` object, required: The rule's conditions: a condition ({column, op, value?, min_confidence?}) or a group ({all: [...]}, {any: [...]} or {not: ...}).
        - `questions` string[], required: Yes/no questions of the rule's own, asked about an email only after its conditions match. An email matches only if every answer is yes.
      - `explanation` string, required
      - `assumptions` string[], required
      - `warnings` string[], required
      - `compiled_by` string, required
      - `vocabulary` string, required
      - `created_at` string, date-time, required
      - `sql` object: Present only when include_sql was true.
        - `expression` string, required: A parameterized boolean expression over one row per email.
        - `params` unknown[], required: The values bound to $1, $2 and so on.
          - unknown

## Other responses

- `400`: Invalid request parameters
- `401`: Invalid or missing API key
- `403`: Authenticated caller lacks permission for the operation
- `429`: Rate limit exceeded. The `ratelimit-*` headers describe the limiter that rejected this request, with its own limit and window, not the API budget that successful responses report. A limiter that reports only a wait sends `Retry-After` alone.

---

[API](https://skmtc.dev/primitive/apis/primitive-api.md) - [All operations](https://skmtc.dev/primitive/apis/primitive-api/llms.txt) - [OpenAPI document](https://skmtc.dev/primitive/apis/primitive-api/revisions/bce409c97acb?raw)
