Skip to content

Assign a label to a user

This endpoint attaches a company label to one user. It is a write operation: the caller needs the labels:write scope, and the acting identity is either the API key owner or the user named in X-On-Behalf-Of. The response is either the user's complete label list (200) or an accepted operation (202) that you poll until it finishes.

Endpoint and method

Send a POST request to /v2/users/{id}/labels. The path parameter id is the user's resource ID and is required. The body is application/json and must include labelId, the ID of the label to assign. The request body is required.

Authorization and scopes

Authenticate with a bearer token in the Authorization header. The endpoint requires the labels:write scope. If you are acting for another user, send X-On-Behalf-Of with that user's ID; this additionally requires the users:act-on-behalf scope, and the key owner must manage the target user. Without that permission the request fails with 403 ON_BEHALF_OF_NOT_ALLOWED.

Input map

Use this checklist before sending the request:

  • Path id: required string, the user's resource ID.
  • Header Idempotency-Key: optional string of 1 to 255 printable ASCII characters. Each key is scoped to one API key and lives for 24 hours. Send the identical request again with that key inside the window and you get the stored response back, flagged by the Idempotent-Replayed header. Point the same key at a different request and the API answers 422 IDEMPOTENCY_KEY_REUSED; send it again while the first attempt is still in flight and you get 409 IDEMPOTENCY_REQUEST_IN_PROGRESS; a malformed key yields 400 INVALID_IDEMPOTENCY_KEY.
  • Header X-On-Behalf-Of: optional string, the user ID the write is performed for. Ignored on GET requests.
  • Body labelId: required string, the label to assign.

Reading the response

A 200 response returns the user's full list of labels as a data array. Each label carries id, name, color (stored as given; the dashboard uses a hex code), and userCount (the number of users with that label, or 0 when none).

A 202 response means the write is still running, or it succeeded but the result could not be read back yet. The body contains an operation object with id, status, and url; the Location header carries the same URL. Poll that URL until 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.

Failure distinctions

The endpoint documents these specific errors: 400 VALIDATION_ERROR when labelId is missing or invalid; 403 ON_BEHALF_OF_NOT_ALLOWED when the on-behalf-of request is not permitted; 403 FORBIDDEN when the acting user may not perform the action; 404 RESOURCE_NOT_FOUND when either the user or the label does not exist; and the Idempotency-Key codes described above. Every error uses the same envelope with error.code, error.name, error.description, error.resolution, error.requestId, and optional error.details. The requestId is the value to quote when contacting support.

Worked example

The captured request example uses labelId 969f8429-7dc3-48a1-96b1-6049a99dda06 and a path placeholder of string for the user ID. The captured 200 example returns a data array containing one label named contractor with color #FF0000 and userCount 12. The captured 202 example returns an operation with status PENDING and a url under /v2/operations/. Use these shapes to check your own request and response handling; replace the placeholder IDs with real values from your account.

Frequently asked questions

What scope does assigning a label require?

The labels:write scope. If you use X-On-Behalf-Of, the users:act-on-behalf scope is also required, and the key owner must manage the target user.

What does the 202 response mean?

The write is still running, or it succeeded but the result could not be read back yet. The body contains an operation with id, status, and url; poll that URL until status is SUCCEEDED or FAILED. UNKNOWN means no result arrived within 15 minutes, so read the resource to check whether the change applied.

What happens if I retry with the same Idempotency-Key?

If the request is the same and within 24 hours, the original response is replayed with the Idempotent-Replayed header. A different request with the same key returns 422 IDEMPOTENCY_KEY_REUSED; retrying while the first request is still running returns 409 IDEMPOTENCY_REQUEST_IN_PROGRESS.

Why might I get 404 RESOURCE_NOT_FOUND?

Either the user or the label does not exist. Check both IDs and confirm the key owner can see them.

What is the difference between 403 ON_BEHALF_OF_NOT_ALLOWED and 403 FORBIDDEN?

ON_BEHALF_OF_NOT_ALLOWED means the X-On-Behalf-Of request is not permitted, for example because the users:act-on-behalf scope is missing or the key owner does not manage the target user. FORBIDDEN means the acting user may not perform the action.

curl -X POST "$API_BASE_URL/v2/users/string/labels" \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  -d '{
    "labelId": "969f8429-7dc3-48a1-96b1-6049a99dda06"
  }'
{
  "data": [
    {
      "id": "969f8429-7dc3-48a1-96b1-6049a99dda06",
      "name": "contractor",
      "color": "#FF0000",
      "userCount": 12
    }
  ]
}
{
  "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

FieldTypeRequiredDescriptionConstraints
idstringYesresource ID.

Idempotency-Key

FieldTypeRequiredDescriptionConstraints
Idempotency-KeystringNooptional 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

FieldTypeRequiredDescriptionConstraints
X-On-Behalf-OfstringNoID 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

FieldTypeRequiredDescriptionConstraints
bodyobjectYes
body.labelIdstringYesthe ID of the label to assign to the user

response

FieldTypeRequiredDescriptionConstraints
responseobject
response.dataarray<Label>Yes
response.data[]object
response.data[].idstringYeslabel ID
response.data[].namestringYeslabel name
response.data[].colorstringYeslabel color, as stored (the dashboard uses a hex code)
response.data[].userCountintegerYesthe number of users with this label, 0 when none

response

FieldTypeRequiredDescriptionConstraints
responseobjectReturned 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.operationobjectYes
response.operation.idstringYes
response.operation.statusstringYesoperation 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."PENDING" | "SUCCEEDED" | "FAILED" | "UNKNOWN"
response.operation.urlstringYes

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo