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:
- username = "<your private key here>"+":";
- baseEncoded = zoho.encryption.base64Encode(username);
- 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):
- 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.
- image = invokeurl
- [
- url :imageURL
- type :GET
- connection:"report_read"
- ];
With this image now stored, we can call the API of imagekit.io to upload:
- URL = "https://upload.imagekit.io/api/v1/files/upload";
- parameters = Map();
- parameters.put("file",zoho.encryption.base64Encode(image));
- parameters.put("fileName",input.file_name);
- upload = invokeurl
- [
- url :URL
- type :POST
- parameters:parameters
- headers:header
- content-type:"multipart/form-data"
- ];