.NET SDK Migration Guide

Version 41.0.0 of the .NET 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 40.1.0 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

To create a new client, you construct it using new and passing in parameters, as opposed to the legacy client that uses a Java-esque builder pattern.

using Square; // new var client = new SquareClient( token, // optional if you configure SQUARE_TOKEN environment variable new ClientOptions { BaseUrl = SquareEnvironment.Production // default, so you can omit ClientOptions parameter } ); // legacy var legacyClient = new Square.Legacy.SquareClient.Builder() .BearerAuthCredentials(new BearerAuthModel.Builder(token).Build()) .Environment(Square.Legacy.Environment.Production) .Build();
LegacyNew Additional information
AccessTokenTokenThe access token used for authentication.
BearerAuthCredentialsRemovedSet using the Token or SQUARE_TOKEN environment variable.
CustomUrlClientOptions.BaseUrlSets the base URL for API requests and defaults to https://connect.squareup.com.
SquareVersionClientOptions.VersionThe Square API version to use.
AdditionalHeadersClientOptions.AdditionalHeadersAvailable through ClientOptions.AdditionalHeaders.
UserAgentDetailRemovedSet the user agent header on ClientOptions.DefaultHeaders.
TimeoutTimeoutAccepts a TimeSpan and the default is 30 seconds.
HttpCallbackRemovedConfigure the HTTP client and options on the ClientOptions.
HttpClientConfigRemovedConfigure the HTTP client and options on the ClientOptions.
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 old SDK is available in the Square.Legacy NuGet package. 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. Install the Square.Legacy NuGet package alongside the existing Square SDK.
  2. Search and replace all using statements from Square to Square.Legacy.
  3. Gradually introduce the new SDK by importing it from the Square namespace.
Link to section

Request construction

The legacy SDK allows you to instantiate a request either using a constructor and positional parameters or using a builder pattern. With the new SDK, you simply create a new instance of the request and use the property initializer to set the properties.

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.CustomersApi property. Methods were named based on the action you want to perform and the same resource name specified earlier. To list customers, you call client.CustomersApi.ListCustomersAsync().

// Legacy client.CustomersApi.ListCustomersAsync()

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.ListAsync()

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

// New client.Customers.Groups.ListAsync();

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.

await foreach (var page in payments.AsPagesAsync()) { foreach (var payment in page.Items) { Console.WriteLine( "payment: ID: {0}, Created at: {1}, Updated at: {2}", payment.Id, payment.CreatedAt, payment.UpdatedAt ); } }
Link to section

Synchronous calls

In the legacy SDK, you can make synchronous calls. Following modern .NET standards, synchronous API calls are no longer supported.

Link to section

Explicit null values

In the legacy SDK, explicit null values can be sent using null values. The new SDK uses RequestOptions.AdditionalBodyProperties.

Note

You can pass anything into AdditionalBodyProperties that translates to a JSON object, but you need to use a dictionary to ensure null values aren't omitted.