squareup


Namesquareup JSON
Version 43.0.1.20250716 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2025-07-16 20:25:39
maintainerNone
docs_urlNone
authorNone
requires_python<4.0,>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Square Python Library

[![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)
[![pypi](https://img.shields.io/pypi/v/squareup)](https://pypi.org/project/squareup)

The Square Python library provides convenient access to the Square API from Python.

## Installation

```sh
pip install squareup
```

## Requirements

Use of the Square Python SDK requires:

* Python 3.8+

## Usage

Instantiate and use the client with the following:

```python
from square import Square

client = Square(
    # This is the default and can be omitted.
    token=os.environ.get("SQUARE_TOKEN"),
)
client.payments.create(
    source_id="ccof:GaJGNaZa8x4OgDJn4GB",
    idempotency_key="7b0f3ec5-086a-4871-8f13-3c81b3875218",
    amount_money={
        "amount": 1000,
        "currency": "USD"
    },
    app_fee_money={
        "amount": 10,
        "currency": "USD"
    },
    autocomplete=True,
    customer_id="W92WH6P11H4Z77CTET0RNTGFW8",
    location_id="L88917AVBK2S5",
    reference_id="123456",
    note="Brief description"
)
```

## Async Client

The SDK also exports an `async` client so that you can make non-blocking calls to our API.

```python
import asyncio

from square import AsyncSquare

async def main() -> None:
    client = AsyncSquare(
        # This is the default and can be omitted.
        token=os.environ.get("SQUARE_TOKEN"),
    )
    await client.payments.create(
        source_id="ccof:GaJGNaZa8x4OgDJn4GB",
        idempotency_key="7b0f3ec5-086a-4871-8f13-3c81b3875218",
        amount_money={
            "amount": 1000,
            "currency": "USD"
        },
        app_fee_money={
            "amount": 10,
            "currency": "USD"
        },
        autocomplete=True,
        customer_id="W92WH6P11H4Z77CTET0RNTGFW8",
        location_id="L88917AVBK2S5",
        reference_id="123456",
        note="Brief description"
    )


asyncio.run(main())
```

## Legacy SDK

While the new SDK has a lot of improvements, we at Square understand that it takes time
to upgrade when there are breaking changes. To make the migration easier, the old SDK
is published as `squareup_legacy` so that the two SDKs can be used side-by-side in the
same project.

Check out the [example](./example/README.md) for a full demonstration, but the gist is
shown below:

```python
from square import Square
from square_legacy.client import Client as LegacySquare


def main():
    client = Square(token=os.environ.get("SQUARE_TOKEN"))
    legacy_client = LegacySquare(access_token=os.environ.get("SQUARE_TOKEN"))

    ...
```

We recommend migrating to the new SDK using the following steps:

1. Upgrade the PyPi package to ^42.0.0
2. Run `pip install squareup_legacy`
3. Search and replace all requires and imports from `square` to `square_legacy`
4. Gradually move over to use the new SDK by importing it from the `square` module

## Versioning

By default, the SDK is pinned to the latest version. If you would like
to override this version you can specify it like so:

```python
client = Square(
    version="2025-03-19"
)
```

## Automatic Pagination

Paginated requests will return a `SyncPager` or `AsyncPager`, which can be used
as generators for the underlying object.

```python
from square import Square

client = Square()
response = client.payments.list()
for item in response:
    yield item
# Alternatively, you can paginate page-by-page.
for page in response.iter_pages():
    yield page
```

## File Uploads

Files are uploaded with the [File](./src/square/core/file.py) type, which is constructed as a tuple in
a variety of formats. You can customize the filename and `Content-Type` of the individual `multipart/form-data`
part like so:

```python
invoice_pdf = client.invoices.create_invoice_attachment(
    invoice_id="inv:0-ChA4-3sU9GPd-uOC3HgvFjMWEL4N",
    image_file=(
         os.path.basename(pdf_filepath), # The filename to include in the `multipart/form-data` part.
         open(pdf_filepath, "rb"),       # The file stream, read as binary data.
         "application/pdf"               # The Content-Type for this particular file.
    ),
    request={
        "idempotency_key": str(uuid.uuid4()),
        "description": f"Invoice-{pdf_filepath}",
    }
)
```

## Exception Handling

When the API returns a non-success status code (4xx or 5xx response), a subclass of
the following error will be thrown.

```python
from square.core.api_error import ApiError

try:
    client.payments.create(...)
except ApiError as e:
    print(e.status_code)
    print(e.body)
```

## Webhook Signature Verification

The SDK provides utility methods that allow you to verify webhook signatures and ensure
that all webhook events originate from Square. The `verify_signature` method will verify
the signature.

```python
from square.utils.webhooks_helper import verify_signature

is_valid = verify_signature(
    request_body=request_body,
    signature_header=request.headers['x-square-hmacsha256-signature'],
    signature_key="YOUR_SIGNATURE_KEY",
    notification_url="https://example.com/webhook", # The URL where event notifications are sent.
)
```

## Advanced

### Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
as the request is deemed retriable and the number of retry attempts has not grown larger than the configured
retry limit (default: 2).

A request is deemed retriable when any of the following HTTP status codes is returned:

- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)

Use the `max_retries` request option to configure this behavior.

```python
from square.core.request_options import RequestOptions

client.payments.create(
    ...,
    request_options=RequestOptions(
        max_retries=1
    )
)
```

### Timeouts

The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.

```python

from square import Square

client = Square(
    ...,
    timeout=20.0,
)

# Override timeout for a specific method
client.payments.create(
    ...,
    request_options=RequestOptions(
        timeout_in_seconds=20
    )
)
```

### Custom Client

You can override the `httpx` client to customize it for your use-case. Some common use-cases
include support for proxies and transports.

```python
import httpx
from square import Square

client = Square(
    ...,
    httpx_client=httpx.Client(
        proxies="http://my.test.proxy.example.com",
        transport=httpx.HTTPTransport(local_address="0.0.0.0"),
    ),
)
```

## Contributing

While we value open-source contributions to this SDK, this library is generated programmatically.
Additions made directly to this library would have to be moved over to our generation code,
otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "squareup",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/79/00/5a7aeabfbeb95c697e4944cd556d496e1e6863dbbd402ab1dc90b44f907f/squareup-43.0.1.20250716.tar.gz",
    "platform": null,
    "description": "# Square Python Library\n\n[![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)\n[![pypi](https://img.shields.io/pypi/v/squareup)](https://pypi.org/project/squareup)\n\nThe Square Python library provides convenient access to the Square API from Python.\n\n## Installation\n\n```sh\npip install squareup\n```\n\n## Requirements\n\nUse of the Square Python SDK requires:\n\n* Python 3.8+\n\n## Usage\n\nInstantiate and use the client with the following:\n\n```python\nfrom square import Square\n\nclient = Square(\n    # This is the default and can be omitted.\n    token=os.environ.get(\"SQUARE_TOKEN\"),\n)\nclient.payments.create(\n    source_id=\"ccof:GaJGNaZa8x4OgDJn4GB\",\n    idempotency_key=\"7b0f3ec5-086a-4871-8f13-3c81b3875218\",\n    amount_money={\n        \"amount\": 1000,\n        \"currency\": \"USD\"\n    },\n    app_fee_money={\n        \"amount\": 10,\n        \"currency\": \"USD\"\n    },\n    autocomplete=True,\n    customer_id=\"W92WH6P11H4Z77CTET0RNTGFW8\",\n    location_id=\"L88917AVBK2S5\",\n    reference_id=\"123456\",\n    note=\"Brief description\"\n)\n```\n\n## Async Client\n\nThe SDK also exports an `async` client so that you can make non-blocking calls to our API.\n\n```python\nimport asyncio\n\nfrom square import AsyncSquare\n\nasync def main() -> None:\n    client = AsyncSquare(\n        # This is the default and can be omitted.\n        token=os.environ.get(\"SQUARE_TOKEN\"),\n    )\n    await client.payments.create(\n        source_id=\"ccof:GaJGNaZa8x4OgDJn4GB\",\n        idempotency_key=\"7b0f3ec5-086a-4871-8f13-3c81b3875218\",\n        amount_money={\n            \"amount\": 1000,\n            \"currency\": \"USD\"\n        },\n        app_fee_money={\n            \"amount\": 10,\n            \"currency\": \"USD\"\n        },\n        autocomplete=True,\n        customer_id=\"W92WH6P11H4Z77CTET0RNTGFW8\",\n        location_id=\"L88917AVBK2S5\",\n        reference_id=\"123456\",\n        note=\"Brief description\"\n    )\n\n\nasyncio.run(main())\n```\n\n## Legacy SDK\n\nWhile the new SDK has a lot of improvements, we at Square understand that it takes time\nto upgrade when there are breaking changes. To make the migration easier, the old SDK\nis published as `squareup_legacy` so that the two SDKs can be used side-by-side in the\nsame project.\n\nCheck out the [example](./example/README.md) for a full demonstration, but the gist is\nshown below:\n\n```python\nfrom square import Square\nfrom square_legacy.client import Client as LegacySquare\n\n\ndef main():\n    client = Square(token=os.environ.get(\"SQUARE_TOKEN\"))\n    legacy_client = LegacySquare(access_token=os.environ.get(\"SQUARE_TOKEN\"))\n\n    ...\n```\n\nWe recommend migrating to the new SDK using the following steps:\n\n1. Upgrade the PyPi package to ^42.0.0\n2. Run `pip install squareup_legacy`\n3. Search and replace all requires and imports from `square` to `square_legacy`\n4. Gradually move over to use the new SDK by importing it from the `square` module\n\n## Versioning\n\nBy default, the SDK is pinned to the latest version. If you would like\nto override this version you can specify it like so:\n\n```python\nclient = Square(\n    version=\"2025-03-19\"\n)\n```\n\n## Automatic Pagination\n\nPaginated requests will return a `SyncPager` or `AsyncPager`, which can be used\nas generators for the underlying object.\n\n```python\nfrom square import Square\n\nclient = Square()\nresponse = client.payments.list()\nfor item in response:\n    yield item\n# Alternatively, you can paginate page-by-page.\nfor page in response.iter_pages():\n    yield page\n```\n\n## File Uploads\n\nFiles are uploaded with the [File](./src/square/core/file.py) type, which is constructed as a tuple in\na variety of formats. You can customize the filename and `Content-Type` of the individual `multipart/form-data`\npart like so:\n\n```python\ninvoice_pdf = client.invoices.create_invoice_attachment(\n    invoice_id=\"inv:0-ChA4-3sU9GPd-uOC3HgvFjMWEL4N\",\n    image_file=(\n         os.path.basename(pdf_filepath), # The filename to include in the `multipart/form-data` part.\n         open(pdf_filepath, \"rb\"),       # The file stream, read as binary data.\n         \"application/pdf\"               # The Content-Type for this particular file.\n    ),\n    request={\n        \"idempotency_key\": str(uuid.uuid4()),\n        \"description\": f\"Invoice-{pdf_filepath}\",\n    }\n)\n```\n\n## Exception Handling\n\nWhen the API returns a non-success status code (4xx or 5xx response), a subclass of\nthe following error will be thrown.\n\n```python\nfrom square.core.api_error import ApiError\n\ntry:\n    client.payments.create(...)\nexcept ApiError as e:\n    print(e.status_code)\n    print(e.body)\n```\n\n## Webhook Signature Verification\n\nThe SDK provides utility methods that allow you to verify webhook signatures and ensure\nthat all webhook events originate from Square. The `verify_signature` method will verify\nthe signature.\n\n```python\nfrom square.utils.webhooks_helper import verify_signature\n\nis_valid = verify_signature(\n    request_body=request_body,\n    signature_header=request.headers['x-square-hmacsha256-signature'],\n    signature_key=\"YOUR_SIGNATURE_KEY\",\n    notification_url=\"https://example.com/webhook\", # The URL where event notifications are sent.\n)\n```\n\n## Advanced\n\n### Retries\n\nThe SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long\nas the request is deemed retriable and the number of retry attempts has not grown larger than the configured\nretry limit (default: 2).\n\nA request is deemed retriable when any of the following HTTP status codes is returned:\n\n- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)\n- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)\n- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)\n\nUse the `max_retries` request option to configure this behavior.\n\n```python\nfrom square.core.request_options import RequestOptions\n\nclient.payments.create(\n    ...,\n    request_options=RequestOptions(\n        max_retries=1\n    )\n)\n```\n\n### Timeouts\n\nThe SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.\n\n```python\n\nfrom square import Square\n\nclient = Square(\n    ...,\n    timeout=20.0,\n)\n\n# Override timeout for a specific method\nclient.payments.create(\n    ...,\n    request_options=RequestOptions(\n        timeout_in_seconds=20\n    )\n)\n```\n\n### Custom Client\n\nYou can override the `httpx` client to customize it for your use-case. Some common use-cases\ninclude support for proxies and transports.\n\n```python\nimport httpx\nfrom square import Square\n\nclient = Square(\n    ...,\n    httpx_client=httpx.Client(\n        proxies=\"http://my.test.proxy.example.com\",\n        transport=httpx.HTTPTransport(local_address=\"0.0.0.0\"),\n    ),\n)\n```\n\n## Contributing\n\nWhile we value open-source contributions to this SDK, this library is generated programmatically.\nAdditions made directly to this library would have to be moved over to our generation code,\notherwise they would be overwritten upon the next generated release. Feel free to open a PR as\na proof of concept, but know that we will not be able to merge it as-is. We suggest opening\nan issue first to discuss with us!\n\nOn the other hand, contributions to the README are always very welcome!\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": null,
    "version": "43.0.1.20250716",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "731d79c8c03445d7fd18e371a5ff7d14bd12e8a36ca8093b1317435ea09bf0b6",
                "md5": "5a0c5e78c474c41521a44c2778d470af",
                "sha256": "885f4bb5938e53ab5e49bace71d323455de97e598a0069d50454a48a21bdb20e"
            },
            "downloads": -1,
            "filename": "squareup-43.0.1.20250716-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a0c5e78c474c41521a44c2778d470af",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 2181107,
            "upload_time": "2025-07-16T20:25:37",
            "upload_time_iso_8601": "2025-07-16T20:25:37.911273Z",
            "url": "https://files.pythonhosted.org/packages/73/1d/79c8c03445d7fd18e371a5ff7d14bd12e8a36ca8093b1317435ea09bf0b6/squareup-43.0.1.20250716-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "79005a7aeabfbeb95c697e4944cd556d496e1e6863dbbd402ab1dc90b44f907f",
                "md5": "78d5419a21a105cf539c645069348876",
                "sha256": "5be2f37ed070623aaa3f9d5570d4ae2e6a75adc3200ee36c7a16cd2bd40d4a55"
            },
            "downloads": -1,
            "filename": "squareup-43.0.1.20250716.tar.gz",
            "has_sig": false,
            "md5_digest": "78d5419a21a105cf539c645069348876",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 722466,
            "upload_time": "2025-07-16T20:25:39",
            "upload_time_iso_8601": "2025-07-16T20:25:39.588176Z",
            "url": "https://files.pythonhosted.org/packages/79/00/5a7aeabfbeb95c697e4944cd556d496e1e6863dbbd402ab1dc90b44f907f/squareup-43.0.1.20250716.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-16 20:25:39",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "squareup"
}
        
Elapsed time: 0.51604s