Announcing Square’s New .NET SDK

Announcing Square’s New .NET SDK

Use the New .NET SDK to Integrate with Square APIs

We’re excited to announce our new .NET 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.

There are a few ways to install the Square package.

Install with Package Manager Console

Install-Package Square

Install with NuGet

nuget install Square

Install with .NET Core

dotnet add package Square

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

SquareClient client = new SquareClient.Builder()
            .environment(Environment.SANDBOX)
            .accessToken("TOKEN HERE")
            .build();

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

var response = square.LocationsApi.ListLocationsAsync();

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 .NET package, you might be wondering how the new .NET SDK compares. Let’s take a look! For example, let’s create the following customer along with their address:

var bodyAddress = new Address.Builder()
     .AddressLine1("500 Electric Ave")
     .AddressLine2("Suite 600")
     .Locality("New York")
     .AdministrativeDistrictLevel1("NY")
     .PostalCode("10003")
     .Country("US")
     .Build();
var body = new CreateCustomerRequest.Builder()
     .GivenName("Amelia")
     .FamilyName("Earhart")
     .EmailAddress("[email protected]")
     .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 .NET Connect SDK:

using System;
using System.Diagnostics;
using Square.Connect.Api;
using Square.Connect.Client;
using Square.Connect.Model;
 
// Instantiate the client
var configuration = new Configuration(new ApiClient("https://connect.squareupsandbox.com"));
configuration.AccessToken = "YOUR_SANDBOX_ACCESS_TOKEN";
var customersApi = new CustomersApi(configuration);
 
// Call the endpoint and handle the response
try
{
 // CreateCustomer
 CreateCustomerResponse result = customersApi.CreateCustomer(body);
 Debug.WriteLine(result);
}
catch (Exception e)
{
 Debug.Print("Exception when calling CustomersApi.CreateCustomer: " + e.Message );
}

For comparison, let’s create the same customer with the new Square SDK in C#:

using Square;
using Square.Models;
using Square.Exceptions;
 
SquareClient square = new SquareClient.Builder()
               .Environment(Square.Environment.Sandbox)
               .AccessToken("YOUR_SQUARE_ACCESS_TOKEN")
               .Build();
 
try
{
   CreateCustomerResponse result = square.CustomersApi.CreateCustomerAsync(body).Result;
   // Business logic
}
catch (APIException e)
{
   // Error Handling
};

That’s all there is to it!

As you can see, the new .NET 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 .NET SDK a try today by installing it with nuget install Square.

With the new .NET SDK it’s easier than ever to use Square as a platform to run your business with .NET. 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.

Table Of Contents
View More Articles ›