Java SDK Migration Guide

Version 44.0.0.20250319 of the Java SDK represents a full rewrite of the SDK with a number of breaking changes, outlined in the following sections. When upgrading to this version or later versions, take note of the changes in the SDK, including client construction and parameter names. If necessary, you can use the legacy version of the SDK along with the latest version.

SDK versions 43.1.0.20250220 and earlier continue to function as expected, but you should plan to update your integration as soon as possible to ensure continued support.

Link to section

Client construction

The new Client class has some breaking changes and is constructed in a slightly different way than the legacy version of the SDK.

LegacyNew Additional information
accessTokentokenThe access token used for authentication.
bearerAuthCredentialsRemovedSet using the token or SQUARE_TOKEN environment variable.
customUrlurlSets the base URL for API requests and defaults to https://connect.squareup.com.
squareVersionversionThe Square API version to use.
additionalHeadersRemovedAvailable through ClientOptions and RequestOptions objects.
userAgentDetailRemovedConfigurable by setting the OkHttp client directly.
timeouttimeoutNow takes an int rather than a long.
httpCallbackRemovedConfigurable by setting the OkHttp client directly.
httpClientConfigRemovedConfigurable by setting the OkHttp client directly.
new BuilderbuilderSee the following example.
Link to section

Use the legacy and new SDK side by side

While the new SDK has many improvements, it takes time to upgrade when there are breaking changes. To make the migration easier, the new SDK exports the legacy SDK as com.squareup.square.legacy. The following example shows how to use the new and legacy SDKs inside a single file:

You should migrate to the new SDK using the following steps:

  1. Include the following dependencies in your project:

    <dependency> <groupId>com.squareup</groupId> <artifactId>square</artifactId> <version>44.0.0.20250319</version> </dependency> <dependency> <groupId>com.squareup</groupId> <artifactId>square-legacy</artifactId> <version>44.0.0.20250319</version> </dependency>
  2. Search and replace all imports from com.squareup.square to com.squareup.square.legacy.

  3. Gradually introduce the new SDK by importing it from the com.squareup.square import.

Link to section

Request builder constructors

There are two main differences between the request builders in the legacy SDK and the new SDK:

  • Instantiation - The legacy SDK builders are instantiated through their internal class constructors, whereas the new SDK builders are instantiated through static builder() methods.

  • Required fields - In the legacy SDK, required fields are passed in as parameters to the builder constructor, while the new SDK has staged builders that won't expose the .build() method until all required fields are set.

// LEGACY CreatePaymentRequest request = new CreatePaymentRequest.Builder("SOURCE_ID", "IDEMPOTENCY_KEY") .amountMoney(new Money.Builder().amount(20L).currency("USD").build()) .tipMoney(new Money.Builder().amount(3L).currency("USD").build()) .build(); // NEW CreatePaymentRequest request = CreatePaymentRequest.builder() .sourceId("SOURCE_ID") .idempotencyKey(UUID.randomUUID().toString()) .amountMoney(Money.builder().amount(20L).currency(Currency.USD).build()) .tipMoney(Money.builder().amount(3L).currency(Currency.USD).build()) .build();
Link to section

API classes and method name changes

Previously, API classes were named after the resource you're interacting with and suffixed with Api. To interact with customer resources, you use the client.getCustomersApi() method. The method was named based on the action you want to perform and the same resource name specified earlier. To list customers, you call client.getCustomersApi().listCustomers();.

// LEGACY client.getCustomersApi().listCustomers();

The new API classes are named by the resource without the Api suffix, and the methods now name the action without mentioning the resource a second time.

// NEW client.customers().list();

When interacting with a nested resource, you can chain the resources using dot-notation.

// NEW client.customers().groups().list();

Some other class and method names might have changed. The differences are easiest to discover by exploring the new method signatures.

Link to section

Auto-pagination

Square's paginated endpoints now support auto-pagination with an iterator. Callers don’t need to manually fetch the next page. You can now iterate over the entire list and the client automatically fetches the next page behind the scenes.

The following example shows the same code, with the legacy and new SDKs:

If you need to paginate page by page, you can still do so.

// First page List<Payment> pagePayments = payments.getItems(); for (Payment payment : pagePayments) { // ... } // Remaining pages while (payments.hasNext()) { pagePayments = payments.nextPage().getItems(); for (Payment payment : pagePayments) { // ... } }
Link to section

Async calls

In the legacy SDK, asynchronous calls can be made on the same client as synchronous calls by appending Async to the method name. In the new SDK, async calls must be made to a class with the same name as the synchronous client with Async prepended:

// LEGACY SquareClient legacyClient = new SquareClient.Builder().accessToken("YOUR_TOKEN").build(); ListLocationsResponse syncResponse = legacyClient.getLocationsApi().listLocations(); CompletableFuture<ListLocationsResponse> asyncResponse = legacyClient.getLocationsApi().listLocationsAsync(); // NEW SquareClient client = SquareClient.builder().token("YOUR_TOKEN").build(); AsyncSquareClient asyncClient = AsyncSquareClient.builder().token("YOUR_TOKEN").build(); ListLocationsResponse syncResponse = client.locations().list(); CompletableFuture<ListLocationsResponse> asyncResponse = asyncClient.locations().list();
Link to section

Explicit null values

In the legacy SDK, explicit null values can be sent using null values in the builder methods. In the new SDK, they can be set using Nullable.ofNull().

// LEGACY new Transaction.Builder().clientId(null).build(); // NEW Transaction.builder().clientId(Nullable.ofNull()).build(); Transaction.builder().clientId(Nullable.of(null)).build(); // also valid