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

# Link Document to Entity

> Attach a document to a contact, company, deal, event, matter, or task

## Request

### Path Parameters

<ParamField path="id" type="string" required>
  Document UUID.
</ParamField>

### Headers

```
Authorization: Bearer wbk_your_api_key_here
Content-Type: application/json
```

<ParamField header="Idempotency-Key" type="string">
  Optional UUID for retry deduplication.
</ParamField>

### Body Parameters

<ParamField body="entity_type" type="string" required>
  One of `contact`, `company`, `deal`, `event`, `matter`, or `task`.
</ParamField>

<ParamField body="entity_id" type="string" required>
  UUID of the target entity.
</ParamField>

<ParamField body="role" type="string">
  Optional relationship label (e.g. `signed`, `draft`, `evidence`). Free-form string, max 64 characters.
</ParamField>

## Response

<ResponseField name="data" type="object">
  <Expandable title="properties">
    <ResponseField name="link_id" type="string">UUID of the newly created link</ResponseField>
    <ResponseField name="document_id" type="string">Document UUID</ResponseField>
    <ResponseField name="entity_type" type="string">Entity type</ResponseField>
    <ResponseField name="entity_id" type="string">Entity UUID</ResponseField>
    <ResponseField name="role" type="string">Optional relationship label</ResponseField>
    <ResponseField name="created_at" type="string">ISO 8601 timestamp</ResponseField>
  </Expandable>
</ResponseField>

A document may be linked to multiple entities simultaneously; the same `(document_id, entity_type, entity_id)` triple returns the existing link rather than creating duplicates. Responses include `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset`, and `X-Request-ID`.

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST \
    https://data.leadlex.com/functions/v1/api-gateway/v1/documents/doc_01HY1/link \
    -H "Authorization: Bearer wbk_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
      "entity_type": "contact",
      "entity_id": "123e4567-e89b-12d3-a456-426614174000",
      "role": "signed"
    }'
  ```

  ```python Python theme={null}
  import requests

  API_KEY = "wbk_your_api_key_here"
  BASE_URL = "https://data.leadlex.com/functions/v1/api-gateway"
  doc_id = "doc_01HY1"

  r = requests.post(
      f"{BASE_URL}/v1/documents/{doc_id}/link",
      headers={
          "Authorization": f"Bearer {API_KEY}",
          "Content-Type": "application/json",
      },
      json={
          "entity_type": "deal",
          "entity_id": "deal_01HY1",
          "role": "signed",
      },
  )
  print(r.json()["data"]["link_id"])
  ```

  ```javascript JavaScript theme={null}
  const docId = 'doc_01HY1';
  const res = await fetch(
    `https://data.leadlex.com/functions/v1/api-gateway/v1/documents/${docId}/link`,
    {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer wbk_your_api_key_here',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        entity_type: 'deal',
        entity_id: 'deal_01HY1',
        role: 'signed',
      }),
    }
  );
  const { data } = await res.json();
  console.log(data.link_id);
  ```
</CodeGroup>

### Example Response

```json theme={null}
{
  "data": {
    "link_id": "dlnk_01HY1",
    "document_id": "doc_01HY1",
    "entity_type": "deal",
    "entity_id": "deal_01HY1",
    "role": "signed",
    "created_at": "2026-04-17T10:55:00Z"
  }
}
```

## Errors

| Status | Code                       | Description                                      |
| ------ | -------------------------- | ------------------------------------------------ |
| 400    | `validation_error`         | Unsupported `entity_type` or missing `entity_id` |
| 401    | `invalid_key`              | Invalid or expired API key                       |
| 403    | `insufficient_permissions` | Missing `write:documents` permission             |
| 404    | `not_found`                | Document or entity does not exist                |
| 429    | `rate_limited`             | Rate limit exceeded                              |
