# User department

The user's department in the company structure — the lowest level the user belongs to: its ID and name.

## Get the department

`GET /v1/chats/users/:userId/department`

The v2 messenger method `im.v2.User.getDepartment`.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|----------|
| `userId` (path) | string | yes | User ID — a positive integer, or `me` for the key owner |

There are no query parameters: any parameter is rejected with `400 INVALID_PARAMS`.

## Examples

### curl — personal key

```bash
curl "https://vibecode.bitrix24.com/v1/chats/users/me/department" \
  -H "X-Api-Key: YOUR_API_KEY"
```

### curl — OAuth application

```bash
curl "https://vibecode.bitrix24.com/v1/chats/users/4/department" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN"
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/users/me/department', {
  headers: { 'X-Api-Key': 'YOUR_API_KEY' },
})

const { data } = await res.json()
console.log('My department:', data.name)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/users/4/department', {
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
  },
})

const { data } = await res.json()
console.log('Department:', data.id, data.name)
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.id` | number \| null | Department ID — a node of the company structure, as in [org structure nodes](/docs/humanresources/nodes); `null` — the user is not in any department |
| `data.name` | string \| null | Department name |

## Response example

```json
{
  "success": true,
  "data": { "id": 1, "name": "Sales" }
}
```

## Error response example

422 — the user was not found:

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

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | `userId` is neither a positive integer nor `me`, or a query parameter was passed |
| 401 | `TOKEN_MISSING` | The API key has no Bitrix24 tokens configured |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 422 | `BITRIX_ERROR` | Bitrix24 returned an error; the portal code is in `error.b24Code`. `USER_NOT_FOUND` — the user does not exist |
| 502 | `ME_ALIAS_RESOLUTION_FAILED` | Failed to resolve the key owner for `me` |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is unavailable or returned a server error |

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

## Known specifics

- If the user belongs to several departments, the lowest one in the structure is returned.
- Bitrix24 returns the department of any user by ID without checking whether that user is visible to the caller.

## See also

- [Employees](/docs/entities/users)
- [Org structure nodes](/docs/humanresources/nodes)
- [Service methods](/docs/chats/service)
