Labor API

Build With the Labor API

Use the Labor API to capture the start and end of an employee shift, and the hourly rate for the shift.

Web
Backend
Employees API
Labor API
cURL (Command Line)

Prerequisites and assumptions
Permalink Get a link to this section

This document provides step-by-step guidance for integrating with the Labor API. For information on how the Labor API works, please read the Labor API How it Works guide.

To use the Labor API, the following must be true:

  • You have a Square account. Create a Square account in minutes if you do not already have one.

  • You have added employees to at least one location in the Square Dashboard

  • Your Labor API integration component is hosted on systems that support dynamic pages with server-side processing (e.g., PHP, Ruby, ASP, or Java pages). Hosting solutions that support only HTML pages cannot use the Labor API


This guide also makes the following assumptions:

  • The Square Dashboard for your account has at least 1 active employee.

  • You have assigned at least one wage type per employee in the Square Dashboard.

  • You are using the Connect v2 PHP SDK on GitHub.

Information you will need
Permalink Get a link to this section

To use the steps in this guide you will need the following information:

  • A valid access token. Square recommends testing with sandbox credentials whenever possible. See Square API Access Tokens for more information.

  • An active location ID. Copy a valid Developer Account location ID from the Locations setting page of your Square application in the Developer Dashboard, or set the dashboard to Sandbox Settings mode and then copy a sandbox location ID.

Shift time tracking at a glance
Permalink Get a link to this section

This guide walks you through the process of starting and ending an employee shift:

Open a new shift

  1. Configure the labor API with a valid access token and location ID.

  2. Get an employee ID.

  3. Get the wage type for the job to be done on the shift.

  4. Check for a previous open shift for the employee.

  5. Create and send a CreateShift request to start a shift.

End the shift

  1. Search for the open shift.

  2. Update the open shift with an end time to end it.

Configure the Labor API
Permalink Get a link to this section

Before you can use the Labor API, you must set the Location ID where the API is used and then authorize the API to access Shift and Employee resources for that location.

Step 1: Get your authorization token
Permalink Get a link to this section

  1. Open the Developer Dashboard and select an application. The Credentials page for the selected app is shown.

  2. Set the dashboard mode to Production Settings for a production environment access token or Sandbox Settings for a sandbox environment access token.

  3. Copy the Access Token in the Credentials section of the page.

Store the copied personal access token in a secure location. Any developer can use that token in their own app to get full API access to your Square account.

Did you know?

For more secure access to your Square account, use an OAuth token. The OAuth flow enforces security in 2 ways:

  • Challenges a user to identify themselves before granting access to a Square account.

  • Limits the scope of access to a Square account and only after the authenticated user authorizes access.

If you are using an OAuth access token, request the EMPLOYEES_READ and TIMECARDS_WRITE permissions.

Step 2: Set the API client configuration
Permalink Get a link to this section

No content

Step 3: Get your location ID
Permalink Get a link to this section

All employee shifts processed with the Labor API must be associated with a valid location ID for the associated Square account. Use the Locations API to find your location ID:

Search for the location where the employee shift is starting. Use the Location.id for the Labor API operations in this guide.

Open a new shift
Permalink Get a link to this section

Step 1: Get an employee to start the shift
Permalink Get a link to this section

Retrieve a list of active employees for a location and select an employee from the list.

Use the following cURL command to get active employees for a location

Curl https://connect.squareupsandbox.com/v2/employees?location_id=YOUR_LOCATION_ID \
 -H 'Content-Type: application/json'                       \
 -H 'Authorization: Bearer YOUR_ACCESS_TOKEN'     \

The response carries an array of Employee

{
    "employees": [
        {
            "id": "EMPLOYEE_ID",
            "first_name": "Babe",
            "last_name": "Ruth",
            "location_ids": [
                "YOUR_LOCATION_ID"
            ],
            "status": "ACTIVE",
            "created_at": "2019-02-19T21:52:48Z",
            "updated_at": "2019-02-19T21:55:16Z"
        }
    ]
}

Step 2: Get the employee wage types
Permalink Get a link to this section

An Employee can have multiple wage types. To assign the correct wage to a shift, get all of the EmployeeWage object for the employee and find the wage type for the job that the employee is performing in this shift.

Did you know?

The first EmployeeWage in the set of returned items is the default employee wage. Use the default wage unless you are choosing an EmployeeWage based on a matching title.

Curl https://connect.squareupsandbox.com/v2/labor/employee-wages?employee_id=EMPLOYEE_ID \
 -H 'Content-Type: application/json'                       \
 -H 'Authorization: Bearer YOUR_ACCESS_TOKEN'     \

The response carries an array of the EmployeeWage objects associated with an employee ID.

{
    "employee_wages": [
        {
            "id": "2Hm55mLKx3q1GyLUAAxhjim8",
            "employee_id": "EMPLOYEE_ID",
            "title": "Photographer",
            "hourly_rate": {
                "amount": 4500,
                "currency": "USD"
            }
        }
    ]
}

Step 3: Check for an open Shift
Permalink Get a link to this section

A new shift cannot be started for an employee until the previous shift is ended.

The following shift search request asks for a set of shifts with the following instructions:

  • Shifts for one specified employee

  • At a specified location

  • With shifts whose status is "OPEN"

This filter will always return 0 or 1 Shift. An employee can have a maximum of 1 open shift.

curl https://connect.squareupsandbox.com/v2/labor/shifts/search \
 -H 'Content-Type: application/json'                       \
 -H 'Authorization: Bearer YOUR_ACCESS_TOKEN'     \
 -d '{
    "query": {
        "filter": {
            "status": "OPEN", 
            "employee_id": ["AN_EMPLOYEE_ID"]
        }
    }
}'

The response to the shift search request is an empty object if the employee does not have an OPEN shift.

{}

Step 4: Build a shift object
Permalink Get a link to this section

To create a new shift, use the location ID, employee ID, and EmployeeWage hourly rate to set values in a new ShiftWage. The shift start_at field can be set to local time or GMT. If GMT, Square's Labor endpoint calculates the local time based on the location of the shift.

POST a new shift using cURL at the command line.

curl https://connect.squareupsandbox.com/v2/labor/shifts \
 -H 'Content-Type: application/json'                       \
 -H 'Authorization: Bearer YOUR_ACCESS_TOKEN'     \
 -d '{
    "idempotency_key": "UUID",
    "shift": {
        "employee_id": "AN_EMPLOYEE_ID",
        "location_id": "YOUR_LOCATION_ID",
        "start_at": "2019-02-05T12:00:00Z", 
        "wage": { 
            "title": "Photographer",
            "hourly_rate": {
                "amount": 4500,
                "currency": "USD"
                }
        }
    }
}'

Step 5: Call CreateShift
Permalink Get a link to this section

To request the new shift, create a new CreateShiftRequest, set the idempotency key, and then set the $shift object in the body of the request. On completion of the operation, a Shift is returned with server-calculated field values.

No content

Close the shift
Permalink Get a link to this section

Step 1: Get the Shift to close
Permalink Get a link to this section

Get the open shift for the employee by using the example in Step 3.

The response to the shift search request is an array of Shift objects that match the filter criteria.

{
    "shifts": [
        {
            "id": "3DBHNF9E8CG27",
            "employee_id": "AN_EMPLOYEE_ID",
            "location_id": "YOUR_LOCATION_ID",
            "timezone": "America/Los_Angeles",
            "start_at": "2018-10-04T04:00:00-07:00",
...
            "status": "OPEN",
            "version": 1,
            "created_at": "2019-02-20T01:28:49Z",
            "updated_at": "2019-02-20T01:28:49Z"
        }
    ]
}

Step 2: Update the Shift and call UpdateShift.
Permalink Get a link to this section

To close a shift, an end_at field is added to$openShift. If the shift has an open break, it must be closed before the shift can be closed.

curl https://connect.squareupsandbox.com/v2/labor/shifts/3DBHNF9E8CG27 \
 -H 'Content-Type: application/json'                       \
 -H 'Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN'     \
 -D '{
    "shift": {
        "id": "3DBHNF9E8CG27",
        "employee_id": "aq8XS-F0-VA0UrgH6h3w",
        "location_id": "YOUR_LOCATION_ID",
        "timezone": "America/Los_Angeles",
        "start_at": "2018-10-16T04:00:00-07:00",
        "end_at": "2018-10-16T12:00:00Z",
        "wage": {
            "title": "Photographer",
            "hourly_rate": {
                "amount": 4500,
                "currency": "USD"
            }
        },
        "status": "OPEN",
        "version": 3,
        "created_at": "2019-02-21T20:22:10Z",
        "updated_at": "2019-02-21T20:24:34Z"
    }
}
'

Before you POST the updated Shift, look for an array of Break objects that will be present if the employee took any breaks during the shift. If you find these object, confirm that they contain end_at properties and that those properties are not null.

The response is an updated version of the Shift object with a CLOSED status and an incremented version number:

{
    "shift": {
        "id": "3DBHNF9E8CG27",
...
        "start_at": "2018-10-16T04:00:00-07:00",
        "end_at": "2018-10-16T12:00:00-07:00",
...
        "status": "CLOSED",
        "version": 4,
...
    }
}

Next steps

Now that you have a basic build in place, expand on it with these recipes!