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.
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:
Python - Square supports Python version 3.7 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 ./quickstart
In your project directory, create a new file named quickstart.py with the following content:
from square.http.auth.o_auth_2 import BearerAuthCredentials from square.client import Client import os client = Client( bearer_auth_credentials=BearerAuthCredentials( access_token=os.environ['SQUARE_ACCESS_TOKEN'] ), environment='sandbox') result = client.locations.list_locations() if result.is_success(): for location in result.body['locations']: print(f"{location['id']}: ", end="") print(f"{location['name']}, ", end="") print(f"{location['address']['address_line_1']}, ", end="") print(f"{location['address']['locality']}") elif result.is_error(): for error in result.errors: print(error['category']) print(error['code']) print(error['detail'])Save the quickstart.py file.
This code does the following:
- Creates a Client object using your Square access token. For more information, see Set your Square credentials.
- Calls the list_locations method on the client object.
- If the request is successful, the code prints the location information on the terminal.
The Python 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
Run the following command:
python ./quickstart.pyVerify the result. You should see at least one location (Square creates one location when you create a Square account).