Square 41.0.1
dotnet add package Square --version 41.0.1
NuGet\Install-Package Square -Version 41.0.1
<PackageReference Include="Square" Version="41.0.1" />
paket add Square --version 41.0.1
#r "nuget: Square, 41.0.1"
// Install Square as a Cake Addin #addin nuget:?package=Square&version=41.0.1 // Install Square as a Cake Tool #tool nuget:?package=Square&version=41.0.1
Square C# Library
The Square .NET library provides convenient access to the Square API from C#, VB.NET, and F#.
Requirements
The Square .NET SDK is supported with the following target frameworks:
- .NET 8 and above
- .NET Framework 4.6.2 and above
- .NET Standard 2.0 and above
Installation
dotnet add package Square
Usage
Instantiate and use the client with the following:
using Square.Payments;
using Square;
var client = new SquareClient();
await client.Payments.CreateAsync(
new CreatePaymentRequest
{
SourceId = "ccof:GaJGNaZa8x4OgDJn4GB",
IdempotencyKey = "7b0f3ec5-086a-4871-8f13-3c81b3875218",
AmountMoney = new Money { Amount = 1000, Currency = Currency.Usd },
AppFeeMoney = new Money { Amount = 10, Currency = Currency.Usd },
Autocomplete = true,
CustomerId = "W92WH6P11H4Z77CTET0RNTGFW8",
LocationId = "L88917AVBK2S5",
ReferenceId = "123456",
Note = "Brief description",
}
);
Instantiation
To get started with the Square SDK, instantiate the SquareClient
class as follows:
using Square;
var client = new SquareClient("SQUARE_TOKEN");
Alternatively, you can omit the token when constructing the client.
In this case, the SDK will automatically read the token from the
SQUARE_TOKEN
environment variable:
using Square;
var client = new SquareClient(); // Token is read from the SQUARE_TOKEN environment variable.
Environment and Custom URLs
This SDK allows you to configure different environments or custom URLs for API requests. You can either use the predefined environments or specify your own custom URL.
Environments
using Square;
var client = new SquareClient(clientOptions: new ClientOptions
{
BaseUrl = SquareEnvironment.Production // Used by default
});
Custom URL
using Square;
var client = new SquareClient(clientOptions: new ClientOptions
{
BaseUrl = "https://custom-staging.com"
});
Enums
This SDK uses forward-compatible enums that provide type safety while maintaining forward compatibility with API updates.
// Use predefined enum values
var accountType = BankAccountType.Checking;
// Use unknown/future enum values
var customType = BankAccountType.FromCustom("FUTURE_VALUE");
// String conversions and equality
string typeString = accountType.ToString(); // Returns "CHECKING"
var isChecking = accountType == "CHECKING"; // Returns true
// When writing switch statements, always include a default case
switch (accountType.Value) {
case BankAccountType.Values.Checking:
// Handle checking accounts
break;
case BankAccountType.Values.BusinessChecking:
// Handle business checking accounts
break;
default:
// Handle unknown values for forward compatibility
break;
}
Pagination
List endpoints are paginated. The SDK provides an async enumerable so that you can simply loop over the items:
using Square.BankAccounts;
using Square;
var client = new SquareClient();
var pager = await client.BankAccounts.ListAsync(new ListBankAccountsRequest());
await foreach (var item in pager)
{
// do something with item
}
Exception Handling
When the API returns a non-success status code (4xx or 5xx response), a SquareApiException
will be thrown.
using Square;
try {
var response = await client.Payments.CreateAsync(...);
} catch (SquareApiException e) {
Console.WriteLine(e.Body);
Console.WriteLine(e.StatusCode);
// Access the parsed error objects
foreach (var error in e.Errors) {
Console.WriteLine($"Category: {error.Category}");
Console.WriteLine($"Code: {error.Code}");
Console.WriteLine($"Detail: {error.Detail}");
Console.WriteLine($"Field: {error.Field}");
}
}
Webhook Signature Verification
The SDK provides utility methods that allow you to verify webhook signatures and ensure
that all webhook events originate from Square. The WebhooksHelper.verifySignature
method
can be used to verify the signature like so:
using Microsoft.AspNetCore.Http;
using Square;
public static async Task CheckWebhooksEvent(
HttpRequest request,
string signatureKey,
string notificationUrl
)
{
var signature = request.Headers["x-square-hmacsha256-signature"].ToString();
using var reader = new StreamReader(request.Body, System.Text.Encoding.UTF8);
var requestBody = await reader.ReadToEndAsync();
if (!WebhooksHelper.VerifySignature(requestBody, signature, signatureKey, notificationUrl))
{
throw new Exception("A webhook event was received that was not from Square.");
}
}
In .NET 6 and above, there are also overloads using spans for allocation free webhook verification.
Legacy SDK
While the new SDK has a lot of improvements, we at Square understand that it takes time to upgrade when there are breaking changes.
To make the migration easier, the legacy SDK is available as a separate NuGet package Square.Legacy
with all functionality under the Square.Legacy
namespace. Here's an example of how you can use the legacy SDK alongside the new SDK inside a single file:
using Square;
using Square.Legacy.Authentication;
var accessToken = "YOUR_SQUARE_TOKEN";
// NEW
var client = new SquareClient(accessToken);
// LEGACY
var legacyClient = new Square.Legacy.SquareClient.Builder()
.BearerAuthCredentials(
new BearerAuthModel.Builder(accessToken)
.Build())
.Environment(Square.Legacy.Environment.Production)
.Build();
We recommend migrating to the new SDK using the following steps:
- Install the
Square.Legacy
NuGet package alongside your existing Square SDK - Search and replace all using statements from
Square
toSquare.Legacy
- Gradually move over to use the new SDK by importing it from the
Square
namespace.
Advanced
Retries
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
Use the MaxRetries
request option to configure this behavior.
var response = await client.Payments.CreateAsync(
...,
new RequestOptions {
MaxRetries: 0 // Override MaxRetries at the request level
}
);
Timeouts
The SDK defaults to a 30 second timeout. Use the Timeout
option to configure this behavior.
var response = await client.Payments.CreateAsync(
...,
new RequestOptions {
Timeout: TimeSpan.FromSeconds(3) // Override timeout to 3s
}
);
Receive Additional Properties
Every response type includes the AdditionalProperties
property, which returns an IDictionary<string, JsonElement>
that contains any properties in the JSON response that were not specified in the returned class. Similar to the use case for sending additional parameters, this can be useful for API features not present in the SDK yet.
You can access the additional properties like so:
var payments = client.Payments.Create(...);
IDictionary<string, JsonElement> additionalProperties = payments.AdditionalProperties;
The AdditionalProperties
dictionary is populated automatically during deserialization using the [JsonExtensionData]
attribute. This provides you with access to any fields that may be added to the API response in the future before they're formally added to the SDK models.
Contributing
While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- OneOf (>= 3.0.271)
- OneOf.Extended (>= 3.0.271)
- Portable.System.DateTimeOnly (>= 8.0.2)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.5)
- System.Text.RegularExpressions (>= 4.3.1)
-
.NETStandard 2.0
- OneOf (>= 3.0.271)
- OneOf.Extended (>= 3.0.271)
- Portable.System.DateTimeOnly (>= 8.0.2)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.5)
- System.Text.RegularExpressions (>= 4.3.1)
-
net6.0
- OneOf (>= 3.0.271)
- OneOf.Extended (>= 3.0.271)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.5)
- System.Text.RegularExpressions (>= 4.3.1)
-
net7.0
- OneOf (>= 3.0.271)
- OneOf.Extended (>= 3.0.271)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.5)
- System.Text.RegularExpressions (>= 4.3.1)
-
net8.0
- OneOf (>= 3.0.271)
- OneOf.Extended (>= 3.0.271)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.5)
- System.Text.RegularExpressions (>= 4.3.1)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Square:
Package | Downloads |
---|---|
DanSaul.SharedCode
Package Description |
|
TheFusionWorks.Platforms
These are base utility classes developed by The Fusion Works which other packages and applications are built off. |
|
DanSaul.SharedCode.DatabaseSchemas
Package Description |
|
Rescope.Commerce.PaymentProcessors.Square
Square payment provider for Rescope Commerce on Umbraco. |
|
Vendr.Contrib.PaymentProviders.Square
Square payment provider for Vendr, the eCommerce package for Umbrao v8+ |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
41.0.1 | 170 | 3/19/2025 |
41.0.0 | 143 | 3/19/2025 |
40.1.0 | 2,494 | 2/20/2025 |
40.0.0 | 5,501 | 1/23/2025 |
39.1.0 | 3,349 | 12/18/2024 |
39.0.0 | 4,322 | 11/20/2024 |
38.2.0 | 10,099 | 10/17/2024 |
38.1.0 | 16,535 | 9/18/2024 |
38.0.0 | 8,934 | 8/21/2024 |
37.1.1 | 16,289 | 7/17/2024 |
37.1.0 | 17,426 | 6/3/2024 |
37.0.1 | 5,140 | 5/15/2024 |
37.0.0 | 213 | 5/14/2024 |
36.0.0 | 10,143 | 4/16/2024 |
35.1.0 | 12,275 | 3/20/2024 |
35.0.0 | 31,645 | 2/21/2024 |
34.0.1 | 9,144 | 1/17/2024 |
34.0.0 | 683 | 1/16/2024 |
33.1.0 | 7,603 | 12/12/2023 |
33.0.0 | 5,646 | 11/15/2023 |
32.0.0 | 14,741 | 10/17/2023 |
31.0.0 | 6,805 | 9/26/2023 |
30.0.0 | 8,835 | 8/15/2023 |
29.0.0 | 12,307 | 7/19/2023 |
28.0.1 | 17,178 | 6/14/2023 |
28.0.0 | 5,294 | 6/7/2023 |
27.0.0 | 28,825 | 5/16/2023 |
26.0.0 | 5,252 | 4/18/2023 |
25.2.0 | 37,990 | 3/14/2023 |
25.1.0 | 38,477 | 1/19/2023 |
25.0.0 | 16,096 | 12/13/2022 |
24.0.0 | 13,277 | 11/16/2022 |
23.0.0 | 16,015 | 10/19/2022 |
22.0.0 | 15,756 | 9/21/2022 |
21.1.0 | 16,169 | 8/22/2022 |
21.0.0 | 1,659 | 8/16/2022 |
20.1.0 | 10,805 | 7/21/2022 |
20.0.0 | 517 | 7/20/2022 |
19.1.0 | 46,286 | 6/16/2022 |
19.0.0 | 13,003 | 5/12/2022 |
18.0.0 | 9,443 | 4/19/2022 |
17.3.0 | 75,932 | 3/16/2022 |
17.2.0 | 17,873 | 2/15/2022 |
17.1.0 | 12,042 | 1/20/2022 |
17.0.0 | 15,614 | 12/15/2021 |
16.0.0 | 8,549 | 11/17/2021 |
15.0.0 | 2,503 | 10/20/2021 |
14.1.0 | 27,250 | 9/16/2021 |
13.1.0 | 13,931 | 8/18/2021 |
13.0.0 | 40,072 | 7/21/2021 |
12.0.0 | 29,751 | 6/15/2021 |
11.0.0 | 17,460 | 5/13/2021 |
10.0.0 | 2,302 | 4/21/2021 |
9.1.0 | 22,510 | 3/17/2021 |
9.0.0 | 9,711 | 2/26/2021 |
8.1.0 | 3,098 | 1/21/2021 |
8.0.0 | 11,444 | 12/16/2020 |
7.0.0 | 3,636 | 11/18/2020 |
6.5.0 | 10,900 | 10/28/2020 |
6.4.0 | 2,473 | 9/23/2020 |
6.3.0 | 10,674 | 8/26/2020 |
6.2.0 | 5,345 | 8/12/2020 |
6.1.0 | 8,852 | 7/22/2020 |
6.0.0 | 8,794 | 6/25/2020 |
5.3.0 | 16,436 | 5/28/2020 |
5.2.2 | 11,236 | 4/25/2020 |
5.2.1 | 579 | 4/22/2020 |
5.2.0 | 547 | 4/22/2020 |
5.1.0 | 3,224 | 3/25/2020 |
5.0.0 | 64,920 | 2/26/2020 |
4.1.0 | 7,421 | 1/23/2020 |
4.0.0 | 16,335 | 12/17/2019 |