{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/sendraven-ai/json-schema/sendraven-ai-segment-schema.json",
  "title": "Segment",
  "description": "A saved filter over one audience, evaluated afresh each time it is counted, listed or sent. A contact matches when on the audience, not unsubscribed, and every rule set holds (AND). Suppressions are not consulted, only the unsubscribed flag. Every rule field is always present: a rule that is not set is null, or [] for the list rules, so a created segment and a fetched one have the same keys.",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/sendraven-ai-segments-api-openapi.yml#/components/schemas/Segment",
  "type": "object",
  "required": [
    "id",
    "key",
    "name",
    "audience_id",
    "opened_within_days",
    "clicked_within_days",
    "exclude_unengaged_days",
    "engaged_within_days",
    "properties",
    "date_conditions",
    "tags",
    "exclude_tags",
    "topic_key",
    "created_at",
    "updated_at"
  ],
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "Pass as segment_id when creating a campaign."
    },
    "key": {
      "type": "string",
      "description": "Unique in the workspace. Cannot be changed."
    },
    "name": {
      "type": "string"
    },
    "audience_id": {
      "type": "string",
      "format": "uuid",
      "description": "The audience the segment filters."
    },
    "opened_within_days": {
      "type": [
        "integer",
        "null"
      ]
    },
    "clicked_within_days": {
      "type": [
        "integer",
        "null"
      ]
    },
    "exclude_unengaged_days": {
      "type": [
        "integer",
        "null"
      ]
    },
    "engaged_within_days": {
      "type": [
        "integer",
        "null"
      ]
    },
    "properties": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "property",
          "operator",
          "value"
        ],
        "properties": {
          "property": {
            "type": "string"
          },
          "operator": {
            "type": "string",
            "enum": [
              "equals",
              "not_equals",
              "contains",
              "gt",
              "lt",
              "exists",
              "not_exists"
            ]
          },
          "value": {
            "description": "null for exists and not_exists.",
            "anyOf": [
              {
                "oneOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "number"
                  },
                  {
                    "type": "boolean"
                  }
                ]
              },
              {
                "type": "null"
              }
            ]
          }
        }
      },
      "description": "Property filters; [] when none."
    },
    "date_conditions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/SegmentDateCondition"
      },
      "description": "Relative-date rules; [] when none."
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Normalised; [] when none."
    },
    "exclude_tags": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Normalised; [] when none."
    },
    "topic_key": {
      "type": [
        "string",
        "null"
      ],
      "description": "null when there is no topic rule."
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    },
    "updated_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time",
      "description": "null until the first update."
    }
  },
  "$defs": {
    "SegmentDateCondition": {
      "type": "object",
      "description": "A date compared with now, in UTC, each time the segment is counted, listed or sent. A contact with no value for the field never matches, under any op: someone who never opened has not opened more than 30 days ago. A campaign's audience is fixed when it starts, so a send that resumes later does not re-evaluate it.",
      "required": [
        "field",
        "op",
        "days"
      ],
      "properties": {
        "field": {
          "type": "string",
          "pattern": "^(created_at|last_opened_at|last_clicked_at|last_active_at|attributes\\.(?!\\$)[^.\\u0000]+)$",
          "description": "created_at (when the contact was added to the workspace), last_opened_at, last_clicked_at, last_active_at, or attributes.<key> for a date-valued attribute such as attributes.trial_ends_at. An attribute is read as an ISO 8601 string (\"2026-11-04\" is midnight UTC; an offset such as +02:00 is honoured) or a stored date. A value that does not parse as a date, a number or a boolean does not match. Anything else is 422 invalid_request."
        },
        "op": {
          "type": "string",
          "enum": [
            "older_than_days",
            "within_last_days",
            "within_next_days",
            "after_next_days"
          ],
          "description": "older_than_days: before now minus days (more than N days ago). within_last_days: from now minus days up to now. within_next_days: from now up to now plus days. after_next_days: after now plus days (more than N days from now)."
        },
        "days": {
          "type": "integer",
          "minimum": 1,
          "maximum": 3650
        }
      }
    }
  }
}