Update a Department via REST API v2
This endpoint changes one department. It requires the departments:write scope, and the acting user is either the API key owner or the user named in the X-On-Behalf-Of header. The call waits for the change and returns the department with its parent and approvers as 200, or returns 202 Accepted with an operation to poll.
Endpoint and authorization
The request is a PATCH to /v2/departments/{id}. The id path parameter is required and identifies the department to change. Send the API key as Authorization: Bearer vt_live_<secret>. The required scope is departments:write.
Two optional headers change how the write behaves. Idempotency-Key makes the write safe to retry: keys are retained for 24 hours per API key, and a retry with the same key and same request replays the original response with the Idempotent-Replayed header. X-On-Behalf-Of names the user the write is performed for. To use it, the key needs the users:act-on-behalf scope and its owner must manage the target user; if either condition is missing, the call returns 403 ON_BEHALF_OF_NOT_ALLOWED.
Merge semantics: what is kept and what is replaced
Send only the fields you want to change. Everything you omit is kept from the department's current state, members and approvers. Two fields are exceptions to that rule because they are full-replacement lists:
- userIds is a full-replacement list: whatever you send becomes the entire membership. Anyone who was in the department but is absent from your list is reassigned to the company's default department.
- approvers is also a full-replacement list: whatever you send becomes the entire approver set, and each userId may appear only once.
parentId moves the department in the hierarchy. The new parent cannot be the department itself or any department beneath it; that attempt fails with 409 DEPARTMENT_CIRCULAR_PARENT. To hand the default flag to a different department, set that department's isDefault to true. Trying to clear the flag on the current default with isDefault: false fails with 409 DEFAULT_DEPARTMENT_UNSET_FORBIDDEN.
Request body field map
The body is application/json and is required. All fields are optional; send only the ones you intend to change.
- name (string, minimum length 1): the department name.
- approvers (array of DepartmentApproverInput, minimum 1 item): the full replacement approver list. Each entry has a required userId and an optional levels array of FIRST or SECOND, defaulting to FIRST. SECOND requires a plan with multi-level approval.
- userIds (array of string): the full replacement membership. Users missing from the list are moved to the company's default department.
- parentId (string, minimum length 1): the ID of the new parent department.
- isDefault (boolean): whether this is the company's default department.
- watchersToNotify (array of email strings): addresses notified about leave requests in this department.
- keepLeaveRequestsOpen (boolean): whether leave requests in this department stay open instead of auto-expiring.
Responses: 200 versus 202
A 200 response returns the updated department in the data object, with id, name, isDefault, createdAt, optional parentId, and the approvers array. Each approver carries a userId and the levels they approve at; FIRST is used when no level is stored. Approvers are written by a background process, so they may briefly lag right after a write.
A 202 response means the write is still running, or it succeeded but the result could not be read back yet. The body carries an operation object with id, status and url, and the Location header carries the same URL. Poll that URL until the status is SUCCEEDED or FAILED. A status of UNKNOWN means no result arrived within 15 minutes; the change may or may not have been applied, so read the resource to check. A result that arrives later still changes the status to SUCCEEDED or FAILED.
Error codes and what they mean
Errors use a common envelope with error.code, error.name, error.description, error.resolution, error.requestId and optional error.details. The codes this endpoint can return are:
- 400 VALIDATION_ERROR with details.fields listing the fields that failed.
- 400 INVALID_REQUEST_BODY.
- 403 ON_BEHALF_OF_NOT_ALLOWED.
- 403 FORBIDDEN.
- 403 FEATURE_NOT_IN_PLAN when a SECOND approval level is sent without multi-level approval in the plan.
- 404 RESOURCE_NOT_FOUND when the department does not exist or is outside the key's data reach.
- 409 DEFAULT_DEPARTMENT_UNSET_FORBIDDEN.
- 409 DEPARTMENT_CIRCULAR_PARENT.
- 409 DUPLICATE_NAME.
- 422 UNKNOWN_USER_IDS.
- The Idempotency-Key codes: 422 IDEMPOTENCY_KEY_REUSED, 409 IDEMPOTENCY_REQUEST_IN_PROGRESS, and 400 INVALID_IDEMPOTENCY_KEY.
Include error.requestId when contacting support.
Pre-flight checklist
Before sending the PATCH, confirm the following:
- The key has departments:write, and users:act-on-behalf if you are using X-On-Behalf-Of.
- You are sending only the fields you intend to change; omitted fields are kept.
- If you send userIds, you have included every user who should remain in the department, because anyone missing moves to the company's default department.
- If you send approvers, each userId appears only once, and any SECOND level is covered by the plan.
- If you send parentId, it is not this department or one of its sub-departments.
- If you send isDefault: false, this is not the current default department; to change the default, set another department's isDefault to true.
- You have an Idempotency-Key ready if the write may be retried.
Worked example: rename and replace approvers
The reference includes two example bodies. The rename example sends {"name": "Platform Team"} and leaves membership and approvers untouched. The replace-approvers example sends an approvers array with one entry: userId user-0f3a9c1e-6c2b-4a5d-9e7f-1b2c3d4e5f60 with levels FIRST and SECOND. Because approvers is a full-replacement list, that body replaces the entire approver list with this single approver; any approver not listed is removed. If the plan does not include multi-level approval, the SECOND level triggers 403 FEATURE_NOT_IN_PLAN.
A successful 200 response returns the department with its parent and approvers. A 202 response returns an operation to poll.
| Field | Type | Behavior when sent |
|---|---|---|
| name | string | Changes the department name; minimum length 1. |
| approvers | array of DepartmentApproverInput | Full replacement of the approver list; each userId may appear only once. |
| userIds | array of string | Full replacement of membership; users missing from the list move to the company's default department. |
| parentId | string | Moves the department in the hierarchy; cannot be itself or a sub-department. |
| isDefault | boolean | false for the current default fails with 409 DEFAULT_DEPARTMENT_UNSET_FORBIDDEN. |
| watchersToNotify | array of email strings | Addresses notified about leave requests in this department. |
| keepLeaveRequestsOpen | boolean | Whether leave requests stay open instead of auto-expiring. |
Field types and constraints are taken from the endpoint's request schema; the behavior column summarizes the merge and replacement rules stated for this endpoint.
| Code | Status | Meaning |
|---|---|---|
| DEFAULT_DEPARTMENT_UNSET_FORBIDDEN | 409 | Attempted to set the current default department's isDefault to false. |
| DEPARTMENT_CIRCULAR_PARENT | 409 | Attempted to make the department a sub-department of itself or one of its sub-departments. |
| DUPLICATE_NAME | 409 | The requested name conflicts with an existing department name. |
| UNKNOWN_USER_IDS | 422 | One or more user IDs in the request do not resolve. |
| FEATURE_NOT_IN_PLAN | 403 | A SECOND approval level was sent without multi-level approval in the plan. |
| IDEMPOTENCY_KEY_REUSED | 422 | The same Idempotency-Key was used with a different request. |
| IDEMPOTENCY_REQUEST_IN_PROGRESS | 409 | A retry arrived while the first request was still running. |
| INVALID_IDEMPOTENCY_KEY | 400 | The Idempotency-Key was invalid. |
Codes and statuses are listed in the endpoint's error documentation; the meaning column paraphrases the stated conditions.
Frequently asked questions
Does PATCH replace the whole department or only the fields I send?
Only the fields you send are changed. Everything you omit is kept from the department's current state, members and approvers. The exceptions are userIds and approvers, which are full-replacement lists when sent.
How do I change the default department?
Set another department's isDefault to true. Sending isDefault: false for the current default department fails with 409 DEFAULT_DEPARTMENT_UNSET_FORBIDDEN.
What should I do when the response is 202 Accepted?
Read the operation object in the body and poll its url until the status is SUCCEEDED or FAILED. If the status is UNKNOWN, no result arrived within 15 minutes; read the department to check whether the change was applied.
When does X-On-Behalf-Of fail?
It fails with 403 ON_BEHALF_OF_NOT_ALLOWED when the key lacks the users:act-on-behalf scope or the key owner does not manage the target user.
Can I safely retry an update?
Yes, if you send an Idempotency-Key. A repeat of the same key and the same request inside the 24-hour retention window returns the stored original response, marked with the Idempotent-Replayed header. If the key is paired with a different request, the call returns 422 IDEMPOTENCY_KEY_REUSED.
curl -X PATCH "$API_BASE_URL/v2/departments/string" \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{
"name": "Platform Team"
}'{
"data": {
"id": "team-0a21c25e-6925-4a5b-90ee-ecf82096c4f6",
"name": "Dev Team",
"isDefault": false,
"createdAt": "2022-08-22T15:00:00.000Z",
"parentId": "team-969f8429-7dc3-48a1-96b1-6049a99dda06",
"approvers": [
{
"userId": "user-63b17e97-d38b-4586-8498-bb4f5d2e2c14",
"levels": [
"FIRST"
]
},
{
"userId": "user-0f3a9c1e-6c2b-4a5d-9e7f-1b2c3d4e5f60",
"levels": [
"SECOND"
]
}
]
}
}{
"operation": {
"id": "pa_3f1c2b9e-8a47-4d5e-9c61-2b7f0e4a1d93",
"status": "PENDING",
"url": "/v2/operations/pa_3f1c2b9e-8a47-4d5e-9c61-2b7f0e4a1d93"
}
}{
"error": {
"code": "VALIDATION_ERROR",
"name": "ValidationError",
"description": "Example post details.",
"resolution": "Fix the fields listed in details.fields and send the request again.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"details": {
"fields": [
{
"path": "startDate",
"message": "\"startDate\" is required"
}
]
}
}
}{
"error": {
"code": "UNAUTHORIZED",
"name": "Unauthorized",
"description": "Example post details.",
"resolution": "Send a valid API key in the Authorization header as Bearer followed by the full vt_live_ key.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
}
}{
"error": {
"code": "INSUFFICIENT_SCOPE",
"name": "InsufficientScope",
"description": "Example post details.",
"resolution": "Add the scopes listed in details.requiredScopes to the key in the OnlyLeave dashboard.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"details": {
"requiredScopes": [
"leaves:write"
]
}
}
}{
"error": {
"code": "RESOURCE_NOT_FOUND",
"name": "ResourceNotFound",
"description": "Example post details.",
"resolution": "Check the ID of the resource named in details.resource and that the key owner can see it.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
}
}{
"error": {
"code": "CONFLICT",
"name": "Conflict",
"description": "Example post details.",
"resolution": "Read the resource again and retry with up-to-date values.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
}
}{
"error": {
"code": "LEAVE_NEGATIVE_BALANCE",
"name": "LeaveNegativeBalance",
"description": "Example post details.",
"resolution": "Shorten the leave, use a different leave type, or ask an admin to adjust the quota.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
}
}{
"error": {
"code": "RATE_LIMITED",
"name": "RateLimited",
"description": "Example post details.",
"resolution": "Wait a few seconds and retry, using the same Idempotency-Key for writes.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
}
}{
"error": {
"code": "INTERNAL_ERROR",
"name": "InternalError",
"description": "Example post details.",
"resolution": "Retry later, and if it keeps happening, contact OnlyLeave support and quote the requestId.",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
}
}id
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| id | string | Yes | resource ID. |
Idempotency-Key
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| Idempotency-Key | string | No | optional key (1 to 255 printable ASCII characters) that makes a write request safe to retry. Keys are retained for 24 hours per API key. Retrying with the same key and the same request within those 24 hours replays the original response (with the Idempotent-Replayed header). Reusing the key with a different request returns 422 IDEMPOTENCY_KEY_REUSED ; retrying while the first request is still running returns 409 IDEMPOTENCY_REQUEST_IN_PROGRESS ; an invalid key returns 400 INVALID_IDEMPOTENCY_KEY . | 1 <= length <= 255 |
X-On-Behalf-Of
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| X-On-Behalf-Of | string | No | ID of the user the write is performed for. Requires the users:act-on-behalf scope, and the key owner must manage the target user; otherwise the request fails with 403 ON_BEHALF_OF_NOT_ALLOWED . Ignored on GET requests. |
body
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| body | object | Yes | ||
| body.name | string | No | department name | 1 <= length |
| body.approvers | array<DepartmentApproverInput> | No | the full replacement approver list; each userId may appear only once. When omitted, the current approvers are kept. | 1 <= items |
| body.approvers[] | object | |||
| body.approvers[].userId | string | Yes | the approver's user ID | 1 <= length |
| body.approvers[].levels | array<string> | No | approval levels this user approves at. SECOND requires a plan with multi-level approval. | ["FIRST"] 1 <= items |
| body.approvers[].levels[] | string | Allowed values: "FIRST" | "SECOND" | ||
| body.userIds | array<string> | No | the full replacement membership. Any user currently in this department whose ID is missing here is moved to the company's default department. When omitted, the current members are kept. | |
| body.userIds[] | string | 1 <= length | ||
| body.parentId | string | No | the ID of the new parent department. Fails with 409 DEPARTMENT_CIRCULAR_PARENT if it is this department or one of its sub-departments. | 1 <= length |
| body.isDefault | boolean | No | whether this is the company's default department. Sending false for the current default department fails with 409 DEFAULT_DEPARTMENT_UNSET_FORBIDDEN . | |
| body.watchersToNotify | array<string> | No | email addresses that are notified about leave requests in this department | |
| body.watchersToNotify[] | string | |||
| body.keepLeaveRequestsOpen | boolean | No | whether leave requests in this department stay open instead of auto-expiring |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | |||
| response.data | object | Yes | A department as returned by create and update, with its parent and approvers. The approvers are written by a background process, so they may briefly lag right after a write. | |
| response.data.id | string | Yes | department ID | |
| response.data.name | string | Yes | department name | |
| response.data.isDefault | boolean | Yes | whether this is the company's default department | |
| response.data.createdAt | string | Yes | a timestamp in the ISO 8601 format | Format: date-time |
| response.data.parentId | string | No | the ID of the parent department, present only for a sub-department | |
| response.data.approvers | array<DepartmentApprover> | Yes | users who approve leave requests for this department, and the levels they approve at | |
| response.data.approvers[] | object | |||
| response.data.approvers[].userId | string | Yes | the approver's user ID | |
| response.data.approvers[].levels | array<string> | Yes | the approval levels this user approves at; FIRST when none is stored | |
| response.data.approvers[].levels[] | string | Allowed values: "FIRST" | "SECOND" |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Returned with HTTP 202 when a write operation is still running, or succeeded but its result could not be read back yet. The Location header carries the same URL as operation.url . Poll it until the status is SUCCEEDED or FAILED . | ||
| response.operation | object | Yes | ||
| response.operation.id | string | Yes | ||
| response.operation.status | string | Yes | operation status. UNKNOWN means no result arrived within 15 minutes; the change may or may not have been applied, so read the resource to check. A result that arrives later still changes it to SUCCEEDED or FAILED. | Allowed values: "PENDING" | "SUCCEEDED" | "FAILED" | "UNKNOWN" |
| response.operation.url | string | Yes |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |
response
| Field | Type | Required | Description | Constraints |
|---|---|---|---|---|
| response | object | Error envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog . | ||
| response.error | object | Yes | ||
| response.error.code | string | Yes | stable machine-readable error code | |
| response.error.name | string | Yes | short human-readable error name | |
| response.error.description | string | Yes | what went wrong | |
| response.error.resolution | string | Yes | how to fix the problem | |
| response.error.requestId | string | Yes | request ID, include it when contacting support | |
| response.error.details | object | No | optional additional information specific to the error code | |
| response.error.details.[key: string] | any | No |