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

# Create Assignment

> Assign a data owner to a dataset for a specific organization or facility

# Create Assignment

Create a new assignment linking a data owner to a dataset. Each assignment specifies who will receive campaign invitations and submit KPI values for a given organization (and optionally a specific facility).

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

  **Example:** `"a1b2c3d4-e5f6-7890-abcd-ef1234567890"`
</ParamField>

### Body Parameters

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

<ParamField body="data_owner_email" type="string" required>
  Email address of the data owner who will receive campaign invitations

  **Example:** `"facility-manager@example.com"`
</ParamField>

<ParamField body="facility_id" type="string">
  UUID of a specific facility. Omit for an organization-level assignment.
</ParamField>

<ParamField body="data_owner_user_id" type="string">
  UUID of an existing Dcycle user. If provided, the user sees in-app KPI forms.
</ParamField>

## Response

Returns the created assignment (HTTP 201).

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

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

<ResponseField name="organization_id" type="string">
  Organization UUID the assignment targets.
</ResponseField>

<ResponseField name="facility_id" type="string | null">
  Facility UUID if the assignment is facility-scoped.
</ResponseField>

<ResponseField name="data_owner_user_id" type="string | null">
  User UUID of the data owner (if they have a Dcycle account).
</ResponseField>

<ResponseField name="data_owner_email" type="string">
  Email address of the recipient.
</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 POST "https://api.dcycle.io/v1/custom-kpi-datasets/${DATASET_ID}/assignments" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "organization_id": "a8315ef3-dd50-43f8-b7ce-d839e68d51fa",
      "data_owner_email": "facility-manager@example.com",
      "facility_id": "550e8400-e29b-41d4-a716-446655440000"
    }'
  ```

  ```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}/assignments",
      headers=headers,
      json={
          "organization_id": "a8315ef3-dd50-43f8-b7ce-d839e68d51fa",
          "data_owner_email": "facility-manager@example.com",
          "facility_id": "550e8400-e29b-41d4-a716-446655440000",
      },
  )

  assignment = response.json()
  print(f"Assigned: {assignment['data_owner_email']} → {assignment['id']}")
  ```

  ```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}/assignments`, {
    organization_id: 'a8315ef3-dd50-43f8-b7ce-d839e68d51fa',
    data_owner_email: 'facility-manager@example.com',
    facility_id: '550e8400-e29b-41d4-a716-446655440000',
  }, { headers })
  .then(response => console.log(`Assigned: ${response.data.id}`));
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "d4e5f6a7-b8c9-0123-defa-234567890123",
  "dataset_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "organization_id": "a8315ef3-dd50-43f8-b7ce-d839e68d51fa",
  "facility_id": "550e8400-e29b-41d4-a716-446655440000",
  "data_owner_user_id": null,
  "data_owner_email": "facility-manager@example.com",
  "created_at": "2025-02-01T09:00:00Z",
  "updated_at": null
}
```

## 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:** Dataset not found

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

### 422 Validation Error

**Cause:** Invalid email address

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body", "data_owner_email"],
      "msg": "value is not a valid email address",
      "type": "value_error.email"
    }
  ]
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="List Assignments" icon="list" href="/api-reference/custom-kpi/list-assignments">
    View all data owner assignments
  </Card>

  <Card title="Update Assignment" icon="pencil" href="/api-reference/custom-kpi/update-assignment">
    Change the data owner
  </Card>
</CardGroup>
