Square PHP SDK Quickstart

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

Link to section

Prepare for the Quickstart

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

  1. Create a Square account and an application. For more information, see Create an Account and Application.

  2. Get a Sandbox access token from the Developer Console. For more information, see Make your First API Call.

  3. Install the following:

    • PHP - Square supports PHP versions 7.4 to 8.1.
    • Composer - Used for dependency management.

Note

If you prefer to skip the following setup steps, download the Square PHP SDK Quickstart sample and follow the instructions in the README.

Link to section

Create a project

  1. Open a new terminal window. Create a new directory for your project, and then go to that directory.

    mkdir quickstart cd ./quickstart
  2. Install the Square PHP SDK.

    composer require square/square
Link to section

Write code

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

  2. Save the quickstart.php file.

    This code does the following:

Link to section

Set your Square credentials

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:

Link to section

Linux or macOS

export SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
Link to section

Windows: PowerShell

Set-item -Path Env:SQUARE_ACCESS_TOKEN -Value yourSandboxAccessToken
Link to section

Windows: Command shell

set SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
Link to section

Run the application

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

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

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

Link to section

See also