Learn how to quickly set up and test the Square Python SDK.
Before you begin, you need a Square account and account credentials. You use the Square Sandbox for the Quickstart exercise.
Install the following:
Python - Square supports Python version 3.8 and later.
Square Python SDK - To install it, use the
pip
command:pip install squareup
Note
If you prefer to skip the following setup steps, download the Square Python SDK Quickstart sample and follow the instructions in the README.
Open a new terminal window. Create a new directory for your project, and then go to that directory.
mkdir quickstart cd ./quickstartSet your square credentials in an
.env
file. In your project directory, create a new file named.env
with the following content, replacingyourSandboxAccessToken
with your Square Sandbox access token:SQUARE_ACCESS_TOKEN=yourSandboxAccessToken
- In your project directory, create a new file named
quickstart.py
with the following content:
from square import Square from square.environment import SquareEnvironment from square.core.api_error import ApiError from dotenv import load_dotenv import os load_dotenv() client = Square( environment=SquareEnvironment.SANDBOX, token=os.environ['SQUARE_ACCESS_TOKEN'] ) try: response = client.locations.list() for location in response.locations: print(f"{location.id}: ", end="") print(f"{location.name}, ", end="") print(f"{location.address}, ", end="") except ApiError as e: for error in e.errors: print(error.category) print(error.code) print(error.detail)
Save the
quickstart.py
file.This code does the following:
- Loads the environment file containing your Square access token.
- Creates a Client object using your Square access token.
- Calls the
locations.list
method on the client object. - If the request is successful, the code prints the location information on the terminal.
Run the following command:
python ./quickstart.pyVerify the result. You should see at least one location (Square creates one sandbox location when you create a Square account).