# Update a segment

`PATCH /v1/segments/{id}`

Requires an API key with the `segments:write` scope. [About keys and scopes](/docs/api/authentication)

Replaces the segment's rules or renames it. Changing rules changes who the segment matches from the next evaluation onward; campaigns already sent are unaffected.

## Path parameters

- `id` (string, required, uuid)

## Request body

- `name` (string, optional, 1–255 characters) — Unique within the workspace.
- `rules` (object, optional) — Replaces the whole rule tree.
  - One of: **Match all**
    - `all` (object[], required, at least 1 item, at most 20 items) — Every rule or nested group must match.
  - One of: **Match any**
    - `any` (object[], required, at least 1 item, at most 20 items) — At least one rule or nested group must match.

## Returns

Returns `200 OK` with a `Segment` in `data`. Otherwise returns an error; [Errors](/docs/api/errors) lists every code and which to retry.

- `id` (string, required, uuid)
- `name` (string, required)
- `rules` (object, required)
  - One of: **Match all**
    - `all` (object[], required, at least 1 item, at most 20 items) — Every rule or nested group must match.
  - One of: **Match any**
    - `any` (object[], required, at least 1 item, at most 20 items) — At least one rule or nested group must match.
- `lastCount` (integer | null, required, min 0, max 9007199254740991) — Audience size from the background count; null until first counted.
- `countedAt` (string | null, required, date-time) — When `lastCount` was taken.
- `createdAt` (string, required, date-time)
- `updatedAt` (string, required, date-time)

## Example

```bash
curl -X PATCH https://api.kirora.io/v1/segments/018f2a4e-6b1c-7d3e-9f40-37386ae05a6b \
  -H "Authorization: Bearer $KIRORA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Weekend specials",
    "rules": {
      "all": [
        {
          "type": "attribute",
          "field": "field_example",
          "operator": "equals"
        }
      ]
    }
  }'
```

Response `200`:

```json
{
  "data": {
    "id": "018f2a4e-6b1c-7d3e-9f40-37386ae05a6b",
    "name": "Weekend specials",
    "rules": {
      "all": [
        {
          "type": "attribute",
          "field": "field_example",
          "operator": "equals"
        }
      ]
    },
    "lastCount": 0,
    "countedAt": "2030-03-14T09:30:00Z",
    "createdAt": "2030-03-14T09:30:00Z",
    "updatedAt": "2030-03-14T09:30:00Z"
  }
}
```
