Skip to content

Create a location with the REST API v2

Use this endpoint to add a new location, optionally moving users into it, marking it as the company default, and setting its starting work week. Only name and timezone are mandatory; the remaining fields have documented defaults, so a minimal request is enough for a standard Monday-to-Friday setup.

Before you call

Send a POST request to /v2/locations with a JSON body. Authorization uses a Bearer API key carrying the locations:write scope. The acting user is normally the key owner; setting X-On-Behalf-Of changes that to the named user, which requires the users:act-on-behalf scope and a managed relationship with that user. Add an Idempotency-Key whenever a retry is possible, because it makes the write safe to repeat.

Required and optional fields

Provide name (surrounding spaces are trimmed) and timezone as an IANA name such as America/Los_Angeles. Everything else is optional: firstDayOfWeek defaults to 1 for Monday, resetQuotas to FISCAL_YEAR, yearStartMonth and yearStartDay to 1, isDefault to false, and userIds to an empty list. Note that yearStartDay must be valid in yearStartMonth in every year, so 29 February is rejected. Unknown fields or out-of-range values fail validation.

Moving users, defaults, and the work week

Listing userIds moves those users into the new location, and isDefault set to true makes it the company default. The workWeek object sets the initial schedule; if you omit it, the location starts with Monday to Friday and default work hours. A practical checklist before sending: confirm the name is not already used, check that every user ID exists, verify the fiscal-year start date is valid every year, and confirm your plan allows any custom work hours you include.

Reading the result and handling errors

A 201 response returns the finished location, including its id, name, timezone, isDefault flag, and createdAt timestamp. A 202 response instead returns an operation with an id, status, and url; poll that URL until the status becomes SUCCEEDED or FAILED. Treat UNKNOWN as undecided: read the location back to see whether the change was applied. Errors use a shared envelope with code, name, description, resolution, and requestId; common cases include 400 VALIDATION_ERROR with details.fields, 403 FORBIDDEN, 409 DUPLICATE_NAME, 422 UNKNOWN_USER_IDS, and 422 PLAN_LIMIT_REACHED.

Create-location request fields
FieldRequiredDefault or rule
nameYesTrimmed; must not be empty
timezoneYesIANA time zone name
firstDayOfWeekNo1 (Monday); range 0-6
resetQuotasNoFISCAL_YEAR or USER_YEAR
yearStartMonth / yearStartDayNo1 / 1; day must exist every year
isDefaultNofalse
userIdsNoEmpty array; listed users move in
workWeekNoMonday-Friday with default work hours

Defaults apply when the field is omitted; unknown fields fail validation.

Response and error handling
StatusMeaningNext step
201Location created and returnedUse the returned location id
202Still running beyond the wait windowPoll the operation URL
400Validation or invalid bodyFix fields listed in details.fields
403Forbidden or on-behalf not allowedCheck scope and acting-user permissions
409Duplicate name or idempotency in progressRead current state, then retry
422Unknown user IDs or plan limitCorrect user IDs or plan

Poll the operation URL for 202 until SUCCEEDED or FAILED; UNKNOWN means check the resource.

Frequently asked questions

Who is treated as acting when I create a location?

Send the request with a Bearer API key that carries locations:write. If you also set X-On-Behalf-Of, that user becomes the acting user; the key owner must manage that user and hold users:act-on-behalf, otherwise the call is rejected with 403 ON_BEHALF_OF_NOT_ALLOWED.

What is the difference between a 201 and a 202 response?

A 201 response returns the created location. A 202 response means creation was still running beyond the wait window, so poll the returned operation URL until it reports SUCCEEDED or FAILED.

How should I retry a failed location creation safely?

Use a stable Idempotency-Key for retries. Repeating the same key with the same payload within 24 hours replays the original response; using that key for a different payload returns 422 IDEMPOTENCY_KEY_REUSED, and retrying while the first attempt is still running returns 409 IDEMPOTENCY_REQUEST_IN_PROGRESS.

curl -X POST "$API_BASE_URL/v2/locations" \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Los Angeles",
    "timezone": "America/Los_Angeles"
  }'
{
  "data": {
    "id": "location-79c77ac8-e5bf-479f-9488-648a58379ff1",
    "name": "Los Angeles",
    "timezone": "America/Los_Angeles",
    "isDefault": false,
    "createdAt": "2022-08-22T15:00:00.000Z"
  }
}
{
  "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.

response

FieldTypeRequiredDescriptionConstraints
responseobject
response.dataobjectYes
response.data.idstringYeslocation ID
response.data.namestringYeslocation name
response.data.timezonestringNothe location's IANA time zone, left out when none is stored
response.data.isDefaultbooleanYeswhether this is the company's default location
response.data.createdAtstringYesa timestamp in the ISO 8601 formatFormat: date-time

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