Learn how to quickly set up and test the Square PHP SDK.
SDKs and Samples

Square PHP SDK Quickstart

Learn how to quickly set up and test the Square PHP SDK.

Before you begin, you need a Square account and account credentials. You use the Square Sandbox for the Quickstart exercise. Do the following:

  1. Create a Square account and an application. For instructions, see Create an account and application.

  2. Get a Sandbox access token from the Developer Dashboard. For instructions, see Step 2.1: Make a test API call and review your account.

  3. Install the following:

    • PHP. Square supports PHP versions 7.4 through 8.1.

    • Composer for dependency management.

  1. Create a new directory for your project, and then go to that directory:

  2. Install the Square PHP SDK:

  1. In your project directory, create a new file named quickstart.php with the following content:

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    <?php
    
    require 'vendor/autoload.php';
    
    use Square\SquareClient;
    use Square\Environment;
    use Square\Exceptions\ApiException;
    
    $client = new SquareClient([
        'accessToken' => getenv('SQUARE_ACCESS_TOKEN'),
        'environment' => Environment::SANDBOX,
    ]);
    
    try {
    
        $apiResponse = $client->getLocationsApi()->listLocations();
    
        if ($apiResponse->isSuccess()) {
            $result = $apiResponse->getResult();
            foreach ($result->getLocations() as $location) {
                printf(
                    "%s: %s, %s, %s<p/>", 
                    $location->getId(),
                    $location->getName(),
                    $location->getAddress()->getAddressLine1(),
                    $location->getAddress()->getLocality()
                );
            }
    
        } else {
            $errors = $apiResponse->getErrors();
            foreach ($errors as $error) {
                printf(
                    "%s<br/> %s<br/> %s<p/>", 
                    $error->getCategory(),
                    $error->getCode(),
                    $error->getDetail()
                );
            }
        }
    
    } catch (ApiException $e) {
        echo "ApiException occurred: <b/>";
        echo $e->getMessage() . "<p/>";
    }
    
  2. When finished, save the quickstart.php file.

The PHP code in this Quickstart reads your Square Sandbox access token from the SQUARE_ACCESS_TOKEN environment variable. This helps avoid the use of hardcoded credentials in the code.

For the following commands, replace yourSandboxAccessToken with your Square Sandbox access token:

  1. PHP ships with a built-in web server for testing purposes. Start the web server as follows:

  2. Open a web browser and navigate to http://localhost:8000/quickstart.php.

    Verify the result. You should see at least one location (Square creates one location when you create a Square account).

If you need more assistance, contact Developer Support or ask for help in the Developer Forums.