> ## Documentation Index
> Fetch the complete documentation index at: https://code.dcycle.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Update KPI

> Update a KPI definition within a dataset

# Update KPI

Update one or more fields of a KPI definition. Only send the fields you want to change.

<Note>
  For `select` KPIs: when you send `options`, the array replaces the current options entirely. Include existing option `id`s to keep them (optionally renaming/reordering); omit an `id` to delete that option. New options (without `id`) are created.
</Note>

## Request

### Headers

<ParamField header="x-api-key" type="string" required>
  Your API key for authentication

  **Example:** `sk_live_1234567890abcdef`
</ParamField>

<ParamField header="x-organization-id" type="string" required>
  Your organization UUID

  **Example:** `a8315ef3-dd50-43f8-b7ce-d839e68d51fa`
</ParamField>

### Path Parameters

<ParamField path="dataset_id" type="string" required>
  UUID of the parent dataset
</ParamField>

<ParamField path="kpi_id" type="string" required>
  UUID of the KPI to update
</ParamField>

### Body Parameters

All fields are optional — only send what you want to change.

<ParamField body="name" type="string">
  KPI name (1–255 characters)
</ParamField>

<ParamField body="description" type="string">
  KPI description (max 2000 characters)
</ParamField>

<ParamField body="unit" type="string">
  Measurement unit (max 50 characters)
</ParamField>

<ParamField body="value_type" type="string">
  Answer type: `number`, `text`, `percentage`, `date`, `boolean`, `select`
</ParamField>

<ParamField body="required" type="boolean">
  Whether a response value is mandatory
</ParamField>

<ParamField body="sort_order" type="integer">
  Display order within the dataset (0-based)
</ParamField>

<ParamField body="options" type="array[object]">
  For `select` KPIs: full replacement array of options (2–100). Each object:

  | Field        | Type    | Description                                         |
  | ------------ | ------- | --------------------------------------------------- |
  | `id`         | string  | Existing option UUID to keep (omit for new options) |
  | `label`      | string  | Option label (1–255 chars)                          |
  | `sort_order` | integer | Display order                                       |
</ParamField>

## Response

Returns the updated KPI definition (HTTP 200).

<ResponseField name="id" type="string">
  KPI UUID.
</ResponseField>

<ResponseField name="dataset_id" type="string">
  Parent dataset UUID.
</ResponseField>

<ResponseField name="name" type="string">
  KPI display name.
</ResponseField>

<ResponseField name="description" type="string | null">
  KPI description.
</ResponseField>

<ResponseField name="unit" type="string | null">
  Measurement unit (e.g. `m³`, `kWh`).
</ResponseField>

<ResponseField name="value_type" type="string">
  Data type: `number`, `text`, `percentage`, `date`, `boolean`, or `select`.
</ResponseField>

<ResponseField name="required" type="boolean">
  Whether a response value is mandatory.
</ResponseField>

<ResponseField name="sort_order" type="integer">
  Display order within the dataset.
</ResponseField>

<ResponseField name="options" type="array[object]">
  Available choices for `select`-type KPIs. Each has `id`, `label`, and `sort_order`.
</ResponseField>

<ResponseField name="created_at" type="datetime">
  Creation timestamp (ISO 8601).
</ResponseField>

<ResponseField name="updated_at" type="string | null">
  Last update timestamp (ISO 8601).
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X PATCH "https://api.dcycle.io/v1/custom-kpi-datasets/${DATASET_ID}/kpis/${KPI_ID}" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{"name": "Total electricity consumed (all sources)", "unit": "MWh"}'
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import requests
  import os

  headers = {
      "x-api-key": os.getenv("DCYCLE_API_KEY"),
      "x-organization-id": os.getenv("DCYCLE_ORG_ID"),
      "Content-Type": "application/json",
  }

  dataset_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
  kpi_id = "c3d4e5f6-a7b8-9012-cdef-123456789012"

  response = requests.patch(
      f"https://api.dcycle.io/v1/custom-kpi-datasets/{dataset_id}/kpis/{kpi_id}",
      headers=headers,
      json={"name": "Total electricity consumed (all sources)", "unit": "MWh"},
  )

  kpi = response.json()
  print(f"Updated: {kpi['name']} [{kpi['unit']}]")
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const axios = require('axios');

  const headers = {
    'x-api-key': process.env.DCYCLE_API_KEY,
    'x-organization-id': process.env.DCYCLE_ORG_ID,
    'Content-Type': 'application/json',
  };

  const datasetId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
  const kpiId = 'c3d4e5f6-a7b8-9012-cdef-123456789012';

  axios.patch(`https://api.dcycle.io/v1/custom-kpi-datasets/${datasetId}/kpis/${kpiId}`, {
    name: 'Total electricity consumed (all sources)',
    unit: 'MWh',
  }, { headers })
  .then(response => console.log(`Updated: ${response.data.name}`));
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "c3d4e5f6-a7b8-9012-cdef-123456789012",
  "dataset_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "name": "Total electricity consumed (all sources)",
  "description": "Annual electricity consumption across all sources",
  "unit": "MWh",
  "value_type": "number",
  "required": true,
  "sort_order": 0,
  "options": [],
  "created_at": "2025-05-01T09:00:00Z",
  "updated_at": "2025-06-18T15:00:00Z"
}
```

## Common Errors

### 401 Unauthorized

**Cause:** Missing or invalid API key

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Invalid API key for organization", "code": "INVALID_API_KEY"}
```

### 403 Forbidden

**Cause:** The authenticated user is not a member of the organization

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Logged User is not Member of Organization", "code": "LOGGED_USER_NOT_MEMBER"}
```

### 404 Not Found

**Cause:** KPI not found

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": "KPI not found"
}
```

### 422 Validation Error

**Cause:** Empty options array for a select KPI

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body", "options"],
      "msg": "A select KPI needs at least 2 options.",
      "type": "value_error"
    }
  ]
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Create KPI" icon="plus" href="/api-reference/custom-kpi/create-kpi">
    Add a new KPI to the dataset
  </Card>

  <Card title="Delete KPI" icon="trash" href="/api-reference/custom-kpi/delete-kpi">
    Remove a KPI definition
  </Card>
</CardGroup>
