## Reorder chat folders

`PUT /v1/chats/folders/order`

Saves the order of the current user's chat folders in the interface, both system and personal.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `folderIds` | number[] | yes | IDs of all the user's visible folders in the new order, both system and personal. Each element is a positive integer: a number or a string of digits. The folder ID is the `id` field in [`GET /v1/chats/folders`](/docs/chats/discovery/folders) |

There are no query parameters. Any query parameter or other body field is rejected with `400 INVALID_PARAMS`.

## Examples

### curl — personal key

```bash
curl -X PUT "https://vibecode.bitrix24.com/v1/chats/folders/order" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"folderIds": [27, 29, 31, 33, 35, 37, 127]}'
```

### curl — OAuth application

```bash
curl -X PUT "https://vibecode.bitrix24.com/v1/chats/folders/order" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"folderIds": [27, 29, 31, 33, 35, 37, 127]}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/folders/order', {
  method: 'PUT',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ folderIds: [27, 29, 31, 33, 35, 37, 127] }),
})

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

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/folders/order', {
  method: 'PUT',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ folderIds: [27, 29, 31, 33, 35, 37, 127] }),
})

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

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.result` | boolean | `true` — the order is saved |

## Response example

```json
{
  "success": true,
  "data": {
    "result": true
  }
}
```

## Error response example

422 — `folderIds` does not list all visible folders:

```json
{
  "success": false,
  "error": {
    "code": "BITRIX_ERROR",
    "message": "FOLDER_SORT_INVALID",
    "b24Code": "FOLDER_SORT_INVALID"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | `folderIds` is missing or not an array, an element is not a positive integer, a query parameter or an unknown body field is passed, or the body is not a JSON object. Checked before the Bitrix24 call |
| 422 | `BITRIX_ERROR` | Bitrix24 returned an error; the Bitrix24 code is in `error.b24Code`. `FOLDER_SORT_INVALID` — the set of `folderIds` does not match the user's visible folders |
| 404 | `ENTITY_NOT_FOUND` | Bitrix24 answered "not found"; the Bitrix24 code is in `error.b24Code` |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | Read-only key: reordering folders is a write, see [access rights](/docs/access-rights) |
| 401 | `TOKEN_MISSING` | The API key has no Bitrix24 tokens configured |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is unavailable or returned a server error |

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

## Known specifics

- **Take the set for `folderIds` from [`GET /v1/chats/folders`](/docs/chats/discovery/folders) right before the call.** A folder created or deleted after the list was read makes the set stale, and the call returns `FOLDER_SORT_INVALID`.
- **Bitrix24 recalculates the `sort` values itself.** After the call they may differ from the previous ones even when the folder order is the same.

## See also

- [Chat folders](/docs/chats/discovery/folders)
- [Create a chat folder](/docs/chats/management/folders-create)
- [Delete a chat folder](/docs/chats/management/folders-delete)
- [Sources of the "All" folder](/docs/chats/management/folder-sources)
