Learn how to quickly set up and test the Square .NET SDK.
Before you begin, you need a Square account and account credentials. You use the Square Sandbox for the Quickstart exercise.
- Create a Square account and an application. For more information, see Create an Account and Application.
- Get a Sandbox access token from the Developer Console. For more information, see Get a personal access token.
- Install .NET if you don't already have it on your computer.
You can now use one of the following workflows depending on whether you're using Visual Studio.
Note
If you prefer to skip the following setup steps, download the Square .NET SDK Quickstart sample and follow the instructions in the README.
Open a new terminal window.
Find or create a new folder for your .NET project.
In that folder, run the following command to create a new .NET project:
dotnet new console --name QuickstartAdd an application settings file to the project to store credentials. Go to the newly created Quickstart folder and add the following
appsettings.json
file, replacingYOUR-SANDBOX-ACCESS-TOKEN
with your Sandbox access token:{ "AppSettings": { "AccessToken": "YOUR-SANDBOX-ACCESS-TOKEN" } }Run the following commands to install the necessary packages:
dotnet add package Square dotnet add package Microsoft.Extensions.Configuration.Json dotnet add package Microsoft.Extensions.ConfigurationThe
Microsoft.Extensions.Configuration
andpackage Microsoft.Extensions.Configuration.Json
packages are used to manage the application settings file.
In your Quickstart folder, find and open
Program.cs
in your preferred editor.Replace the contents with the following code and save the file:
using Square; using Square.Locations; using Microsoft.Extensions.Configuration; namespace ExploreLocationsAPI { public class Program { private static SquareClient client = null!; private static IConfigurationRoot config = null!; static async Task Main(string[] args) { var builder = new ConfigurationBuilder() .AddJsonFile($"appsettings.json", true, true); config = builder.Build(); var accessToken = config["AppSettings:AccessToken"]; client = new SquareClient( accessToken, new ClientOptions { BaseUrl = SquareEnvironment.Sandbox } ); await RetrieveLocationsAsync(); } static async Task RetrieveLocationsAsync() { try { var response = await client.Locations.ListAsync(); if (response.Locations != null) { foreach (var location in response.Locations) { Console.WriteLine($"location: country = {location.Country} name = {location.Name}"); } } else { Console.WriteLine("No locations found."); } } catch (SquareApiException e) { Console.WriteLine("SquareApiException occurred:"); Console.WriteLine("Status Code: {0}", e.StatusCode); Console.WriteLine("Error: {0}", e.Message); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e.Message}"); } } } }This code does the following:
- Reads your Square Sandbox access token from the
appsettings.json
file. - Creates a new client using the access token.
- Calls the
ListAsync
method on the Locations API to retrieve the locations for your Square account. - If the request is successful, the code prints the location information in the terminal.
- Reads your Square Sandbox access token from the
Run the following command:
dotnet run
You should see at least one location listed (Square creates one Sandbox location when you create a Square account).