
## Update a department

`PATCH /v1/departments/:id`

Updates the fields of an existing department. Pass only the fields being changed.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|---------|
| `id` (path) | number | yes | Department ID. List: [`GET /v1/departments`](./list.md) |

## Request fields (body)

All fields are optional, but the body must contain at least one of them.

| Field | Type | Description |
|------|-----|---------|
| `name` | string | Department name |
| `parentId` | number | ID of the new parent department, used to move the department within the tree. Must reference an existing department. List: [`GET /v1/departments`](./list.md) |
| `headId` | number | ID of the new department head. Source: [`GET /v1/users`](/docs/entities/users) |
| `sort` | number | Order among departments at the same level |

Full field list — [Department fields](/docs/entities/departments/fields).

> **`headId` is not checked for existence.** A nonexistent `userId` in `headId` is accepted and stored, and the request returns `200`. Only `parentId` is checked: a nonexistent parent returns `422 BITRIX_ERROR`. Confirm the user exists via [`GET /v1/users`](/docs/entities/users) before assigning.

## Examples

### curl — personal key

```bash
curl -X PATCH "https://vibecode.bitrix24.com/v1/departments/47" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "West sales department",
    "headId": 101
  }'
```

### curl — OAuth application

```bash
curl -X PATCH "https://vibecode.bitrix24.com/v1/departments/47" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "West sales department",
    "headId": 101
  }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/departments/47', {
  method: 'PATCH',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'West sales department',
    headId: 101,
  }),
})

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

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/departments/47', {
  method: 'PATCH',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'West sales department',
    headId: 101,
  }),
})

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

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data` | object | The department after the update. Field descriptions — [Department fields](/docs/entities/departments/fields) |
| `data.id` | number | Department ID |
| `data.name` | string | Department name |
| `data.parentId` | number | Parent department ID |
| `data.headId` | number | Department head ID. If no head is assigned, the value is `0` or the field is absent |
| `data.sort` | number | Department order among siblings |

## Response example

```json
{
  "success": true,
  "data": {
    "id": 47,
    "name": "West sales department",
    "sort": 500,
    "parentId": 1,
    "headId": 101
  }
}
```

## Error response example

404 — department not found:

```json
{
  "success": false,
  "error": {
    "code": "ENTITY_NOT_FOUND",
    "message": "Department not found"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `EMPTY_UPDATE_BODY` | The request body is empty — pass at least one field |
| 400 | `READONLY_FIELD` | The body contains a read-only field, such as `id` |
| 404 | `ENTITY_NOT_FOUND` | No department with this `id` |
| 422 | `BITRIX_ERROR` | `parentId` references a nonexistent department |
| 403 | `SCOPE_DENIED` | The API key does not have the `department` scope |
| 401 | `TOKEN_MISSING` | The API key has no configured tokens |

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

## See also

- [Get a department](/docs/entities/departments/get)
- [Department fields](/docs/entities/departments/fields)
- [Batch](/docs/batch)
