## Link a company to a contact

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

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

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

## Parameters

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

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|----------|
| `companyId` | number | yes | Company ID. List: [`GET /v1/companies`](/docs/entities/companies/list). A string of digits is accepted too, for example `"3101"` |
| `isPrimary` | boolean | no | Make the company primary. The previous primary company loses the flag. The contact's first company becomes primary regardless of the value |
| `sort` | number | no | The company's position in the list. If omitted, the binding goes to the end of the list |

## Examples

### curl — personal key

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

### curl — OAuth application

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

### JavaScript — personal key

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

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

### JavaScript — OAuth application

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

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

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `success` | boolean | Always `true` on success |
| `data` | array | All of the contact's bindings after linking, in `sort` order |
| `data[].companyId` | number | Company ID. Get the company: [`GET /v1/companies/:id`](/docs/entities/companies/get) |
| `data[].isPrimary` | boolean | The contact's primary company. `true` on exactly one binding |
| `data[].sort` | number | The company's position in the list |
| `data[].roleId` | number | Binding role. The binding methods do not save it |

## Response example

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

```json
{
  "success": true,
  "data": [
    { "companyId": 3099, "sort": 10, "isPrimary": false, "roleId": 0 },
    { "companyId": 3101, "sort": 20, "isPrimary": true, "roleId": 0 }
  ]
}
```

## Error response example

409 — the company is already linked to this contact:

```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` | `companyId` is not passed or cannot be converted to a positive integer, for example `"abc"` or `0` |
| 404 | `ENTITY_NOT_FOUND` | There is no contact with this `id` |
| 409 | `RELATION_ALREADY_EXISTS` | The company is already linked to this contact |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |
| 401 | `TOKEN_MISSING` | No Bitrix24 tokens are attached to the key |
| 403 | `SCOPE_DENIED` | The key lacks 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

- **Company existence is not checked.** An ID that does not belong to any company is linked without an error and appears in the contact's list of bindings. Before linking, check the company with [`GET /v1/companies/:id`](/docs/entities/companies/get).
- **The role is not saved.** The `roleId` field in the body is accepted without an error but is not saved: in the response, the new binding has `roleId` equal to `0`.
- **Change the primary company or the order.** For companies that are already linked, use [`PUT /v1/contacts/:id/companies`](/docs/entities/contacts/companies-set) and pass the whole required set in `items`: companies not passed are unlinked.

## See also

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