Skip to content Skip to sidebar Skip to footer

Uploading Item Image Using Square Connect Api

I've reviewed the examples posted in the Square Connect API documentation and the examples on GitHub, however, I can't seem to adapt these examples to the guidance on uploading ima

Solution 1:

Thanks for pointing out this gap in the documentation. The function below uses the Requests Python library to upload an image for an item (this library makes multipart/form-data requests significantly simpler). Note that you'll need to install Requests first if you haven't.

import requests

defupload_item_image(item_id, image_path, access_token):

  endpoint_path = 'https://connect.squareup.com/v1/' + your location + '/items/' + item_id + '/image'# Don't include a Content-Type header, because the Requests library adds its own
  upload_request_headers = {'Authorization': 'Bearer ' + access_token,
                            'Accept': 'application/json'}

  # Be sure to set the correct MIME type for the image
  files = [('image_data', (image_path, open(image_path, 'rb'), "image/jpeg"))] 
  response = requests.post(endpoint_path, files=files, headers=upload_request_headers)

  # Print the response bodyprint response.text
  • item_id is the ID of the item you're uploading an image for.
  • image_path is the relative path to the image you're uploading.
  • access_token is the access token for the merchant you're acting on behalf of.

It isn't possible to upload images for multiple items in a single request to this endpoint. Instead, send a separate request for each item.

Post a Comment for "Uploading Item Image Using Square Connect Api"