Sending images to imagekit.io using API

Sending images to imagekit.io using API

imagekit.io is a service for optimising media content for websites. It's API is pretty straightforward, but it's always a bit finicky to get a connection going the way you want it. 

To make an API call, first follow the steps in "Authorisation" and then add the code for the specific API endpoint that you want to use.

Authorisation

First, log in to your account and find your private key. It's located under Developer options:


Use the Private Key in your Deluge code as follows:
  1. username = "<your private key here>"+":";
  2. baseEncoded = zoho.encryption.base64Encode(username);
  3. header = {"Authorization":"Basic " + baseEncoded};
You can now use this header in your API calls as the header. 

Upload images

In this example, I have a form with an image field. The image uploaded into that field is the one I want to send to imagekit.io. One point of attention: this code uses the stored image location, which means you need to first save the record! I used a form button with a custom function to get this to work:
Step one is to create the download URL of the image. Find the report link name and the image field link name and insert them into the following line, and make sure that you use the correct zoho domain (eu, com, etc):
  1. imageURL = "https://creator.zoho.eu/api/v2"+zoho.appuri+"report/<your report link name here>/" + input.ID + "/<your image field name here>/download";
Then, we need to store the image in a variable, in this case "image", using a Zoho Creator API call. Make sure to create a connection first, with the ZohoCreator.report.READ scope. I called this connection report_read.
  1. image = invokeurl
  2. [
  3. url :imageURL
  4. type :GET
  5. connection:"report_read"
  6. ];
With this image now stored, we can call the API of imagekit.io to upload:
  1. URL = "https://upload.imagekit.io/api/v1/files/upload";
  2. parameters = Map();
  3. parameters.put("file",zoho.encryption.base64Encode(image));
  4. parameters.put("fileName",input.file_name);
  5. upload = invokeurl
  6. [
  7. url :URL
  8. type :POST
  9. parameters:parameters
  10. headers:header
  11. content-type:"multipart/form-data"
  12. ];

    • Related Articles

    • Limitations in CSS styling

      Styling forms in Zoho Creator using CSS styling in a Notes field brings many possibilities, but unfortunately not all CSS properties and functions work: some are simply stripped out. Whenever I come across a limitation, I add it to the list below, ...
    • Add a custom logo to an app

      Zoho Creator gives you a couple of options for the logo that is displayed in the top left corner of an app: Your company logo (the same for all your apps) An icon from the predefined icon list A 1-3 letter combination Now, you might have different ...
    • Create a European VAT ID checker (also works for Zoho CRM!)

      For invoicing to other businesses within the EU, it is mandatory to check whether the VAT ID of your customer is correct, in order to invoice using a 0% tax rate. I wrote a script that checks the VAT number that you have from your customer against ...