Code Cookbook

Get a List of Active Employees

Get a paged list of active employees and display the results.

Labor API
Employees API
PHP (Connect SDK)
Connect SDK
Command Line
Save

Process Overview

    The https://connect.squareup.com/v2/employees endpoint takes a GET request that includes filter and pagination instructions as URL parameters.

    The following employee list request asks for all of a location's current employees:

    • At a specified location
    • Limit result page size to 20 employees
    • Are currently ACTIVE
    1. Get a paged list of employees
    curl https://connect.squareup.com/v2/employees?location_id=LOCATION_ID&status=ACTIVE&limit=20 \
     -H 'Content-Type: application/json'                       \
     -H 'Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN'
    

    The response to the shift search request is an array of Employee objects that match the filter criteria. If more results are available, the response includes a cursor property: "cursor": "KFuNoJ1SQp1xkgb7EsxjSucfgx3K5PZbe0HifD22A43l03ZofED"

    {
        "employees": [
            {
            {
                "id": "16LNfi7Kh1tAhDECMZEO",
                "first_name": "Jane",
                "last_name": "Smith",
                "location_ids": ["LOCATION_ID"],
                "status": "ACTIVE",
                "created_at": "2019-02-20T01:28:49Z",
                "updated_at": "2019-02-20T01:28:49Z"
            },
            {
                "id": "RkReoWaEZI3x22NwGIH4",
                "first_name": "Jane",
                "last_name": "Smith",
                "location_ids": ["LOCATION_ID"],
                "status": "ACTIVE",
                "created_at": "2019-02-20T01:28:49Z",
                "updated_at": "2019-02-20T01:28:49Z"
            },
    
            {
                "id": "m_Omc4Z7dswgz5pLrD6z",
                 ...
            },
            {
                "id": "r4nBW_BWnCqoRqVKbdOR",
                ...
            },
            ...
        ],
        "cursor": "0:7UwZq_TWrobq7kcgHUP1"
    }
    
    
    1. Initialize the Employees API.
    <?php
    require_once(__DIR__ . '/vendor/autoload.php');
    SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken(
      'YOUR_ACCESS_TOKEN');
    $Employees_api = new SquareConnect\Api\EmployeesApi();
    ?>
    
    1. Call $retrieveEmployeesByLocation once to get the first page of results. If there are more results, call $retrieveEmployeesByLocation until there are no more results.
    $locationId = 'LOCATION_ID';
    $status = 'ACTIVE';
    $limit = 20; 
    $listEmployeeResponse = $employees_api->listEmployees(
           $locationId,
           $status,
           $limit,
           NULL);
     $listEmployees($listEmployeeResponse->getEmployees());
    
    1. Get the next page

    To get the next page of results, make the following request, appending the cursor from the previous response:

    curl https://connect.squareup.com/v2/employees?location_id=LOCATION_ID&status=ACTIVE&limit=20&cursor=0:7UwZq_TWrobq7kcgHUP1 \
     -H 'Content-Type: application/json'                       \
     -H 'Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN'
    
     //Get next page of employee shifts until all pages have been received
     while ($listEmployeeResponse->getCursor() != NULL) {
       $listEmployeeResponse = $employees_api->listEmployees(
           $locationId,
           $status,
           $limit,
           $listEmployeeResponse->getCursor());
    
       $listEmployees($listEmployeeResponse->getEmployees());
     }
    
    1. List details of each employee returned in the result set. Details include:
    • First name
    • Last name

    No content

    public function listEmployees($employees) {
       foreach($employees as $employee) {
         $displayEmployees(
           $employee->getLastName(),
           $employee->getFirstName());
       }
    } 
    
    Prev
    < Employees API Code Cookbook

    Contact Developer Support, join our Slack channel, or ask for help on Stack Overflow