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

# Link Entities to Project

> Link up to 100 entities by ID to a project

# Link Entities to Project

Link a batch of entities (by ID) to a project. Entities already linked are silently skipped — safe to call repeatedly with the same IDs.

<Note>
  **New API**: This endpoint is part of the new API architecture.
</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="project_id" type="string" required>
  The UUID of the project to link entities to

  **Example:** `b7f2a1c3-4d5e-6f7a-8b9c-0d1e2f3a4b5c`
</ParamField>

### Body Parameters

<ParamField body="entity_type" type="string" required>
  The type of entities being linked.

  **Available values:** `logistic_requests`, `logistic_recharges`, `logistic_packages`, `invoices`, `file_readings`
</ParamField>

<ParamField body="entity_ids" type="array[string]" required>
  List of entity UUIDs to link. Minimum 1, maximum 100 per request.

  **Example:** `["550e8400-e29b-41d4-a716-446655440000", "660e8400-e29b-41d4-a716-446655440001"]`
</ParamField>

## Response

<ResponseField name="created" type="integer">
  Number of new links created
</ResponseField>

<ResponseField name="skipped" type="integer">
  Number of entities that were already linked (skipped)
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST "https://api.dcycle.io/v1/projects/b7f2a1c3-4d5e-6f7a-8b9c-0d1e2f3a4b5c/entities/link" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "entity_type": "logistic_requests",
      "entity_ids": [
        "550e8400-e29b-41d4-a716-446655440000",
        "660e8400-e29b-41d4-a716-446655440001"
      ]
    }'
  ```

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

  api_key = os.getenv("DCYCLE_API_KEY")
  org_id = os.getenv("DCYCLE_ORG_ID")
  project_id = "b7f2a1c3-4d5e-6f7a-8b9c-0d1e2f3a4b5c"

  headers = {
      "x-api-key": api_key,
      "x-organization-id": org_id,
      "Content-Type": "application/json"
  }

  payload = {
      "entity_type": "logistic_requests",
      "entity_ids": [
          "550e8400-e29b-41d4-a716-446655440000",
          "660e8400-e29b-41d4-a716-446655440001"
      ]
  }

  response = requests.post(
      f"https://api.dcycle.io/v1/projects/{project_id}/entities/link",
      headers=headers,
      json=payload
  )

  result = response.json()
  print(f"Created: {result['created']}, Skipped: {result['skipped']}")
  ```

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

  const apiKey = process.env.DCYCLE_API_KEY;
  const orgId = process.env.DCYCLE_ORG_ID;
  const projectId = 'b7f2a1c3-4d5e-6f7a-8b9c-0d1e2f3a4b5c';

  const headers = {
    'x-api-key': apiKey,
    'x-organization-id': orgId,
    'Content-Type': 'application/json'
  };

  const payload = {
    entity_type: 'logistic_requests',
    entity_ids: [
      '550e8400-e29b-41d4-a716-446655440000',
      '660e8400-e29b-41d4-a716-446655440001'
    ]
  };

  axios.post(
    `https://api.dcycle.io/v1/projects/${projectId}/entities/link`,
    payload,
    { headers }
  )
  .then(response => {
    console.log(`Created: ${response.data.created}, Skipped: ${response.data.skipped}`);
  })
  .catch(error => console.error(error));
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "created": 2,
  "skipped": 0
}
```

## Common Errors

### 404 Not Found

**Cause:** Project not found or doesn't belong to your organization

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

### 422 Validation Error

**Cause:** Invalid `entity_type` or empty `entity_ids`

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body", "entity_type"],
      "msg": "value is not a valid enumeration member",
      "type": "type_error.enum"
    }
  ]
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Unlink Entities" icon="link-slash" href="/api-reference/projects/unlink-entities">
    Remove entity-project associations
  </Card>

  <Card title="Link by Filters" icon="filter" href="/api-reference/projects/link-by-filters">
    Bulk-link all entities matching a filter
  </Card>
</CardGroup>
