Announcing Square’s New Java SDK

Use the New Java SDK to Integrate with Square APIs

Reddit
LinkedIn

We’re very pleased to announce our new Java SDK. You can use the SDK to integrate Square payments into your app. Run your business with Square APIs including Catalog, Customers, Employees, Inventory, Labor, Locations, Orders, and more.

To install the package, just run

mvn dependency:get=com.squareup:square:RELEASE

from the command line or install the client dynamically by adding a dependency to the POM for your project:

<dependency>
    <groupId>com.squareup</groupId>
    <artifactId>square</artifactId>
    <version>4.0.0.20191217</version>
</dependency>

Then after importing a few SDK packages, you’ll be ready to instantiate a client with your Square access token.

import com.squareup.square.SquareClient;
 
SquareClient square = new SquareClient.Builder()
   .environment(Environment.SANDBOX)
   .accessToken("YOUR_SANDBOX_ACCESS_TOKEN")
   .build();

With a client, we’re ready to use the Square API. For example, we can list locations.

square.getLocationsApi().listLocationsAsync().thenAccept(result -> {
   // Business logic
}).exceptionally(exception -> {
   // Exception handling
   return null;
});

One of the great features in the new SDK is that the response object contains new rich information about both the request and response. These details can be used for control flow and debugging.

If you were previously using the square_connect Java package, you might be wondering how the new Java SDK compares. Let’s take a look! For example, let’s create the following customer along with their address:

Address bodyAddress = new Address.Builder()
   .addressLine1("500 Electric Ave")
   .addressLine2("Suite 600")
   .locality("New York")
   .administrativeDistrictLevel1("NY")
   .postalCode("10003")
   .country("US")
   .build();
 
CreateCustomerRequest body = new CreateCustomerRequest.Builder()
   .givenName("Amelia")
   .familyName("Earhart")
   .emailAddress("Amelia.Earhart@example.com")
   .address(bodyAddress)
   .phoneNumber("1-212-555-4240")
   .referenceId("YOUR_REFERENCE_ID")
   .note("a customer")
   .build();

First, let’s look at how we used to create a customer with the deprecated square_connect SDK:

ApiClient defaultClient = Configuration.getDefaultApiClient();
CustomersApi customersApi = new CustomersApi();
 
try {
   CreateCustomerResponse result = customersApi.createCustomer(body);
   System.out.println(result);
} catch (ApiException e) {
   System.err.println("Exception when calling CustomersApi.createCustomer");
   e.printStackTrace();
}

For comparison, let’s create the same customer with the new Square Java SDK:

SquareClient square = new SquareClient.Builder()
   .environment(Environment.SANDBOX)
   .accessToken("YOUR_SANDBOX_ACCESS_TOKEN")
   .build();
 
square.getCustomersApi().createCustomerAsync(body).thenAccept(result -> {
   System.out.println("Successfully created customer with id:"+ result.getCustomer().getId());
}).exceptionally(e -> {
   System.err.println("Exception when calling CustomersApi.createCustomer");
   e.printStackTrace();
   return null;
});

That’s all there is to it! As you can see, the new Java SDK interface is much simpler, but provides richer data. The new package also ships with a ton of usability and debugging features.

Give the new Java SDK a try today by installing it with mvn dependency:get=com.squareup:square:RELEASE.

With the new Java SDK it’s easier than ever to use Square as a platform to run your business with Java. We can’t wait to see what you’ll build! If you have any questions or feedback, drop by our developer community. We’d love to hear from you.