For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at http://ascentbusiness.co.uk/zoho-support-2.  For larger projects, check our bespoke pricing structure and receive dedicated support from our hands-on project consultants and developers at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at http://ascentbusiness.co.uk.

Zoho Creator: Public URL of an Image field / Upload to Shopify API

What?
This is an article to document how I downloaded an image held in a Zoho Creator form, and sent it to an API wanting the publicly accessible link or URL of the image.

Why?
I've got some other articles on handling images in Zoho Creator (see "Sources" below), the most relevant one being my article Zoho Deluge: Get Image Uploaded in Creator Form which has one method of getting a public link.

My use-case scenario here is that while I could upload the link from Creator to eBay's Picture Services successfully, Shopify would not accept any links I gave it. Why not use the links that eBay returns? Well eBay returns URLs to images that have been resized to 400x400 (even with tweak to return 800x800). I wanted a way to upload my image directly to Shopify in 3024x3024 resolution (or the size that the client wants: iPhone res). I then found that Shopify does accept a Base64Encoded version of the image and the below is how I achieved this.

How?
First-off, let me list the various formats that Zoho Creator says that public link exists. Let's say I have a field called Main_Photo and this is in a form called Portal_Listing with a report/view called Portal_Listing_Report and I have a publish key held in v_ListingPublishKey and lastly, my datacenter is .COM:

URL Syntax #1
copyraw
// works in eBay
v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "?filepath=/" + v_Filename;
  1.  // works in eBay 
  2.  v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "?filepath=/" + v_Filename; 

URL Syntax #2
copyraw
// works in eBay
v_ImageSrc = "https://creator.zohopublic.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename;
  1.  // works in eBay 
  2.  v_ImageSrc = "https://creator.zohopublic.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename; 

URL Syntax #3
copyraw
// Works in eBay
v_ImageSrc = "https://creatorexport.zoho.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename;
  1.  // Works in eBay 
  2.  v_ImageSrc = "https://creatorexport.zoho.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename; 

URL Syntax #4
The following won't work for the example below as it doesn't have the publish/public key, but it can be used internally:
copyraw
// DOESN'T WORK FOR ME!!!
v_ImageSrc = "https://creatorexport.zoho.com/DownloadFile.do?filepath=/"+v_Filename+"&sharedBy="+zoho.adminuser+"&appLinkName="+zoho.appname+"&viewLinkName=Portal_Listing_Report";
  1.  // DOESN'T WORK FOR ME!!! 
  2.  v_ImageSrc = "https://creatorexport.zoho.com/DownloadFile.do?filepath=/"+v_Filename+"&sharedBy="+zoho.adminuser+"&appLinkName="+zoho.appname+"&viewLinkName=Portal_Listing_Report"

URL Syntax #5 [image]: Update 2023
Again this example doesn't include the publish key but can be used across apps in Creator [note this is just for quick ref]. Where my form is called "My_Form" and the image field is "My_Photo" [also note: instead of zoho.appuri, we are hardcoding the appowner and appname as this is used across Creator apps, ie. images are stored in a separate app]:
copyraw
l_CompatiblePhotoExtensions = {"png","jpg","jpeg","gif","bmp","heic"};
c_Document = My_Form[ID == 1234567890];
//
// for image field type
v_PhotoFull = ifnull(c_Document.My_Photo,"");
v_PhotoWithExt = v_PhotoFull.getSuffix("/image/").getPrefix("\"");
v_PhotoFileExtension = v_PhotoWithExt.subString(v_PhotoWithExt.lastIndexOf(".") + 1).toLowerCase();
//
if(l_CompatiblePhotoExtensions.containsIgnoreCase(v_PhotoFileExtension))
{
	v_ThisPhotoSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Photo/image-download?filepath=/" + v_PhotoWithExt;
}
  1.  l_CompatiblePhotoExtensions = {"png","jpg","jpeg","gif","bmp","heic"}
  2.  c_Document = My_Form[ID == 1234567890]
  3.  // 
  4.  // for image field type 
  5.  v_PhotoFull = ifnull(c_Document.My_Photo,"")
  6.  v_PhotoWithExt = v_PhotoFull.getSuffix("/image/").getPrefix("\"")
  7.  v_PhotoFileExtension = v_PhotoWithExt.subString(v_PhotoWithExt.lastIndexOf(".") + 1).toLowerCase()
  8.  // 
  9.  if(l_CompatiblePhotoExtensions.containsIgnoreCase(v_PhotoFileExtension)) 
  10.  { 
  11.      v_ThisPhotoSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Photo/image-download?filepath=/" + v_PhotoWithExt; 
  12.  } 

URL Syntax #6 [file]: Update 2023
Same again but this is for a field of type document/file rather than image. Where my form is called "My_Form" and the file upload field is "My_File":
copyraw
c_Document = My_Form[ID == 1234567890];
//
// for file upload field type
v_DocFileName = ifnull(c_Document.My_File,"");
v_DocExtension = v_DocFileName.subString(v_DocFileName.lastIndexOf(".") + 1).toLowerCase();
v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download?filepath=/" + v_DocFileName;
//
// with public key
v_PublishKey = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download/" + v_PublishKey + "?filepath=/" + v_DocFileName;
//
// if My_File is in a subform
v_DocFileName = ifnull(row.My_File,"");
v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Subform.My_File/download?filepath=/" + v_DocFileName;
  1.  c_Document = My_Form[ID == 1234567890]
  2.  // 
  3.  // for file upload field type 
  4.  v_DocFileName = ifnull(c_Document.My_File,"")
  5.  v_DocExtension = v_DocFileName.subString(v_DocFileName.lastIndexOf(".") + 1).toLowerCase()
  6.  v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download?filepath=/" + v_DocFileName; 
  7.  // 
  8.  // with public key 
  9.  v_PublishKey = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  10.  v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download/" + v_PublishKey + "?filepath=/" + v_DocFileName; 
  11.  // 
  12.  // if My_File is in a subform 
  13.  v_DocFileName = ifnull(row.My_File,"")
  14.  v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Subform.My_File/download?filepath=/" + v_DocFileName; 

The API Solution
The following solution requires that your target API accepts base64encoded images or that you can send these in a cURL request (same thing really):
copyraw
string API.fn_ShopifyQuery_UploadPhoto(int p_Position, int p_ProductID)
{
	v_Output = "";
	m_Header = Map();
	m_Header.put("Content-Type","application/json");
	// 
	// your shopify details 
	v_ClientID = "<YOUR_CLIENT_ID>"; 
	v_ClientSecret = "<YOUR_CLIENT_SECRET>"; 
	v_ShopID = "example.myshopify.com"; 
	v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID; 
	v_ShopifyApiVersion = "2020-01"; 
	//
	// your creator details
	v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890"; 
	v_Position = ifnull(p_Position, 1);
	v_ShopifyProductID = ifnull(p_ProductID, 123456789012);
	r_PortalListing = Portal_Listing[Shopify_Product_ID == v_ShopifyProductID];
	if(r_PortalListing.count()>0)
	{
		v_Filename = r_PortalListing.Main_Photo.getSuffix("/image/").getPrefix("\"");
		v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
		v_ImageSrc = v_ImageSrc + r_PortalListing.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
		v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
		r_DownloadPhoto = invokeUrl
        [
        	url: v_ImageSrc
        	type: GET
        ];
		r_DownloadPhoto.setParamName("file");
		v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
		m_Params = Map();
		m_SubParams = Map();
		m_SubParams.put("position", 1);
		m_SubParams.put("attachment", v_EncodedImg);
		m_SubParams.put("filename", v_Filename);
		m_Params.put("image", m_SubParams);
		//
		v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json";
		r_UploadPhoto = invokeurl
		[
			url :v_Endpoint
			type :POST
			headers:m_Header
			parameters: m_Params.toString()
		];	
		v_Output = r_UploadPhoto;
		
	}
	return v_Output;
}
  1.  string API.fn_ShopifyQuery_UploadPhoto(int p_Position, int p_ProductID) 
  2.  { 
  3.      v_Output = ""
  4.      m_Header = Map()
  5.      m_Header.put("Content-Type","application/json")
  6.      // 
  7.      // your shopify details 
  8.      v_ClientID = "<YOUR_CLIENT_ID>"
  9.      v_ClientSecret = "<YOUR_CLIENT_SECRET>"
  10.      v_ShopID = "example.myshopify.com"
  11.      v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID; 
  12.      v_ShopifyApiVersion = "2020-01"
  13.      // 
  14.      // your creator details 
  15.      v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890"
  16.      v_Position = ifnull(p_Position, 1)
  17.      v_ShopifyProductID = ifnull(p_ProductID, 123456789012)
  18.      r_PortalListing = Portal_Listing[Shopify_Product_ID == v_ShopifyProductID]
  19.      if(r_PortalListing.count()>0) 
  20.      { 
  21.          v_Filename = r_PortalListing.Main_Photo.getSuffix("/image/").getPrefix("\"")
  22.          v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/"
  23.          v_ImageSrc = v_ImageSrc + r_PortalListing.ID + "/Main_Photo/image-download/" + v_ListingPublishKey; 
  24.          v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename; 
  25.          r_DownloadPhoto = invokeUrl 
  26.          [ 
  27.              url: v_ImageSrc 
  28.              type: GET 
  29.          ]
  30.          r_DownloadPhoto.setParamName("file")
  31.          v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto)
  32.          m_Params = Map()
  33.          m_SubParams = Map()
  34.          m_SubParams.put("position", 1)
  35.          m_SubParams.put("attachment", v_EncodedImg)
  36.          m_SubParams.put("filename", v_Filename)
  37.          m_Params.put("image", m_SubParams)
  38.          // 
  39.          v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json"
  40.          r_UploadPhoto = invokeUrl 
  41.          [ 
  42.              url :v_Endpoint 
  43.              type :POST 
  44.              headers:m_Header 
  45.              parameters: m_Params.toString() 
  46.          ]
  47.          v_Output = r_UploadPhoto; 
  48.   
  49.      } 
  50.      return v_Output; 
  51.  } 
Sends something like the following request (sent using Zoho Deluge but the cURL breakdown as follows):
copyraw
curl -d '{"image":{"position":1,"attachment":"/9/R0lGODlhbgCMAPf ... ... ... Q7PCAAOw==\n","filename":"my_test_photo.jpg"}}'
-X POST "https://your-development-store.myshopify.com/admin/api/2021-10/products/9876543210987/images.json" \
-H "X-Shopify-Access-Token: {access_token}"
  1.  curl -d '{"image":{"position":1,"attachment":"/9/R0lGODlhbgCMAPf ... ... ... Q7PCAAOw==\n","filename":"my_test_photo.jpg"}}' 
  2.  -X POST "https://your-development-store.myshopify.com/admin/api/2021-10/products/9876543210987/images.json" \ 
  3.  -H "X-Shopify-Access-Token: {access_token}" 
Yields something like the following response:
copyraw
{
  "image": {
    "position": 1,
    "width": 3024,
    "height": 3024,
    "alt": null,
    "id": 12345678901234,
    "product_id": 9876543210987,
    "created_at": "2021-10-17T21:45:56+01:00",
    "updated_at": "2021-10-17T21:45:56+01:00",
    "src": "https://cdn.shopify.com/s/files/1/2345/6789/1234/products/aaaabbbbccccddddeeeeffff11112222.jpg?v=1234567890",
    "variant_ids": [],
    "admin_graphql_api_id": "gid://shopify/ProductImage/23456789012345"
  }
}
  1.  { 
  2.    "image": { 
  3.      "position": 1, 
  4.      "width": 3024, 
  5.      "height": 3024, 
  6.      "alt": null, 
  7.      "id": 12345678901234, 
  8.      "product_id": 9876543210987, 
  9.      "created_at": "2021-10-17T21:45:56+01:00", 
  10.      "updated_at": "2021-10-17T21:45:56+01:00", 
  11.      "src": "https://cdn.shopify.com/s/files/1/2345/6789/1234/products/aaaabbbbccccddddeeeeffff11112222.jpg?v=1234567890", 
  12.      "variant_ids": [], 
  13.      "admin_graphql_api_id": "gid://shopify/ProductImage/23456789012345" 
  14.    } 
  15.  } 

This next version is a more generic form of the above function uploading the photo to Shopify:
copyraw
void API.fn_ShopifyQuery_UploadPhoto(int p_ProductID, int p_Position, string p_ImageSrc)
{
	m_Header = Map();
	m_Header.put("Content-Type","application/json");
	//
	// app specific
	v_ClientID = "<YOUR_CLIENT_ID>"; 
	v_ClientSecret = "<YOUR_CLIENT_SECRET>"; 
	v_ShopID = "example.myshopify.com"; 
	v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID; 
	v_ShopifyApiVersion = "2020-01"; 
	//
	// your creator details
	v_Position = ifnull(p_Position,1);
	v_ShopifyProductID = ifnull(p_ProductID,0);
	v_ImageSrc = ifnull(p_ImageSrc,"");
	if(v_ImageSrc != "" && v_ShopifyProductID != 0)
	{
		r_DownloadPhoto = invokeurl
		[
			url :v_ImageSrc
			type :GET
		];
		r_DownloadPhoto.setParamName("file");
		v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
		//
		// build shopify request
		m_Params = Map();
		m_SubParams = Map();
		m_SubParams.put("position",v_Position);
		m_SubParams.put("attachment",v_EncodedImg);
		m_Params.put("image",m_SubParams);
		//
		v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json";
		r_UploadPhoto = invokeurl
		[
			url :v_Endpoint
			type :POST
			parameters:m_Params.toString()
			headers:m_Header
		];
	}
}
  1.  void API.fn_ShopifyQuery_UploadPhoto(int p_ProductID, int p_Position, string p_ImageSrc) 
  2.  { 
  3.      m_Header = Map()
  4.      m_Header.put("Content-Type","application/json")
  5.      // 
  6.      // app specific 
  7.      v_ClientID = "<YOUR_CLIENT_ID>"
  8.      v_ClientSecret = "<YOUR_CLIENT_SECRET>"
  9.      v_ShopID = "example.myshopify.com"
  10.      v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID; 
  11.      v_ShopifyApiVersion = "2020-01"
  12.      // 
  13.      // your creator details 
  14.      v_Position = ifnull(p_Position,1)
  15.      v_ShopifyProductID = ifnull(p_ProductID,0)
  16.      v_ImageSrc = ifnull(p_ImageSrc,"")
  17.      if(v_ImageSrc != "" && v_ShopifyProductID != 0) 
  18.      { 
  19.          r_DownloadPhoto = invokeUrl 
  20.          [ 
  21.              url :v_ImageSrc 
  22.              type :GET 
  23.          ]
  24.          r_DownloadPhoto.setParamName("file")
  25.          v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto)
  26.          // 
  27.          // build shopify request 
  28.          m_Params = Map()
  29.          m_SubParams = Map()
  30.          m_SubParams.put("position",v_Position)
  31.          m_SubParams.put("attachment",v_EncodedImg)
  32.          m_Params.put("image",m_SubParams)
  33.          // 
  34.          v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json"
  35.          r_UploadPhoto = invokeUrl 
  36.          [ 
  37.              url :v_Endpoint 
  38.              type :POST 
  39.              parameters:m_Params.toString() 
  40.              headers:m_Header 
  41.          ]
  42.      } 
  43.  } 
And it's usage:
copyraw
// somewhere in the beginning
l_ShopifyPictures = List();
v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
if(!isnull(input.Main_Photo))
{
	v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"");
	v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
	v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
	v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
	l_ShopifyPictures.add(v_ImageSrc);
}
// after I've pushed the product to Shopify and it returned to me a Product ID:
if(v_ShopifyProductID != 0)
{
	// do photos
	if(input.Photos_Changed)
	{
		v_PositionIndex = 1;
		for each  v_ShopifyImageSrc in l_ShopifyPictures
		{
			if(v_ShopifyImageSrc!="")
			{
				thisapp.API.fn_ShopifyQuery_UploadPhoto(v_ShopifyProductID, v_PositionIndex, v_ShopifyImageSrc);
			}
			v_PositionIndex = v_PositionIndex + 1;
		}		
		info "Re-uploaded Photos to Shopify";
	}
}
  1.  // somewhere in the beginning 
  2.  l_ShopifyPictures = List()
  3.  v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890"
  4.  if(!isnull(input.Main_Photo)) 
  5.  { 
  6.      v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"")
  7.      v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/"
  8.      v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey; 
  9.      v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename; 
  10.      l_ShopifyPictures.add(v_ImageSrc)
  11.  } 
  12.  // after I've pushed the product to Shopify and it returned to me a Product ID: 
  13.  if(v_ShopifyProductID != 0) 
  14.  { 
  15.      // do photos 
  16.      if(input.Photos_Changed) 
  17.      { 
  18.          v_PositionIndex = 1
  19.          for each  v_ShopifyImageSrc in l_ShopifyPictures 
  20.          { 
  21.              if(v_ShopifyImageSrc!="") 
  22.              { 
  23.                  thisapp.API.fn_ShopifyQuery_UploadPhoto(v_ShopifyProductID, v_PositionIndex, v_ShopifyImageSrc)
  24.              } 
  25.              v_PositionIndex = v_PositionIndex + 1
  26.          } 
  27.          info "Re-uploaded Photos to Shopify"
  28.      } 
  29.  } 

Correction / Additional
The above solution means that for every photo uploaded, an API call is used up. If you have low limits, even 2000, this means you can only upload 100 products a day if they each have 20 photos (even less products then that as the upload of a product costs 1 API call). But I have found you can submit all photos in one go as well:
copyraw
// build up list of photos from Creator fields
l_CreatorPhotos = List();
v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
if(!isnull(input.Main_Photo))
{
	v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"");
	v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
	v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
	v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
	l_CreatorPhotos.add(v_ImageSrc);
}
//
// then upload
l_ShopifyUploadImages = List();
v_PositionIndex = 0;
for each  v_CreatorImageSrc in l_CreatorPhotos
{
    if(v_CreatorImageSrc != "")
    {
        v_PositionIndex = v_PositionIndex + 1;
        r_DownloadPhoto = invokeurl
        [
            url : v_CreatorImageSrc
            type : GET
        ];
        r_DownloadPhoto.setParamName("file");
        v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
        //
        // build shopify request
        m_Image = Map();
        m_SubParams = Map();
        m_SubParams.put("position",v_PositionIndex);
        m_SubParams.put("attachment",v_EncodedImg);
        m_Image.put("image",m_SubParams);
        l_ShopifyUploadImages.add(m_Image);
    }
}
//
v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyAPIVersion + "/products/" + v_ShopifyProductID + "/images.json";
r_UploadPhoto = invokeurl
[
    url : v_Endpoint
    type : POST
    parameters: l_ShopifyUploadImages.toString()
    headers: m_Header
];
  1.  // build up list of photos from Creator fields 
  2.  l_CreatorPhotos = List()
  3.  v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890"
  4.  if(!isnull(input.Main_Photo)) 
  5.  { 
  6.      v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"")
  7.      v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/"
  8.      v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey; 
  9.      v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename; 
  10.      l_CreatorPhotos.add(v_ImageSrc)
  11.  } 
  12.  // 
  13.  // then upload 
  14.  l_ShopifyUploadImages = List()
  15.  v_PositionIndex = 0
  16.  for each  v_CreatorImageSrc in l_CreatorPhotos 
  17.  { 
  18.      if(v_CreatorImageSrc != "") 
  19.      { 
  20.          v_PositionIndex = v_PositionIndex + 1
  21.          r_DownloadPhoto = invokeUrl 
  22.          [ 
  23.              url : v_CreatorImageSrc 
  24.              type : GET 
  25.          ]
  26.          r_DownloadPhoto.setParamName("file")
  27.          v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto)
  28.          // 
  29.          // build shopify request 
  30.          m_Image = Map()
  31.          m_SubParams = Map()
  32.          m_SubParams.put("position",v_PositionIndex)
  33.          m_SubParams.put("attachment",v_EncodedImg)
  34.          m_Image.put("image",m_SubParams)
  35.          l_ShopifyUploadImages.add(m_Image)
  36.      } 
  37.  } 
  38.  // 
  39.  v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyAPIVersion + "/products/" + v_ShopifyProductID + "/images.json"
  40.  r_UploadPhoto = invokeUrl 
  41.  [ 
  42.      url : v_Endpoint 
  43.      type : POST 
  44.      parameters: l_ShopifyUploadImages.toString() 
  45.      headers: m_Header 
  46.  ]

Correction / Additional version 2.0
Playing around with this for a client, I found an even better solution! When creating or updating a product, you can instead of giving it Image URLs, give it the Image as attachment a base64 encoded string. Note the above says that 10 photos produced 10 webhooks but actually I'd overlooked the webhook used to invokeUrl download a Creator file so it was using 20 webhooks; the following reduces that back down to 10 webhooks (1 to download each photo and base64 encode it but no extras to upload it in addition to the product update):
copyraw
// initialize
m_Product = Map();
b_CreateProduct = true;  
//
// I have a field called Shopify Product ID which stores the... um... shopify product id...
if(!isNull(input.Shopify_Product_ID))
{
    m_Product.put("id",input.Shopify_Product_ID);
    b_CreateProduct = false;
}
//
// title and HTML
m_Product.put("title",input.Listing_Title);
if(!isNull(input.Full_Shopify_HTML_Code))
{
    m_Product.put("body_html",input.Full_Shopify_HTML_Code);
}
m_Product.put("vendor",input.Brand);
m_Product.put("product_type",input.Item_Type);
//
// listings / channels
m_Product.put("published_scope","global");
m_Product.put("published",true);  // set to true when comfortable with it publishing correctly
m_Product.put("status",ifnull(input.Publish_Status.toLowerCase(),"draft"));
//
// build up variant list under this product
l_Variants = List();
m_Variant = Map();
m_Variant.put("weight",0.0);
m_Variant.put("title",input.Listing_Title);
m_Variant.put("sku",input.Product_SKU);
m_Variant.put("price",input.Shopify_Unit_Sell_Price);
m_Variant.put("taxable",input.Taxable);
m_Variant.put("inventory_quantity",input.Quantity.round(0));  // deprecated // use inventory_levels
m_Variant.put("inventory_policy","deny");
m_Variant.put("inventory_management","shopify");
m_Variant.put("inventoryItem",{"cost":ifnull(input.Shopify_Unit_Cost_Price,0).toDecimal().round(2).toString()}); // not working // use inventory_items
l_Variants.add(m_Variant);
m_Product.put("variants",l_Variants);
//
// do photos
if(input.Photos_Changed)
{
    l_AllImages = List();
    v_PositionIndex = 0;
    for each  v_ShopifyImageSrc in l_ShopifyPictures
    {
        if(v_ShopifyImageSrc != "")
        {
            v_PositionIndex = v_PositionIndex + 1;
            r_DownloadPhoto = invokeurl
            [
                url :v_ShopifyImageSrc
                type :GET
            ];
            r_DownloadPhoto.setParamName("file");
            v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
            //
            // build photos request
            m_Image = Map();
            m_Image.put("position",v_PositionIndex);
            m_Image.put("attachment",v_EncodedImg);
            l_AllImages.add(m_Image);
        }
    }
    m_Product.put("images",l_AllImages);
}
//
// shopify tags
l_Tags = input.Tags;
m_Product.put("tags",l_Tags);
//
// JSON product request
m_CreateRecord = Map();
m_CreateRecord.put("product",m_Product);
//
// send request to Shopify API
r_ShopifyConfig = API_Integration[Connection_Name == "Shopify API"];
if(r_ShopifyConfig.count() > 0)
{
    v_ShopifyAPIVersion = r_ShopifyConfig.API_Version.toString();
    v_ShopifyURL = "https://" + r_ShopifyConfig.Client_ID + ":" + r_ShopifyConfig.Client_Secret + "@" + r_ShopifyConfig.Shop_ID;
    //
    // if creating the product
    if(b_CreateProduct)
    {
        v_Endpoint = v_ShopifyURL + "/admin/api/" + r_ShopifyConfig.API_Version + "/products.json";
        r_NewProduct = invokeurl
        [
            url :v_Endpoint
            type :POST
            parameters:m_CreateRecord.toString()
            headers:m_Header
        ];
        r_Response = r_NewProduct.toMap();
        if(!isnull(r_Response.get("product").get("id")))
        {
            v_ShopifyProductID = r_Response.get("product").get("id").toLong();
            v_ShopifyHandle = r_Response.get("product").get("handle");
            // here I'm just getting the first variant in the list under this product
            v_ShopifyInventoryID = r_Response.get("product").get("variants").get(0).get("inventory_item_id").toLong();
            v_ShopifyVariantID = r_Response.get("product").get("variants").get(0).get("id").toLong();
        }
    }
}
  1.  // initialize 
  2.  m_Product = Map()
  3.  b_CreateProduct = true
  4.  // 
  5.  // I have a field called Shopify Product ID which stores the... um... shopify product id... 
  6.  if(!isNull(input.Shopify_Product_ID)) 
  7.  { 
  8.      m_Product.put("id",input.Shopify_Product_ID)
  9.      b_CreateProduct = false
  10.  } 
  11.  // 
  12.  // title and HTML 
  13.  m_Product.put("title",input.Listing_Title)
  14.  if(!isNull(input.Full_Shopify_HTML_Code)) 
  15.  { 
  16.      m_Product.put("body_html",input.Full_Shopify_HTML_Code)
  17.  } 
  18.  m_Product.put("vendor",input.Brand)
  19.  m_Product.put("product_type",input.Item_Type)
  20.  // 
  21.  // listings / channels 
  22.  m_Product.put("published_scope","global")
  23.  m_Product.put("published",true);  // set to true when comfortable with it publishing correctly 
  24.  m_Product.put("status",ifnull(input.Publish_Status.toLowerCase(),"draft"))
  25.  // 
  26.  // build up variant list under this product 
  27.  l_Variants = List()
  28.  m_Variant = Map()
  29.  m_Variant.put("weight",0.0)
  30.  m_Variant.put("title",input.Listing_Title)
  31.  m_Variant.put("sku",input.Product_SKU)
  32.  m_Variant.put("price",input.Shopify_Unit_Sell_Price)
  33.  m_Variant.put("taxable",input.Taxable)
  34.  m_Variant.put("inventory_quantity",input.Quantity.round(0));  // deprecated // use inventory_levels 
  35.  m_Variant.put("inventory_policy","deny")
  36.  m_Variant.put("inventory_management","shopify")
  37.  m_Variant.put("inventoryItem",{"cost":ifnull(input.Shopify_Unit_Cost_Price,0).toDecimal().round(2).toString()})// not working // use inventory_items 
  38.  l_Variants.add(m_Variant)
  39.  m_Product.put("variants",l_Variants)
  40.  // 
  41.  // do photos 
  42.  if(input.Photos_Changed) 
  43.  { 
  44.      l_AllImages = List()
  45.      v_PositionIndex = 0
  46.      for each  v_ShopifyImageSrc in l_ShopifyPictures 
  47.      { 
  48.          if(v_ShopifyImageSrc != "") 
  49.          { 
  50.              v_PositionIndex = v_PositionIndex + 1
  51.              r_DownloadPhoto = invokeUrl 
  52.              [ 
  53.                  url :v_ShopifyImageSrc 
  54.                  type :GET 
  55.              ]
  56.              r_DownloadPhoto.setParamName("file")
  57.              v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto)
  58.              // 
  59.              // build photos request 
  60.              m_Image = Map()
  61.              m_Image.put("position",v_PositionIndex)
  62.              m_Image.put("attachment",v_EncodedImg)
  63.              l_AllImages.add(m_Image)
  64.          } 
  65.      } 
  66.      m_Product.put("images",l_AllImages)
  67.  } 
  68.  // 
  69.  // shopify tags 
  70.  l_Tags = input.Tags; 
  71.  m_Product.put("tags",l_Tags)
  72.  // 
  73.  // JSON product request 
  74.  m_CreateRecord = Map()
  75.  m_CreateRecord.put("product",m_Product)
  76.  // 
  77.  // send request to Shopify API 
  78.  r_ShopifyConfig = API_Integration[Connection_Name == "Shopify API"]
  79.  if(r_ShopifyConfig.count() > 0) 
  80.  { 
  81.      v_ShopifyAPIVersion = r_ShopifyConfig.API_Version.toString()
  82.      v_ShopifyURL = "https://" + r_ShopifyConfig.Client_ID + ":" + r_ShopifyConfig.Client_Secret + "@" + r_ShopifyConfig.Shop_ID; 
  83.      // 
  84.      // if creating the product 
  85.      if(b_CreateProduct) 
  86.      { 
  87.          v_Endpoint = v_ShopifyURL + "/admin/api/" + r_ShopifyConfig.API_Version + "/products.json"
  88.          r_NewProduct = invokeUrl 
  89.          [ 
  90.              url :v_Endpoint 
  91.              type :POST 
  92.              parameters:m_CreateRecord.toString() 
  93.              headers:m_Header 
  94.          ]
  95.          r_Response = r_NewProduct.toMap()
  96.          if(!isnull(r_Response.get("product").get("id"))) 
  97.          { 
  98.              v_ShopifyProductID = r_Response.get("product").get("id").toLong()
  99.              v_ShopifyHandle = r_Response.get("product").get("handle")
  100.              // here I'm just getting the first variant in the list under this product 
  101.              v_ShopifyInventoryID = r_Response.get("product").get("variants").get(0).get("inventory_item_id").toLong()
  102.              v_ShopifyVariantID = r_Response.get("product").get("variants").get(0).get("id").toLong()
  103.          } 
  104.      } 
  105.  } 

Source(s)
Category: Zoho :: Article: 779

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.