A quick article on how to upload an image to the product/item listing in Zoho Inventory.
Why?
We're enhancing the out-of-the-box integration from eBay to Zoho Inventory bespoke for a customer. We want the photo on eBay to be downloaded and uploaded to the Zoho Inventory record for the respective record. As Zoho Books and Zoho Subscriptions uses the same database tables as Zoho Inventory, and there's more documentation on the API for Zoho Books, we're going to apply this solution to Zoho Books.
How?
We're not going to go into detail on how to parse an eBay response here (see my other articles on eBay webhooks). Instead, all we want is the URL of an image to be downloaded, then uploaded to the Zoho Inventory or Zoho Books item record.
Well I don't want to research this often so here's the quick parsing instruction to get the eBay photos; this snippet stores the URL of each picture in a list variable:
l_Pictures = x_MainNode.executeXPath("//Item/PictureDetails/PictureURL/text()").toXmlList(); // yields // https://i.ebayimg.com/00/s/SAMPLE/z/ELPMAS/$_57.JPG?set_id=1234567890,https://i.ebayimg.com/00/s/SAMPLE/z/ELPMAS/$_57.JPG?set_id=1234567891
- l_Pictures = x_MainNode.executeXPath("//Item/PictureDetails/PictureURL/text()").toXmlList();
- // yields
- // https://i.ebayimg.com/00/s/SAMPLE/z/ELPMAS/$_57.JPG?set_id=1234567890,https://i.ebayimg.com/00/s/SAMPLE/z/ELPMAS/$_57.JPG?set_id=1234567891
// // declare ID of item in Zoho Inventory v_InventoryId = 123456789012345678; // // your organization ID (optional) v_BooksOrgId = 20220621; // // download photo from eBay r_DownloadedPhoto = invokeurl [ url :v_EbayPictureUrl type :GET ]; // // set the data type r_DownloadedPhoto.setParamName("image"); // // build up request to Zoho m_Params = Map(); m_Params.put("image",r_DownloadedPhoto); // // generate endpoint v_Url = "https://books.zoho.eu/api/v3/items/" + v_InventoryId + "/image?organization_id=" + v_BooksOrgId; // // updload the photo r_UploadPhoto = invokeurl [ url :v_Url type :POST files:r_DownloadedPhoto connection:"joel_books" ]; // output response to console info "Response of attempt to upload to Zoho Inventory:"; info r_UploadPhoto;
- //
- // declare ID of item in Zoho Inventory
- v_InventoryId = 123456789012345678;
- //
- // your organization ID (optional)
- v_BooksOrgId = 20220621;
- //
- // download photo from eBay
- r_DownloadedPhoto = invokeUrl
- [
- url :v_EbayPictureUrl
- type :GET
- ];
- //
- // set the data type
- r_DownloadedPhoto.setParamName("image");
- //
- // build up request to Zoho
- m_Params = Map();
- m_Params.put("image",r_DownloadedPhoto);
- //
- // generate endpoint
- v_Url = "https://books.zoho.eu/api/v3/items/" + v_InventoryId + "/image?organization_id=" + v_BooksOrgId;
- //
- // updload the photo
- r_UploadPhoto = invokeUrl
- [
- url :v_Url
- type :POST
- files:r_DownloadedPhoto
- connection:"joel_books"
- ];
- // output response to console
- info "Response of attempt to upload to Zoho Inventory:";
- info r_UploadPhoto;
{"code":0,"message":"We've updated the item image. Nice one, by the way!"}
- {"code":0,"message":"We've updated the item image. Nice one, by the way!"}
Update 2024: Following Books Domain Upgrade
// // ZohoBooks Organization ID v_BooksOrgId = 12345678; // // Zoho Books Item ID v_ZB_ItemID = 123456000000789012; // // download photo v_ImageSrc = "https://picsum.photos/200"; r_DownloadedPhoto = invokeurl [ url :v_ImageSrc type :GET ]; // // set the data type r_DownloadedPhoto.setParamName("image"); // // generate endpoint v_Url = "https://www.zohoapis.com/books/v3/items/" + v_ZB_ItemID + "/images?organization_id=" + v_BooksOrgId; // // updload the photo r_UploadPhoto = invokeurl [ url :v_Url type :POST files:r_DownloadedPhoto connection:"my_books_connection" ]; // // output for demo purposes info r_UploadPhoto;
- //
- // ZohoBooks Organization ID
- v_BooksOrgId = 12345678;
- //
- // Zoho Books Item ID
- v_ZB_ItemID = 123456000000789012;
- //
- // download photo
- v_ImageSrc = "https://picsum.photos/200";
- r_DownloadedPhoto = invokeUrl
- [
- url :v_ImageSrc
- type :GET
- ];
- //
- // set the data type
- r_DownloadedPhoto.setParamName("image");
- //
- // generate endpoint
- v_Url = "https://www.zohoapis.com/books/v3/items/" + v_ZB_ItemID + "/images?organization_id=" + v_BooksOrgId;
- //
- // updload the photo
- r_UploadPhoto = invokeUrl
- [
- url :v_Url
- type :POST
- files:r_DownloadedPhoto
- connection:"my_books_connection"
- ];
- //
- // output for demo purposes
- info r_UploadPhoto;
// // ZohoBooks Organization ID v_BooksOrgId = 12345678; // // Zoho Books Item ID v_ZB_ItemID = 123456000000789012; // // download photo v_ImageSrc = "https://picsum.photos/200"; r_DownloadedPhoto = invokeurl [ url :v_ImageSrc type :GET ]; v_DownloadedBase64 = zoho.encryption.base64Encode(r_DownloadedPhoto); f_base64 = zoho.encryption.base64DecodeToFile(v_DownloadedBase64,"my_image.png"); // // set the data type f_base64.setParamName("image"); // // generate endpoint v_Url = "https://www.zohoapis.com/books/v3/items/" + v_ZB_ItemID + "/images?organization_id=" + v_BooksOrgId; // // updload the photo r_UploadPhoto = invokeurl [ url :v_Url type :POST files:f_base64 connection:"my_books_connection" ];
- //
- // ZohoBooks Organization ID
- v_BooksOrgId = 12345678;
- //
- // Zoho Books Item ID
- v_ZB_ItemID = 123456000000789012;
- //
- // download photo
- v_ImageSrc = "https://picsum.photos/200";
- r_DownloadedPhoto = invokeUrl
- [
- url :v_ImageSrc
- type :GET
- ];
- v_DownloadedBase64 = zoho.encryption.base64Encode(r_DownloadedPhoto);
- f_base64 = zoho.encryption.base64DecodeToFile(v_DownloadedBase64,"my_image.png");
- //
- // set the data type
- f_base64.setParamName("image");
- //
- // generate endpoint
- v_Url = "https://www.zohoapis.com/books/v3/items/" + v_ZB_ItemID + "/images?organization_id=" + v_BooksOrgId;
- //
- // updload the photo
- r_UploadPhoto = invokeUrl
- [
- url :v_Url
- type :POST
- files:f_base64
- connection:"my_books_connection"
- ];
Caveat(s)
- The code above only uploads 1 photo... Update 2024: the code above STILL only uploads 1 photo.
Error(s) Encountered
- {"code":37,"message":"The HTTP method POST is not allowed for the requested resource"} The endpoint is wrong (for example you don't need the organization ID parameter at the end of the URL)
- {"code":2,"message":"The request passed is not valid."} The .setParamName() function is setting the data type to probably file or attachment when it should be image.