Skip to content

Create a Label with the REST API v2

The create-label endpoint adds a new label to the company and returns the created record. This reference covers the required body fields, the scope and headers that govern the call, and the two success shapes you need to handle: an immediate 201 with the label, or a 202 with an operation to poll.

Endpoint and authorization

Send a POST request to /v2/labels. The call requires the labels:write scope, carried as a bearer token in the Authorization header. The acting user is either the key owner or, when the X-On-Behalf-Of header is present, the user named there. That header additionally requires the users:act-on-behalf scope, and the key owner must manage the target user; when either requirement is unmet, the request fails with 403 ON_BEHALF_OF_NOT_ALLOWED. On GET requests the header has no effect.

Request body: name and color

Both name and color are required, non-empty strings. Unknown fields fail validation. The name is the label name. The color accepts any non-empty string, but a # hex code such as #00AAFF is recommended so the dashboard can render it. The body is sent as application/json. The supplied request example uses the name "Team B" and the color "#00AAFF".

Headers that affect the write

Two optional headers change how the write behaves. Idempotency-Key accepts 1 to 255 printable ASCII characters and lets you retry the request without duplicating the effect. Keys are stored per API key for 24 hours; during that window, repeating the same key with an identical request replays the original response and includes the Idempotent-Replayed header. Three failure modes apply: pairing the key with a different request yields 422 IDEMPOTENCY_KEY_REUSED; sending it again while the first attempt is still running yields 409 IDEMPOTENCY_REQUEST_IN_PROGRESS; and a key that breaks the character or length rule yields 400 INVALID_IDEMPOTENCY_KEY. X-On-Behalf-Of names the user the write is performed for, subject to the scope and management rules above.

Success responses: 201 versus 202

A 201 returns the created label in a data object with id, name, color, and userCount. The userCount field is the number of users with this label and is 0 when none. A 202 returns an operation object with id, status, and url; the Location header carries the same URL as operation.url. Poll that URL until the status is SUCCEEDED or FAILED. A status of UNKNOWN means no result arrived within 15 minutes, so the change may or may not have been applied and you should read the resource to check; a result that arrives later still changes the status to SUCCEEDED or FAILED.

Errors and how to tell them apart

The documented error codes for this endpoint are 400 VALIDATION_ERROR (with details.fields), 400 INVALID_REQUEST_BODY, 403 ON_BEHALF_OF_NOT_ALLOWED, 403 FORBIDDEN, 422 PLAN_LIMIT_REACHED, and the Idempotency-Key codes. Two pairs are easy to confuse. A 400 VALIDATION_ERROR points at specific body fields in details.fields, while a 400 INVALID_REQUEST_BODY means the JSON body itself could not be accepted. A 403 ON_BEHALF_OF_NOT_ALLOWED means the on-behalf-of scope or the key owner's management of the target user is missing, whereas a 403 FORBIDDEN means the acting user may not perform the action at all. A 422 PLAN_LIMIT_REACHED means the plan's label allowance is exhausted, not that the request was malformed. Every error envelope carries a stable code, a name, a description, a resolution, and a requestId to quote when contacting support.

After creation: assigning the label

Creating a label does not attach it to anyone. Assign the label to users with POST /v2/users/{id}/labels. The returned userCount reflects how many users currently carry the label, so a freshly created label typically reports 0 until assignments are made.

A worked example

Using the supplied example, a request with name "Team B" and color "#00AAFF" can return a 201 whose data object contains an id, the name, the color as stored, and a userCount. The supplied response example shows a label named "contractor" with color "#FF0000" and userCount 12, illustrating that userCount is populated from existing assignments rather than from the create call itself. If the same request instead returns a 202, capture operation.url and poll it; do not treat the 202 as a failure.

Frequently asked questions

What happens if I send a field the endpoint does not recognize?

Unknown fields fail validation. Send only name and color in the request body.

Is a 202 a failure?

No. A 202 means the write is still running, or succeeded but its result could not be read back yet. Poll operation.url until the status is SUCCEEDED or FAILED.

Can I retry a create request safely?

Yes, if you supply an Idempotency-Key. Retrying with the same key and the same request within 24 hours replays the original response. Reusing the key with a different request returns 422 IDEMPOTENCY_KEY_REUSED.

Why did my request return 422 PLAN_LIMIT_REACHED?

The plan's label allowance has been reached. This is a plan limit, not a malformed request; review the plan's label capacity before retrying.

Does creating a label assign it to users?

No. Use POST /v2/users/{id}/labels to assign the label. The userCount field reflects existing assignments.

curl -X POST "$API_BASE_URL/v2/labels" \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Team B",
    "color": "#00AAFF"
  }'
{
  "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": "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"
  }
}

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.namestringYeslabel name1 <= length
body.colorstringYeslabel color. Any non-empty string is accepted; use a # hex code such as #00AAFF so the dashboard can show it.1 <= length

response

FieldTypeRequiredDescriptionConstraints
responseobject
response.dataobjectYes
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.Allowed values: "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