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.

  2. Get a Sandbox access token from the Developer Console.

  3. Install the following:

    • PHP - If you don't already have php installed on your machine.
    • Composer - If you don't already have composer installed on your machine.

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

Installation

Install the latest version by running the following command:

composer require square/square
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. Set your square credentials in an .env file. In your project directory, create a new file named .env with the following content, replacing yourSandboxAccessToken with your Square Sandbox access token:

    SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
Link to section

Write code

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

    This code does the following:

    • Loads the environment file containing your Square access token.
    • Creates a new SquareClient object with your Square access token.
    • Calls the $square->locations->list() method.
    • If the request is successful, the code prints the location information on the terminal.
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 Sandbox location when you create a Square account).

    LHI1YXJ8YSV5Z: Default Test Account, 1600 Pennsylvania Ave NW, Washington