Jump to content

Uploading Product Photo via API

Recommended Posts

Posted (edited)

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
Posted

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.

  • 1 year later...
  • 1 month later...
Posted

This worked for me as well

On 6/14/2023 at 9:23 PM, CKAKUMANU7 said:

What fixed it for me was NOT to add the below in the headers

 "Content-Type": "multipart/form-data;"

 

 

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.