11# Square Python Library
22
33[ ![ fern shield] ( https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen )] ( https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fsquare%2Fsquare-python-sdk )
4- [ ![ pypi] ( https://img.shields.io/pypi/v/squareup )] ( https://pypi.python. org/pypi /squareup )
4+ [ ![ pypi] ( https://img.shields.io/pypi/v/squareup )] ( https://pypi.org/project /squareup )
55
66The Square Python library provides convenient access to the Square API from Python.
77
8- ## Requirements
9-
10- Use of the Python SDK requires:
11-
12- * Python 3 version 3.7 or higher
13-
148## Installation
159
1610``` sh
1711pip install squareup
1812```
1913
20- ## Reference
21-
22- A full reference for this library is available [ here] ( ./reference.md ) .
23-
24- ## Quickstart
25-
26- For more information, see [ Square Python SDK Quickstart] ( https://developer.squareup.com/docs/sdks/python/quick-start ) .
27-
2814## Usage
2915
3016Instantiate and use the client with the following:
@@ -33,18 +19,25 @@ Instantiate and use the client with the following:
3319from square import Square
3420
3521client = Square(
36- token = " YOUR_TOKEN" ,
22+ # This is the default and can be omitted.
23+ token = os.environ.get(" SQUARE_TOKEN" ),
3724)
3825client.payments.create(
3926 source_id = " ccof:GaJGNaZa8x4OgDJn4GB" ,
4027 idempotency_key = " 7b0f3ec5-086a-4871-8f13-3c81b3875218" ,
41- amount_money = {" amount" : 1000 , " currency" : " USD" },
42- app_fee_money = {" amount" : 10 , " currency" : " USD" },
28+ amount_money = {
29+ " amount" : 1000 ,
30+ " currency" : " USD"
31+ },
32+ app_fee_money = {
33+ " amount" : 10 ,
34+ " currency" : " USD"
35+ },
4336 autocomplete = True ,
4437 customer_id = " W92WH6P11H4Z77CTET0RNTGFW8" ,
4538 location_id = " L88917AVBK2S5" ,
4639 reference_id = " 123456" ,
47- note = " Brief description" ,
40+ note = " Brief description"
4841)
4942```
5043
@@ -57,134 +50,94 @@ import asyncio
5750
5851from square import AsyncSquare
5952
60- client = AsyncSquare(
61- token = " YOUR_TOKEN" ,
62- )
63-
64-
6553async def main () -> None :
54+ client = AsyncSquare(
55+ # This is the default and can be omitted.
56+ token = os.environ.get(" SQUARE_TOKEN" ),
57+ )
6658 await client.payments.create(
6759 source_id = " ccof:GaJGNaZa8x4OgDJn4GB" ,
6860 idempotency_key = " 7b0f3ec5-086a-4871-8f13-3c81b3875218" ,
69- amount_money = {" amount" : 1000 , " currency" : " USD" },
70- app_fee_money = {" amount" : 10 , " currency" : " USD" },
61+ amount_money = {
62+ " amount" : 1000 ,
63+ " currency" : " USD"
64+ },
65+ app_fee_money = {
66+ " amount" : 10 ,
67+ " currency" : " USD"
68+ },
7169 autocomplete = True ,
7270 customer_id = " W92WH6P11H4Z77CTET0RNTGFW8" ,
7371 location_id = " L88917AVBK2S5" ,
7472 reference_id = " 123456" ,
75- note = " Brief description" ,
73+ note = " Brief description"
7674 )
7775
7876
7977asyncio.run(main())
8078```
8179
82- ## Tests
83-
84- First, clone the repo locally and ` cd ` into the directory.
85-
86- ``` sh
87- git clone https://github.com/square/square-python-sdk.git
88- cd square-python-sdk
89- ```
90-
91- Next, install dependencies.
80+ ## Legacy SDK
9281
93- ``` sh
94- python3 -m pip install .
95- ```
82+ While the new SDK has a lot of improvements, we at Square understand that it takes time
83+ to upgrade when there are breaking changes. To make the migration easier, the old SDK
84+ is published as ` squareup_legacy ` so that the two SDKs can be used side-by-side in the
85+ same project.
9686
97- Before running the tests, find a sandbox token in your [ Developer Dashboard] and set a ` SQUARE_SANDBOX_TOKEN ` environment variable.
87+ Check out the [ example] ( ./example/README.md ) for a full demonstration, but the gist is
88+ shown below:
9889
99- ``` sh
100- export SQUARE_SANDBOX_TOKEN= " YOUR SANDBOX TOKEN HERE "
101- ```
90+ ``` python
91+ from square import Square
92+ from square_legacy.client import Client as LegacySquare
10293
103- Ensure you have ` pytest ` installed:
10494
105- ```
106- python3 -m pip install pytest
107- ```
108-
109- And lastly, run the tests.
95+ def main ():
96+ client = Square(token = os.environ.get(" SQUARE_TOKEN" ))
97+ legacy_client = LegacySquare(access_token = os.environ.get(" SQUARE_TOKEN" ))
11098
111- ``` sh
112- pytest
99+ ...
113100```
114101
115- ## SDK Reference
116-
117- ### Payments
118- * [ Payments]
119- * [ Refunds]
120- * [ Disputes]
121- * [ Checkout]
122- * [ Apple Pay]
123- * [ Cards]
124- * [ Payouts]
125-
126- ### Terminal
127- * [ Terminal]
128-
129- ### Orders
130- * [ Orders]
131- * [ Order Custom Attributes]
132-
133- ### Subscriptions
134- * [ Subscriptions]
102+ We recommend migrating to the new SDK using the following steps:
135103
136- ### Invoices
137- * [ Invoices]
104+ 1 . Upgrade the PyPi package to ^42.0.0
105+ 2 . Run ` pip install squareup_legacy `
106+ 3 . Search and replace all requires and imports from ` square ` to ` square_legacy `
107+ 4 . Gradually move over to use the new SDK by importing it from the ` square ` module
138108
139- ### Items
140- * [ Catalog]
141- * [ Inventory]
109+ ## Versioning
142110
143- ### Customers
144- * [ Customers]
145- * [ Customer Groups]
146- * [ Customer Segments]
111+ By default, the SDK is pinned to the latest version. If you would like
112+ to override this version you can specify it like so:
147113
148- ### Loyalty
149- * [ Loyalty]
150-
151- ### Gift Cards
152- * [ Gift Cards]
153- * [ Gift Card Activities]
154-
155- ### Bookings
156- * [ Bookings]
157- * [ Booking Custom Attributes]
158-
159- ### Business
160- * [ Merchants]
161- * [ Merchant Custom Attributes]
162- * [ Locations]
163- * [ Location Custom Attributes]
164- * [ Devices]
165- * [ Cash Drawers]
114+ ``` python
115+ client = Square(
116+ version = " 2025-03-19"
117+ )
118+ ```
166119
167- ### Team
168- * [ Team]
169- * [ Labor]
120+ ## Automatic Pagination
170121
171- ### Financials
172- * [ Bank Accounts ]
122+ Paginated requests will return a ` SyncPager ` or ` AsyncPager ` , which can be used
123+ as generators for the underlying object.
173124
174- ### Online
175- * [ Sites]
176- * [ Snippets]
125+ ``` python
126+ from square import Square
177127
178- ### Authorization
179- * [ Mobile Authorization]
180- * [ OAuth]
128+ client = Square()
129+ response = client.payments.list()
130+ for item in response:
131+ yield item
132+ # Alternatively, you can paginate page-by-page.
133+ for page in response.iter_pages():
134+ yield page
135+ ```
181136
182- ### Webhook Subscriptions
183- * [ Webhook Subscriptions]
184137## Exception Handling
185138
186- When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error
187- will be thrown.
139+ When the API returns a non-success status code (4xx or 5xx response), a subclass of
140+ the following error will be thrown.
188141
189142``` python
190143from square.core.api_error import ApiError
@@ -196,33 +149,32 @@ except ApiError as e:
196149 print (e.body)
197150```
198151
199- ## Pagination
152+ ## Webhook Signature Verification
200153
201- Paginated requests will return a ` SyncPager ` or ` AsyncPager ` , which can be used as generators for the underlying object.
154+ The SDK provides utility methods that allow you to verify webhook signatures and ensure
155+ that all webhook events originate from Square. The ` verify_signature ` method will verify
156+ the signature.
202157
203158``` python
204- from square import Square
159+ from square.utils.webhooks_helper import verify_signature
205160
206- client = Square(
207- token = " YOUR_TOKEN" ,
161+ is_valid = verify_signature(
162+ request_body = request_body,
163+ signature_header = request.headers[' x-square-hmacsha256-signature' ],
164+ signature_key = " YOUR_SIGNATURE_KEY" ,
165+ notification_url = " https://example.com/webhook" , # The URL where event notifications are sent.
208166)
209- response = client.bank_accounts.list()
210- for item in response:
211- yield item
212- # alternatively, you can paginate page-by-page
213- for page in response.iter_pages():
214- yield page
215167```
216168
217169## Advanced
218170
219171### Retries
220172
221173The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
222- as the request is deemed retryable and the number of retry attempts has not grown larger than the configured
174+ as the request is deemed retriable and the number of retry attempts has not grown larger than the configured
223175retry limit (default: 2).
224176
225- A request is deemed retryable when any of the following HTTP status codes is returned:
177+ A request is deemed retriable when any of the following HTTP status codes is returned:
226178
227179- [ 408] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408 ) (Timeout)
228180- [ 429] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429 ) (Too Many Requests)
@@ -231,9 +183,14 @@ A request is deemed retryable when any of the following HTTP status codes is ret
231183Use the ` max_retries ` request option to configure this behavior.
232184
233185``` python
234- client.payments.create(... , request_options = {
235- " max_retries" : 1
236- })
186+ from square.core.request_options import RequestOptions
187+
188+ client.payments.create(
189+ ... ,
190+ request_options = RequestOptions(
191+ max_retries = 1
192+ )
193+ )
237194```
238195
239196### Timeouts
@@ -249,17 +206,19 @@ client = Square(
249206 timeout = 20.0 ,
250207)
251208
252-
253209# Override timeout for a specific method
254- client.payments.create(... , request_options = {
255- " timeout_in_seconds" : 1
256- })
210+ client.payments.create(
211+ ... ,
212+ request_options = RequestOptions(
213+ timeout_in_seconds = 20
214+ )
215+ )
257216```
258217
259218### Custom Client
260219
261- You can override the ` httpx ` client to customize it for your use-case. Some common use-cases include support for proxies
262- and transports.
220+ You can override the ` httpx ` client to customize it for your use-case. Some common use-cases
221+ include support for proxies and transports.
263222
264223``` python
265224import httpx
0 commit comments