Jump to content

Uploading Product Photo via API

Recommended Posts

Hi! I'm trying to programmatically upload photos to my products (https://developers.squarespace.com/commerce-apis/upload-product-image).

CURL Requests via Insomnia will work, but I cannot for the life of me get uploads via Python working. 

My code (generated from a working Insomnia request):

url = "https://api.squarespace.com/1.0/commerce/products/MY_PRODUCT_ID/images"
payload = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"; filename=\"MY_FILE_NAME.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n-----011000010111000001101001--\r\n"
headers = {
    "Authorization": "Bearer MY_TOKEN",
    "User-Agent": "MY_USER_AGENT",
    "Content-Type": "multipart/form-data; boundary=---011000010111000001101001"
}
response = requests.post(url, data=payload, headers=headers)

I keep hitting this error:

"The provided file could not be read as an image."

Has anyone done this before? What am I missing? If anyone has code examples from any language I'm happy to reverse engineer!

Edited by ksho
clarifying some words
Link to comment

Hi,

If you haven't still solved this yet, you can post on some FB groups
+ Squarespace Customization Resource Groups
+ Squarespace Community

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

How to: Setup Password & Share url Insert Custom CSS - Page Header - Upload Custom Font - Upload File - Find Block ID - Contact Customer Care

Link to comment

Hello @ksho,

Thanks for posting your question!

This example currently works for me, though I am not exactly sure the syntax in your example is correct:
 

import requests

url = "https://api.squarespace.com/1.0/commerce/products/<product id>/images"

files=[
  ('file',('<filename>',open('<path to image>','rb'),'image/jpeg'))
]
headers = {
  'Authorization': 'Bearer <api key>',
  'User-Agent': '<your user-agent>'
}

response = requests.request("POST", url, headers=headers, files=files)

print(response.text)

Also, check that your images meets the platform requirements

Let me know if this resolves the issue!

Build together: Together, we make up a community of Squarespace professionals. Lean on your fellow community members for support and welcome diverse perspectives. 
Work in progress: Trust that we are all working for the collective good of Circle members and their clients. 
Keep it light mode: Balance productivity with joyful creativity and curiosity.

Link to comment
  • 1 year later...
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.