## Link a contact to a lead

`POST /v1/leads/:id/contacts`

Links one contact to a lead. The lead's other contacts stay linked.

To set the full list of contacts in one call, use [`PUT /v1/leads/:id/contacts`](/docs/entities/leads/contacts-set).

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|----------|
| `id` (path) | number | yes | Lead ID. List: [`GET /v1/leads`](/docs/entities/leads/list) |

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|----------|
| `contactId` | number | yes | Contact ID. List: [`GET /v1/contacts`](/docs/entities/contacts/list). A string of digits is also accepted, for example `"2939"` |
| `isPrimary` | boolean | no | Make the contact primary. The previous primary contact loses the flag. The first contact of a lead becomes primary regardless of the value |
| `sort` | number | no | Position of the contact in the list. If omitted, the link goes to the end of the list |

## Examples

### curl — personal key

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/leads/1001321/contacts" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "contactId": 2939, "isPrimary": true, "sort": 20 }'
```

### curl — OAuth application

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/leads/1001321/contacts" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "contactId": 2939, "isPrimary": true, "sort": 20 }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/leads/1001321/contacts', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ contactId: 2939, isPrimary: true, sort: 20 }),
})

const { success, data } = await res.json()
console.log('Lead contacts:', data.length)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/leads/1001321/contacts', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ contactId: 2939, isPrimary: true, sort: 20 }),
})

const { success, data } = await res.json()
```

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `success` | boolean | Always `true` on success |
| `data` | array | All links of the lead after linking, in `sort` order |
| `data[].contactId` | number | Contact ID. Get the contact: [`GET /v1/contacts/:id`](/docs/entities/contacts/get) |
| `data[].isPrimary` | boolean | Primary contact of the lead. `true` for exactly one link |
| `data[].sort` | number | Position of the contact in the list |
| `data[].roleId` | number | Role of the contact in the lead. The link methods do not store it |

## Response example

A second contact is linked to a lead that had one contact, and the new contact becomes primary:

```json
{
  "success": true,
  "data": [
    { "contactId": 2937, "sort": 10, "isPrimary": false, "roleId": 0 },
    { "contactId": 2939, "sort": 20, "isPrimary": true, "roleId": 0 }
  ]
}
```

## Error response example

409 — the contact is already linked to this lead:

```json
{
  "success": false,
  "error": {
    "code": "RELATION_ALREADY_EXISTS",
    "message": "This binding already exists"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|----------|
| 400 | `INVALID_PARAMS` | `id` is not a positive integer |
| 400 | `MISSING_REQUIRED_FIELDS` | `contactId` is missing or is not a positive integer |
| 404 | `ENTITY_NOT_FOUND` | No lead with this `id` exists |
| 409 | `RELATION_ALREADY_EXISTS` | The contact is already linked to this lead |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |
| 401 | `TOKEN_MISSING` | The key has no Bitrix24 tokens attached |
| 403 | `SCOPE_DENIED` | The key does not have the `crm` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is in read-only mode. Linking requires a key with write access |
| 429 | `RATE_LIMITED` | Rate limit exceeded. Retry after the delay in the `Retry-After` header |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is unavailable. Retry the request later |

Full list of common API errors — [Errors](/docs/errors).

## Known specifics

- **Contact existence is not checked.** An ID that matches no contact is linked without an error and appears in the lead's list of links. Before linking, check the contact with [`GET /v1/contacts/:id`](/docs/entities/contacts/get).
- **The role is not stored.** The `roleId` field in the body is accepted without an error but is not stored: in the response, the new link has `roleId` equal to `0`.
- **Replacement changes the primary contact and the order.** Change the primary contact or the order of already linked contacts with [`PUT /v1/leads/:id/contacts`](/docs/entities/leads/contacts-set) and pass the full desired list in `items`: contacts missing from `items` are unlinked. `PATCH /v1/leads/:id` with the `contactId` of an already linked contact responds with `200` but does not change the primary contact.

## See also

- [Lead contacts](/docs/entities/leads/contacts)
- [List lead contacts](/docs/entities/leads/contacts-list)
- [Replace lead contacts](/docs/entities/leads/contacts-set)
- [Unlink a contact from a lead](/docs/entities/leads/contacts-delete-delete)
- [Unlink all contacts from a lead](/docs/entities/leads/contacts-delete)
- [Contacts](/docs/entities/contacts)
