Hi Square devs,
I am trying to create square client using c# dot net framework 4.8 and facing compile time errors.
public static SquareClient CreateClient()
{
return new SquareClient(
Environment.GetEnvironmentVariable("TEST_SQUARE_TOKEN")
?? "ACCESS_TOKEN"
?? throw new Exception("TEST_SQUARE_TOKEN environment variable is not set"),
new ClientOptions { **BaseUrl = SquareEnvironment.Sandbox** }
);
}


Dot net framework 4.8 have c# version 7.3 and Square SDK is built on c# 9 , I am unable to set BaseURL value , as per Square Documentation above dot net framework 4.6 it must be working but actually it is not working as seen in screenshot.
I **tried another way** to create client but here in this method Builder object is not found.
client = new SquareClient.Builder() // here builder is not defined
.Environment(Square.Environment.Sandbox)
.AccessToken(accessToken)
.Build();
I need a proper solution for targeted Dot Net framework 4.8.
I want to see **CreatePayment** process complete flow and example code.
Thank you.
I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:
Additional Documentation
.NET SDK
Download, Configure, and Run the Client Sample
Set up SDK for a .NET Project
If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.
To create a Square client in .NET Framework 4.8, you can use the following code:
var builder = new SquareClient.Builder()
{
Environment = Square.Environment.Sandbox,
AccessToken = "your-access-token"
};
var client = builder.Build();
In this code, replace “your-access-token” with your actual access token. This code creates a new instance of SquareClient
using the builder pattern. The Environment
is set to Square.Environment.Sandbox
for testing in the Sandbox environment, and the AccessToken
is set to your actual access token.
For the CreatePayment process, you can use the following example code:
var amountMoney = new Money.Builder()
{
Amount = 100,
Currency = "USD"
}.Build();
var createPaymentRequest = new CreatePaymentRequest.Builder(
"cnon:CBASEDcp22SoN8POKR_FvgjPndM",
Guid.NewGuid().ToString())
{
AmountMoney = amountMoney
}.Build();
try
{
var response = await client.PaymentsApi.CreatePaymentAsync(createPaymentRequest);
Console.WriteLine($"Payment successfully created. Payment ID: {response.Payment.Id}");
}
catch (ApiException e)
{
Console.WriteLine($"Error occurred while creating payment: {e.Message}");
}
In this code, replace "cnon:CBASEDcp22SoN8POKR_FvgjPndM"
with your actual nonce. This code creates a new payment of $1.00 USD. If the payment is successfully created, it prints the payment ID. If an error occurs, it prints the error message.
This answer was reviewed by @Bryan-Square.
Also please never share your access token, they are secret. All other values are safe to share. 