Search code examples
pythonsquare

square api add image with python fails


When using the squareup python API to upload an image to the catalog it fails on the file parameter. I will post the code, the failure and then some observations regarding the API documentation.

pip install method #1

I actually used a requirements.txt, but this is effectivly how I installed the module:

$ pip install squareup

The code

# Import the os package
import os

# import the dotenv package
from dotenv import load_dotenv
# Get the current working directory
cwd = os.getcwd()

# Construct the .env file path
env_path = os.path.join(cwd, '.env')

# Load the .env file
load_dotenv(dotenv_path=env_path)

# Import square 
from square.client import Client

# init the square API
sq_client = Client(
    access_token=os.environ['SQUARE_ACCESS_TOKEN'],
    environment='production'  
)

# This works to verify api key and access id to upload to production environment. ie 
# not sandbox
result = sq_client.catalog.upsert_catalog_object(
  body = {
    "idempotency_key": "{UNIQUE_KEY}",
    "object": {
      "type": "ITEM",
      "id": "#coffee",
      "item_data": {
        "name": "Coffee",
        "description": "Coffee Drink",
        "abbreviation": "Co",
        "variations": [
          {
            "type": "ITEM_VARIATION",
            "id": "#small_coffee",
            "item_variation_data": {
              "item_id": "#coffee",
              "name": "Small",
              "pricing_type": "FIXED_PRICING",
              "price_money": {
                "amount": 300,
                "currency": "USD"
              }
            }
          },
          {
            "type": "ITEM_VARIATION",
            "id": "#large_coffee",
            "item_variation_data": {
              "item_id": "#coffee",
              "name": "Large",
              "pricing_type": "FIXED_PRICING",
              "price_money": {
                "amount": 350,
                "currency": "USD"
              }
            }
          }
        ]
      }
    }
  }
)

if result.is_success():
  print(result.body)
elif result.is_error():
  print(result.errors)

# afterwards, I could look in the square storefront and see the 
# sample item in the storefront catalog.

# As a further test of the API, here is getting info about the 
# api in the sample item.  This also works as expected.

result = sq_client.catalog.list_catalog()

if result.is_success():
  print(result.body)
elif result.is_error():
  print(result.errors)


# This fails though.
file_to_upload_path = "./sample_imgs/drdoom.jpeg" # Modify this to point to your desired file.
f_stream = open(file_to_upload_path, "rb")


result = sq_client.catalog.create_catalog_image(
  request = {
    "idempotency_key": "{UNIQUE_KEY}",
    "image": {
      "type": "IMAGE",
      "id": "#image_id",
      "image_data": {
        "name": "Image name",
        "caption": "Image caption"
      }
    }
  },
  file = f_stream
)

if result.is_success():
  print(result.body)
elif result.is_error():
  print(result.errors)

The last api call as noted in the comments fails. It generates a stack trace with failure on the file = f_stream parameter. Using the read operation on f_stream works, so its not that the image cannot be found, its the file parameter itself.

Other points of interest, I used the square api explorer webpage and did the exact same code and it worked there. The only difference was that the explorer generates a key rather than use {UNIQUE_KEY} mechanism. I also tried putting the image as a peer to the code so that I did not need to use a relative directory path. ie. no ./sample_imgs' prefix.

Thinkingn that the error might be related to the version of python library and the file parameter, I went back to the square docs to find how they specify to install the sdk. This page has a link on how to get the SDK package which says to visit PyPi. Note the link on PyPi specifies to use git. Consquently, I used this in my requirements.txt

requirements.txt for method #2

# square api install for method #1
#squareup
# square api install for method #2 using git clone
#square-python-sdk 
git+https://github.com/square/square-python-sdk.git

However, using the git version has the same error. Please advise.


Solution

  • square developer docs and api explorer has wrong parameter name in the example. But the weird thing is that in api explorer you will be able to run and get 200 when uploading, but wont make it work outside.

    So the right catalog.create_catalog_image method signature is shown at python sdk github: https://github.com/square/square-python-sdk/blob/master/doc/api/catalog.md

    to make the image upload work, change the parameter name file to image_file

    right signature is:

    def create_catalog_image(self,
    request=None,
    image_file=None)