## Link a contact to a company

`POST /v1/companies/:id/contacts`

Links one contact to a company; the company's other contacts stay linked.

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

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|----------|
| `id` (path) | number | yes | Company ID. List: [`GET /v1/companies`](/docs/entities/companies/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 `"2989"` |
| `isPrimary` | boolean | no | Ignored: linking always makes the company primary for the contact, and the new link has `isPrimary: true` in the response |
| `sort` | number | no | The contact's position in the list. If omitted, the link is added to the end of the list |

## Examples

### curl — personal key

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

### curl — OAuth application

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

### JavaScript — personal key

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

const { success, data } = await res.json()
console.log('Contacts linked to the company:', data.length)
```

### JavaScript — OAuth application

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

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

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `success` | boolean | Always `true` on success |
| `data` | array | All of the company's links after linking, in `sort` order |
| `data[].contactId` | number | Contact ID. Get the contact: [`GET /v1/contacts/:id`](/docs/entities/contacts/get) |
| `data[].isPrimary` | boolean | The company is primary for this contact: its ID arrives in the contact's `companyId` field |
| `data[].sort` | number | The contact's position in the list |
| `data[].roleId` | number | The contact's role in the company. The link methods do not save it |

## Response example

A second contact is linked to a company that has one contact:

```json
{
  "success": true,
  "data": [
    { "contactId": 2987, "sort": 10, "isPrimary": true, "roleId": 0 },
    { "contactId": 2989, "sort": 20, "isPrimary": true, "roleId": 0 }
  ]
}
```

## Error response example

409 — the contact is already linked to this company:

```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 company with this `id` |
| 409 | `RELATION_ALREADY_EXISTS` | The contact is already linked to this company |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |
| 401 | `TOKEN_MISSING` | The API key has no Bitrix24 tokens configured |
| 403 | `SCOPE_DENIED` | The key has no `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

- **The company becomes the contact's primary company.** After linking, the contact's `companyId` field contains the ID of this company, and the contact's link to its previous primary company gets `isPrimary: false`.
- **Contact existence is not checked.** An ID that does not belong to any contact is linked without an error and appears in the company's list of links. Before linking, check the contact with [`GET /v1/contacts/:id`](/docs/entities/contacts/get).
- **The role is not saved.** The `roleId` field in the body is accepted without an error but is not saved: the new link has `roleId` equal to `0` in the response.
- **Position of an already linked contact.** Linking the same contact again answers `409`. To change the position of contacts that are already linked, use [`PUT /v1/companies/:id/contacts`](/docs/entities/companies/contacts-set) and pass the full desired set of contacts in `items`: contacts missing from `items` are unlinked.

## See also

- [Company contacts](/docs/entities/companies/contacts)
- [List company contacts](/docs/entities/companies/contacts-list)
- [Replace company contacts](/docs/entities/companies/contacts-set)
- [Unlink a contact from a company](/docs/entities/companies/contacts-delete-delete)
- [Unlink all contacts from a company](/docs/entities/companies/contacts-delete)
- [Contacts](/docs/entities/contacts)
