Add Image Python API

Hello,
I’m trying to add an image to my catalog to attach to an item using the python api. I get an error about the file parameter.

I am using the api docs and when I do using api explorer, the code works. However, when I do the same code in a python notebook it fails.

With that said, the python notebook work for adding a text item to the catalog and I can view the item in the catalog. So app ID and access token is valid. I am using production and not sandbox so that the item is visible in my storefront online store.

I am also confused regarding the square module to import via pip. One document from square says to pip install squareup. Another document says to visit pypy where it says to git clone the repo and then pip install the repo contents. Furthermore, it says to import squareup. Here is my code with comments.

# Import square 
from square.client import Client

# not shown in os imports and dotenv stuff

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

# this works. post truncated for clarity
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": [
# truncated
)

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


# verifying the object is created in the catalog (also actually looked in storefront site catalog and
# its there.)
result = sq_client.catalog.list_catalog()

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

# this works in api explorer but fails when done in the notebook
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 file f_stream is valid.  We can read the buffer in python
# however this is the stack trace complaining about the file parameter
# which lead me to believe my pip install was wrong.  However, using the git
# repo version rather than the pip install squareup did the same.

TypeError: CatalogApi.create_catalog_image() got an unexpected keyword argument 'file'

To gain a wider audience, I also posted a question on stackoverflow here

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)

1 Like

Thanks Sergei! I appreciate it! You rock the roll like a natural man!

We’re constantly working to improve our features based on feedback like this, so I’ll be sure to share your request to the API product team. :slightly_smiling_face: