> ## 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.

# Submit Response

> Submit KPI values and optional evidence attachments from the authenticated in-app flow

# Submit Response

Submit a Custom KPI response with values and optional evidence attachments. This is the **authenticated** submission flow — the caller must be a logged-in member of the organization.

<Note>
  For submissions from external data owners via magic-link, see [Submit Public Form](/api-reference/custom-kpi/submit-public-form) instead.
</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>

### Body Parameters

<ParamField body="organization_id" type="string" required>
  UUID of the organization this response belongs to
</ParamField>

<ParamField body="campaign_id" type="string" required>
  UUID of the campaign being responded to
</ParamField>

<ParamField body="facility_id" type="string">
  UUID of the facility (optional — used to match an assignment)
</ParamField>

<ParamField body="values" type="array[object]" required>
  KPI values to submit (1–100 entries, no duplicate `kpi_id`). Each entry:

  | Field           | Type   | Description                                              |
  | --------------- | ------ | -------------------------------------------------------- |
  | `kpi_id`        | string | UUID of the KPI definition                               |
  | `value_numeric` | number | For `number`/`percentage` types (nullable)               |
  | `value_text`    | string | For `text` type, max 10 000 chars (nullable)             |
  | `value_date`    | string | For `date` type, ISO date (nullable)                     |
  | `option_id`     | string | For `select` type — UUID of the chosen option (nullable) |
</ParamField>

<ParamField body="attachment_file_ids" type="array[string]">
  UUIDs of evidence files previously uploaded via [Presigned URL](/api-reference/custom-kpi/presigned-url). Max 20 files.
</ParamField>

## Response

Returns the created response set with saved values and attachment metadata (HTTP 201).

<ResponseField name="response_set_id" type="string">Response set UUID</ResponseField>
<ResponseField name="campaign_assignment_id" type="string">Resolved campaign-assignment UUID</ResponseField>
<ResponseField name="submitted_at" type="string">ISO 8601 timestamp</ResponseField>
<ResponseField name="values" type="array[object]">Saved value objects</ResponseField>

<ResponseField name="attachments" type="array[object]">
  Attachment metadata:

  | Field       | Type   | Description            |
  | ----------- | ------ | ---------------------- |
  | `file_id`   | string | File UUID              |
  | `file_name` | string | Original file name     |
  | `file_url`  | string | Presigned download URL |
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST "https://api.dcycle.io/v1/custom-kpi-datasets/${DATASET_ID}/responses" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "campaign_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "values": [
        {"kpi_id": "c3d4e5f6-a7b8-9012-cdef-123456789012", "value_numeric": 42.5},
        {"kpi_id": "d4e5f6a7-b8c9-0123-defa-234567890123", "value_text": "Completed audit"}
      ],
      "attachment_file_ids": ["e5f6a7b8-c9d0-1234-efab-345678901234"]
    }'
  ```

  ```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"

  response = requests.post(
      f"https://api.dcycle.io/v1/custom-kpi-datasets/{dataset_id}/responses",
      headers=headers,
      json={
          "organization_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
          "campaign_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "values": [
              {"kpi_id": "c3d4e5f6-a7b8-9012-cdef-123456789012", "value_numeric": 42.5},
          ],
      },
  )

  result = response.json()
  print(f"Response set: {result['response_set_id']}, {len(result['values'])} values saved")
  ```

  ```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';

  axios.post(`https://api.dcycle.io/v1/custom-kpi-datasets/${datasetId}/responses`, {
    organization_id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
    campaign_id: 'b2c3d4e5-f6a7-8901-bcde-f12345678901',
    values: [
      { kpi_id: 'c3d4e5f6-a7b8-9012-cdef-123456789012', value_numeric: 42.5 },
    ],
  }, { headers })
  .then(response => {
    console.log(`Response set: ${response.data.response_set_id}`);
    console.log(`Values saved: ${response.data.values.length}`);
  });
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "response_set_id": "f1a2b3c4-d5e6-7890-abcd-ef9876543210",
  "campaign_assignment_id": "f1a2b3c4-d5e6-7890-abcd-ef1234567890",
  "submitted_at": "2025-06-20T14:30:00Z",
  "values": [
    {
      "id": "e1f2a3b4-c5d6-7890-abcd-ef1234567890",
      "assignment_id": "d4e5f6a7-b8c9-0123-defa-234567890123",
      "response_set_id": "f1a2b3c4-d5e6-7890-abcd-ef9876543210",
      "kpi_id": "c3d4e5f6-a7b8-9012-cdef-123456789012",
      "value_numeric": 42.5,
      "value_text": null,
      "value_date": null,
      "option_id": null,
      "submitted_at": "2025-06-20T14:30:00Z",
      "submitter_email": "admin@company.com",
      "submitter_user_id": "b9f1e2d3-0000-0000-0000-000000000001",
      "created_at": "2025-06-20T14:30:00Z"
    }
  ],
  "attachments": [
    {
      "file_id": "e5f6a7b8-c9d0-1234-efab-345678901234",
      "file_name": "energy-audit-2025.pdf",
      "file_url": "https://storage.dcycle.io/..."
    }
  ]
}
```

## 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"}
```

### 422 Validation Error

**Cause:** Duplicate `kpi_id` entries in the values array

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body", "values"],
      "msg": "Duplicate kpi_id entries are not allowed in a single submission",
      "type": "value_error"
    }
  ]
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Presigned URL" icon="cloud-arrow-up" href="/api-reference/custom-kpi/presigned-url">
    Upload evidence files before submitting
  </Card>

  <Card title="Dataset Values" icon="table" href="/api-reference/custom-kpi/list-dataset-values">
    View all submitted values across campaigns
  </Card>
</CardGroup>
