Square Java SDK Quickstart

Learn how to quickly set up and test the Square Java 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 Dashboard. For more information, see Make your First API Call.

  3. Install the following:

Link to section

Create a project

  1. Open a new terminal window. Use the Maven command-line interface (mvn) to create a simple project.

    mvn archetype:generate \ -DinteractiveMode=false \ -DgroupId=com.square.examples \ -DartifactId=quickstart \ -DarchetypeArtifactId=maven-archetype-quickstart
  2. After the command completes, go to your new project directory.

    cd ./quickstart

    Familiarize yourself with the directory structure and supporting files.

  3. Your project is defined by its project object model (POM). Open the pom.xml file in a text editor and replace its entire content with the following:

  4. Replace SDK_VERSION_HERE with the latest version of the Square Java SDK:

    <version>38.1.0.20240320</version>

  5. When finished, save the pom.xml file.

Link to section

Write code

  1. In your project's src/main/java/com/square/examples subdirectory, create a new file named Quickstart.java with the following content:

  2. Save the Quickstart.java file.

    This code does the following:

    • Reads a configuration file that contains your Square access token. For more information, see Set your Square credentials.
    • Creates a SquareClient object that uses the credentials to authenticate with Square.
    • Creates a LocationsApi object that uses the SquareClient object to make API calls.
    • Calls the listLocationsAsync method of the LocationsApi object to retrieve the locations associated with the Square account.
    • If the request is successful, the code prints the location information on the terminal.
Link to section

Set your Square credentials

The Java code in this Quickstart reads your Square Sandbox access token from a separate configuration file. This helps avoid the use of hardcoded credentials in the code. Do the following:

  1. Create a new subdirectory (src/main/resources) and then, within that subdirectory, create a file named config.properties with the following content:

    SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
  2. Replace yourSandboxAccessToken with your Square Sandbox access token.

  3. When finished, save the config.properties file.

Link to section

Run the application

  1. Run the following command:

    mvn package -DskipTests

    Maven compiles your program into bytecode and prepares it for execution.

  2. Run the following command:

    mvn exec:java -Dexec.mainClass="com.square.examples.Quickstart"
  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