## Replace contact companies

`PUT /v1/contacts/:id/companies`

Defines the contact's entire set of companies in one call: links the companies from the list and unlinks all the others.

To add one company without unlinking the others, use [`POST /v1/contacts/:id/companies`](/docs/entities/contacts/companies-create).

## 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 |
|------|-----|:-----:|----------|
| `items` | array | yes | The contact's new set of companies, including already linked companies that must stay. Companies missing from the array are unlinked. An empty array `[]` unlinks all companies |
| `items[].companyId` | number | yes | Company ID. List: [`GET /v1/companies`](/docs/entities/companies/list). A string of digits is accepted too, for example `"3099"` |
| `items[].isPrimary` | boolean | no | The contact's primary company. If no element has `true`, the first element of the array becomes primary, even if it does not have the lowest `sort`. If several have `true`, the first of them becomes primary. To keep the primary company unchanged, pass `isPrimary: true` for it |
| `items[].sort` | number | no | The company's position in the list. If omitted, it is assigned based on the element's position in the array |

## Examples

### curl — personal key

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

### curl — OAuth application

```bash
curl -X PUT "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 '{
    "items": [
      { "companyId": 3103, "sort": 10 },
      { "companyId": 3099, "isPrimary": true, "sort": 20 }
    ]
  }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/contacts/2943/companies', {
  method: 'PUT',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    items: [
      { companyId: 3103, sort: 10 },
      { companyId: 3099, 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: 'PUT',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    items: [
      { companyId: 3103, sort: 10 },
      { companyId: 3099, 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 the replacement, in `sort` order. After `"items": []` — an empty array |
| `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

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

## Error response example

400 — the body has no `items` array:

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "items must be an array (use [] to unlink all)"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|----------|
| 400 | `INVALID_PARAMS` | `id` is not a positive integer |
| 400 | `INVALID_PARAMS` | `items` is not passed or is not an array |
| 400 | `INVALID_PARAMS` | An `items` element is not an object. The message starts with `items[0] must be an object`, where `0` is the element's index |
| 400 | `INVALID_PARAMS` | An `items` element has no `companyId`, or it cannot be converted to a positive integer, for example `0` or `"abc"`. Message: `items[0] requires 'companyId'`, where `0` is the element's index |
| 404 | `ENTITY_NOT_FOUND` | There is no contact with this `id` |
| 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. Replacing 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 replacing, check the companies with [`GET /v1/companies/:id`](/docs/entities/companies/get).
- **The role is not saved.** The `roleId` field in an `items` element is accepted without an error but is not saved: in the response, `roleId` is `0`.

## See also

- [Contact companies](/docs/entities/contacts/companies)
- [List contact companies](/docs/entities/contacts/companies-list)
- [Link a company to a contact](/docs/entities/contacts/companies-create)
- [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)
