Learn how to quickly set up and test the Square Go 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 the following:
- Go - Square supports Go version 1.18 or later.
Open a new terminal window.
Create a new directory for your project, navigate to that directory, and initialize a new Go module.
mkdir quickstart cd quickstart go mod init quickstartRun the following command to use the Square Go library in your module:
go get github.com/square/square-go-sdk
- In your project directory, create a new file named quickstart.go with the following content:
import (
"context"
"fmt"
"os"
squareclient "github.com/square/square-go-sdk/client"
"github.com/square/square-go-sdk/option"
)
func main() {
client := squareclient.NewClient(
option.WithToken(os.Getenv("SQUARE_ACCESS_TOKEN")),
)
// Get the list of locations
response, err := client.Locations.List(context.TODO())
if err != nil {
fmt.Println(err)
} else {
for l := range response.Locations {
fmt.Printf("ID: %s \n", *response.Locations[l].ID)
fmt.Printf("Name: %s \n", *response.Locations[l].Name)
fmt.Printf("Address: %s \n", *response.Locations[l].Address.AddressLine1)
fmt.Printf("%s \n", *response.Locations[l].Address.Locality)
}
fmt.Println(response)
}
}
This code does the following:
- Creates a new
squareclient
object with your Square access token. For more information, see Set your Square credentials. - Calls the
Locations.List
method. - If the request is successful, your Square location details are printed in the terminal window.
The Go code in this Quickstart reads your Square Sandbox access token from the SQUARE_ACCESS_TOKEN
environment variable. This helps avoid the use of hardcoded credentials in the code.
For the following commands, replace yourSandboxAccessToken
with your Square Sandbox access token:
export SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
Set-item -Path Env:SQUARE_ACCESS_TOKEN -Value yourSandboxAccessToken
set SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
- In your quickstart directory, run the
go build
command to compile the code. - Run your program with
./quickstart
. - Verify the result. You should see at least one location, even if you just created your Square account.