# Add a category (/api/hierarchy/create_category)

`POST /v1/products/{productId}/categories`

Operation name: `create_category`

At the top level (`parentId` null) or under a category holding no features. Choose a new uuid as `id` so that a retry is safe: a repeat answers the same category's add change; an id anything else holds is refused. Product Owners and Admins only; Viewers are refused. It answers the hierarchy change it stored (what changed, who and when), which `undo_hierarchy_change` reverses. Values a person sets have origin `user` and are kept by later knowledge base reads and rebuilds. Ids that are unknown, deleted or another product's are not found; a place that breaks the tree's rules (a category never holds both subcategories and features, never goes under itself) is refused (`conflict`, with the rule in `details.rule` and, for moves and merges, the allowed places in `details.allowed`). Send the `version` from `get_hierarchy` as `expectedVersion` to be refused (`conflict`) when the hierarchy changed since you read it; every answer carries the `version` after the change. For many changes at once use `apply_hierarchy_changes`.

## Authentication

An OAuth access token for the person who allowed your tool: register a client, then the authorization code flow with PKCE (S256). Send it as `Authorization: Bearer <token>` on every route that needs a signed-in person. Calls with a token are limited per person, across all their apps, to 120 calls a minute and 10,000 a UTC day; calls that start AI runs or knowledge base reads (creating an organization, starting a knowledge base read, rebuilding categories, retrying topics or rechecking) also to 10 a minute and 200 a UTC day. Over a limit a call answers 429 `rate_limited` with `Retry-After` and `details.retryAfter` in seconds, and changes nothing.

Or: An API key, sent as `Authorization: Bearer ha_…` (keys made before the rename to Heuristic start `fsa_` and keep working). Keys are created only in the Heuristic web app and shown once. A personal key (Settings → Personal → My API keys) acts as its person in one of their organizations, with exactly their access there, checked on every call; its calls count toward the person's limits on calls through apps and agents, as described for OAuth tokens, and it can't change the person's account (their profile, organizations, invitations or connected apps). An organization key (Settings → Organization → API keys, created by Admins) acts for the organization, not a person: it can see and change every product and its feedback as an Admin can, but not people, product access, CRM connections or the organization's settings, and it can't delete a product; each organization key has its own limits, with the same numbers. Either kind is Full access or Read only (a change with a read-only key answers 403 `read_only_key`), reaches only its own organization (others answer 404), works on /v1 only, not on the MCP server, and can't create keys or manage the organization's API keys. Other refused calls answer 403 `api_key_not_allowed`. A wrong, expired or revoked key, or a disabled member's personal key, answers 401 `unauthorized`. A full-access key sends feedback: a personal key as its person, to the products they own; an organization key to any product of its organization; the record's `sentBy` names the key.

## Path parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `productId` | path | string | yes | A Heuristic id: a uuid, in any case (it is read in lower case). Anything else answers 400 `validation_error`. |

## Query parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `expectedVersion` | query | string | no | Optional: the `version` from `get_hierarchy` you planned from; refused (`conflict`) when the hierarchy changed since. |

## Request body

Content type: `application/json` (required)

```json
{
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "A Heuristic id: a uuid, in any case (it is read in lower case). Anything else answers 400 `validation_error`.",
      "x-feedstack-id": true
    },
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u2028\\u2029]*$",
      "description": "The category's title, 1 to 100 characters on one line."
    },
    "description": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 250,
      "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u2028\\u2029]*$",
      "description": "What the category covers; empty or null for none."
    },
    "parentId": {
      "type": [
        "string",
        "null"
      ],
      "format": "uuid",
      "description": "A Heuristic id: a uuid, in any case (it is read in lower case). Anything else answers 400 `validation_error`.",
      "x-feedstack-id": true
    }
  },
  "required": [
    "title",
    "parentId"
  ],
  "description": "The new category. `parentId` is the category to put it under, or null for the top level."
}
```

## Responses

### 200

Added earlier with the same id

```json
{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "change": {
          "type": [
            "object",
            "null"
          ],
          "properties": {
            "id": {
              "type": "string"
            },
            "kind": {
              "type": "string",
              "enum": [
                "edit",
                "move",
                "add",
                "merge",
                "delete",
                "undo",
                "bulk"
              ]
            },
            "subject": {
              "type": "object",
              "properties": {
                "type": {
                  "type": "string",
                  "enum": [
                    "category",
                    "feature",
                    "product"
                  ]
                },
                "id": {
                  "type": "string"
                },
                "title": {
                  "type": "string"
                }
              },
              "required": [
                "type",
                "id",
                "title"
              ]
            },
            "details": {
              "type": "object",
              "properties": {
                "fields": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "field": {
                        "type": "string",
                        "enum": [
                          "title",
                          "description"
                        ]
                      },
                      "before": {
                        "type": [
                          "string",
                          "null"
                        ]
                      },
                      "after": {
                        "type": [
                          "string",
                          "null"
                        ]
                      }
                    },
                    "required": [
                      "field",
                      "before"
                    ]
                  }
                },
                "from": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "to": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "into": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "categories": {
                  "type": "integer"
                },
                "features": {
                  "type": "integer"
                },
                "parent": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "other": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "stepCount": {
                  "type": "integer"
                },
                "steps": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "step": {
                        "type": "integer"
                      },
                      "kind": {
                        "type": "string",
                        "enum": [
                          "edit",
                          "move",
                          "add",
                          "merge",
                          "delete"
                        ]
                      },
                      "subject": {
                        "type": "object",
                        "properties": {
                          "type": {
                            "type": "string",
                            "enum": [
                              "category",
                              "feature"
                            ]
                          },
                          "id": {
                            "type": "string"
                          },
                          "title": {
                            "type": "string"
                          }
                        },
                        "required": [
                          "type",
                          "id",
                          "title"
                        ]
                      },
                      "details": {
                        "type": "object",
                        "properties": {
                          "fields": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "field": {
                                  "type": "string",
                                  "enum": [
                                    "title",
                                    "description"
                                  ]
                                },
                                "before": {
                                  "type": [
                                    "string",
                                    "null"
                                  ]
                                },
                                "after": {
                                  "type": [
                                    "string",
                                    "null"
                                  ]
                                }
                              },
                              "required": [
                                "field",
                                "before"
                              ]
                            }
                          },
                          "from": {
                            "type": [
                              "object",
                              "null"
                            ],
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "to": {
                            "type": [
                              "object",
                              "null"
                            ],
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "into": {
                            "type": "object",
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "categories": {
                            "type": "integer"
                          },
                          "features": {
                            "type": "integer"
                          },
                          "parent": {
                            "type": [
                              "object",
                              "null"
                            ],
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "other": {
                            "type": "object",
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          }
                        }
                      }
                    },
                    "required": [
                      "step",
                      "kind",
                      "subject",
                      "details"
                    ]
                  }
                }
              }
            },
            "undoesChangeId": {
              "type": [
                "string",
                "null"
              ]
            },
            "undoneAt": {
              "type": [
                "string",
                "null"
              ]
            },
            "createdAt": {
              "type": "string"
            },
            "createdBy": {
              "type": [
                "object",
                "null"
              ],
              "properties": {
                "id": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "id",
                "name"
              ]
            },
            "via": {
              "type": "string",
              "enum": [
                "web",
                "app",
                "not_recorded"
              ]
            },
            "app": {
              "type": [
                "object",
                "null"
              ],
              "properties": {
                "clientId": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "clientId",
                "name"
              ]
            }
          },
          "required": [
            "id",
            "kind",
            "subject",
            "details",
            "undoesChangeId",
            "undoneAt",
            "createdAt",
            "createdBy",
            "via",
            "app"
          ]
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "change",
        "version"
      ]
    },
    "meta": {
      "type": "object",
      "additionalProperties": {}
    }
  },
  "required": [
    "data",
    "meta"
  ]
}
```

### 201

Added

```json
{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "change": {
          "type": [
            "object",
            "null"
          ],
          "properties": {
            "id": {
              "type": "string"
            },
            "kind": {
              "type": "string",
              "enum": [
                "edit",
                "move",
                "add",
                "merge",
                "delete",
                "undo",
                "bulk"
              ]
            },
            "subject": {
              "type": "object",
              "properties": {
                "type": {
                  "type": "string",
                  "enum": [
                    "category",
                    "feature",
                    "product"
                  ]
                },
                "id": {
                  "type": "string"
                },
                "title": {
                  "type": "string"
                }
              },
              "required": [
                "type",
                "id",
                "title"
              ]
            },
            "details": {
              "type": "object",
              "properties": {
                "fields": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "field": {
                        "type": "string",
                        "enum": [
                          "title",
                          "description"
                        ]
                      },
                      "before": {
                        "type": [
                          "string",
                          "null"
                        ]
                      },
                      "after": {
                        "type": [
                          "string",
                          "null"
                        ]
                      }
                    },
                    "required": [
                      "field",
                      "before"
                    ]
                  }
                },
                "from": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "to": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "into": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "categories": {
                  "type": "integer"
                },
                "features": {
                  "type": "integer"
                },
                "parent": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "other": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "id",
                    "title"
                  ]
                },
                "stepCount": {
                  "type": "integer"
                },
                "steps": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "step": {
                        "type": "integer"
                      },
                      "kind": {
                        "type": "string",
                        "enum": [
                          "edit",
                          "move",
                          "add",
                          "merge",
                          "delete"
                        ]
                      },
                      "subject": {
                        "type": "object",
                        "properties": {
                          "type": {
                            "type": "string",
                            "enum": [
                              "category",
                              "feature"
                            ]
                          },
                          "id": {
                            "type": "string"
                          },
                          "title": {
                            "type": "string"
                          }
                        },
                        "required": [
                          "type",
                          "id",
                          "title"
                        ]
                      },
                      "details": {
                        "type": "object",
                        "properties": {
                          "fields": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "field": {
                                  "type": "string",
                                  "enum": [
                                    "title",
                                    "description"
                                  ]
                                },
                                "before": {
                                  "type": [
                                    "string",
                                    "null"
                                  ]
                                },
                                "after": {
                                  "type": [
                                    "string",
                                    "null"
                                  ]
                                }
                              },
                              "required": [
                                "field",
                                "before"
                              ]
                            }
                          },
                          "from": {
                            "type": [
                              "object",
                              "null"
                            ],
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "to": {
                            "type": [
                              "object",
                              "null"
                            ],
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "into": {
                            "type": "object",
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "categories": {
                            "type": "integer"
                          },
                          "features": {
                            "type": "integer"
                          },
                          "parent": {
                            "type": [
                              "object",
                              "null"
                            ],
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          },
                          "other": {
                            "type": "object",
                            "properties": {
                              "id": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              }
                            },
                            "required": [
                              "id",
                              "title"
                            ]
                          }
                        }
                      }
                    },
                    "required": [
                      "step",
                      "kind",
                      "subject",
                      "details"
                    ]
                  }
                }
              }
            },
            "undoesChangeId": {
              "type": [
                "string",
                "null"
              ]
            },
            "undoneAt": {
              "type": [
                "string",
                "null"
              ]
            },
            "createdAt": {
              "type": "string"
            },
            "createdBy": {
              "type": [
                "object",
                "null"
              ],
              "properties": {
                "id": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "id",
                "name"
              ]
            },
            "via": {
              "type": "string",
              "enum": [
                "web",
                "app",
                "not_recorded"
              ]
            },
            "app": {
              "type": [
                "object",
                "null"
              ],
              "properties": {
                "clientId": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "clientId",
                "name"
              ]
            }
          },
          "required": [
            "id",
            "kind",
            "subject",
            "details",
            "undoesChangeId",
            "undoneAt",
            "createdAt",
            "createdBy",
            "via",
            "app"
          ]
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "change",
        "version"
      ]
    },
    "meta": {
      "type": "object",
      "additionalProperties": {}
    }
  },
  "required": [
    "data",
    "meta"
  ]
}
```

### 400

Error

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "message": {
          "type": "string"
        },
        "details": {}
      },
      "required": [
        "code",
        "message"
      ]
    }
  },
  "required": [
    "error"
  ]
}
```

### 401

Error

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "message": {
          "type": "string"
        },
        "details": {}
      },
      "required": [
        "code",
        "message"
      ]
    }
  },
  "required": [
    "error"
  ]
}
```

### 403

Error

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "message": {
          "type": "string"
        },
        "details": {}
      },
      "required": [
        "code",
        "message"
      ]
    }
  },
  "required": [
    "error"
  ]
}
```

### 404

Error

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "message": {
          "type": "string"
        },
        "details": {}
      },
      "required": [
        "code",
        "message"
      ]
    }
  },
  "required": [
    "error"
  ]
}
```

### 409

Error

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "message": {
          "type": "string"
        },
        "details": {}
      },
      "required": [
        "code",
        "message"
      ]
    }
  },
  "required": [
    "error"
  ]
}
```

## Example request

```bash
curl -X POST "https://staging.heuristic.app/v1/products/{productId}/categories" \
  -H "Authorization: Bearer <access token>" \
  -H "Content-Type: application/json" \
  -d '{"id":"00000000-0000-4000-8000-000000000000","title":"string","description":"string","parentId":"00000000-0000-4000-8000-000000000000"}'
```
