BETA
Skip to content

Segments

Create and manage reusable audience segments. Segments define groups of users based on conditions and can be referenced by targeting rules across multiple flags.

Base URL: https://app.krafter.dev/api/v1

List Segments

Retrieve all segments for a project.

GET /flags/segments?project_id=...

Required scope: flags:read

Query Parameters

ParameterTypeRequiredDescription
project_idstringYesProject to list segments for.

Example Request

bash
curl "https://app.krafter.dev/api/v1/flags/segments?project_id=a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d" \
  -H "Authorization: Bearer kr_live_abc123def456"

Example Response

json
{
  "data": [
    {
      "id": "1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e",
      "name": "Pro Users",
      "description": "Users on the pro plan in North America",
      "rules": [
        {
          "key": "plan",
          "operator": "equals",
          "value": "pro"
        },
        {
          "key": "country",
          "operator": "in",
          "value": ["US", "CA"]
        }
      ],
      "match_type": "all",
      "created_at": "2025-03-10T11:00:00Z",
      "updated_at": "2025-03-10T11:00:00Z"
    },
    {
      "id": "2b3c4d5e-6f7a-4b8c-9d0e-1f2a3b4c5d6f",
      "name": "Beta Testers",
      "description": "Users who opted into the beta program",
      "rules": [
        {
          "key": "beta_optin",
          "operator": "equals",
          "value": true
        }
      ],
      "match_type": "all",
      "created_at": "2025-03-11T09:30:00Z",
      "updated_at": "2025-03-11T09:30:00Z"
    }
  ]
}

Create Segment

Create a new reusable segment.

POST /flags/segments

Required scope: flags:write

Request Body

FieldTypeRequiredDescription
project_idstringYesProject this segment belongs to.
namestringYesDisplay name for the segment.
descriptionstringNoDescription of the audience this segment represents.
rulesarrayYesArray of condition objects defining segment membership. See Rule Format.
match_typestringYesHow conditions are combined: "all" (AND) or "any" (OR).

Rule Format

Each rule is a condition object with three fields:

FieldTypeDescription
keystringThe context attribute to evaluate (e.g., "plan", "country").
operatorstringComparison operator: equals, not_equals, contains, not_contains, in, not_in, greater_than, less_than.
valueanyThe value to compare against. For in and not_in, use an array.

Example Request

bash
curl -X POST https://app.krafter.dev/api/v1/flags/segments \
  -H "Authorization: Bearer kr_live_abc123def456" \
  -H "Content-Type: application/json" \
  -d '{
    "project_id": "a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d",
    "name": "Pro Users",
    "description": "Users on the pro plan in North America",
    "rules": [
      {
        "key": "plan",
        "operator": "equals",
        "value": "pro"
      },
      {
        "key": "country",
        "operator": "in",
        "value": ["US", "CA"]
      }
    ],
    "match_type": "all"
  }'

Example Response

json
// 201 Created
{
  "data": {
    "id": "1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e",
    "name": "Pro Users",
    "description": "Users on the pro plan in North America",
    "rules": [
      {
        "key": "plan",
        "operator": "equals",
        "value": "pro"
      },
      {
        "key": "country",
        "operator": "in",
        "value": ["US", "CA"]
      }
    ],
    "match_type": "all",
    "created_at": "2025-03-10T11:00:00Z",
    "updated_at": "2025-03-10T11:00:00Z"
  }
}

Update Segment

Update an existing segment. Only provided fields are changed. Changes affect all rules that reference this segment.

PATCH /flags/segments/:segment_id

Required scope: flags:write

Request Body

FieldTypeRequiredDescription
namestringNoDisplay name for the segment.
descriptionstringNoDescription of the audience.
rulesarrayNoArray of condition objects.
match_typestringNoHow conditions are combined: "all" or "any".

Example Request

bash
curl -X PATCH https://app.krafter.dev/api/v1/flags/segments/1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e \
  -H "Authorization: Bearer kr_live_abc123def456" \
  -H "Content-Type: application/json" \
  -d '{
    "rules": [
      {
        "key": "plan",
        "operator": "in",
        "value": ["pro", "enterprise"]
      },
      {
        "key": "country",
        "operator": "in",
        "value": ["US", "CA", "GB"]
      }
    ]
  }'

Example Response

json
{
  "data": {
    "id": "1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e",
    "name": "Pro Users",
    "description": "Users on the pro plan in North America",
    "rules": [
      {
        "key": "plan",
        "operator": "in",
        "value": ["pro", "enterprise"]
      },
      {
        "key": "country",
        "operator": "in",
        "value": ["US", "CA", "GB"]
      }
    ],
    "match_type": "all",
    "created_at": "2025-03-10T11:00:00Z",
    "updated_at": "2025-03-16T16:00:00Z"
  }
}

Delete Segment

Permanently delete a segment. Any rules referencing this segment will have their segment_id cleared.

DELETE /flags/segments/:segment_id

Required scope: flags:write

Example Request

bash
curl -X DELETE https://app.krafter.dev/api/v1/flags/segments/1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e \
  -H "Authorization: Bearer kr_live_abc123def456"

Example Response

// 204 No Content

Built by Krafter Studio