Tutorial for NodeJS SDK

You can find the NodeJS SDK also at https://www.npmjs.com/package/@shimmercat/toilmore-sdk.

Prerequisites:

Installing

Step 1. Install


npm install --save-dev @shimmercat/toilmore-sdk

This will create a directory which looks similar to:

├── node_modules
│   └── @shimmercat
│       └── toilmore-sdk
│           ├── index.js
│           ├── lib
│           │   ├── api_config.js
│           │   ├── http_requester.js
│           │   ├── stream_helpers.js
│           │   └── submit_machine.js
│           ├── package.json
│           └── README.md
└── package-lock.json

4 directories, 8 files

Step 2. Ready to take it for a spin!

Depending on the preferred API (Light or Lux), create a .js file to process any image that you want. Just make sure to provide a valid authentication token as the api_token and its related domain as the ‘domain’.

  • Light API

If you plan to use the Light API, create a file e.g light.js, paste the code below changing the api_token, domain, and of course the path of the image you want to optimize.

$ cd node_modules/@shimmercat/toilmore-sdk/

import { LIGHT_API, Toilmore } from '@shimmercat/toilmore-sdk';
import fs from "fs";
import { Readable } from "stream";

let toilmore = new Toilmore(
    {
        // `LIGHT_API` contains our light api endpoint.
        'api_endpoint': LIGHT_API,

        // Use a valid API token below:
        'api_token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',

        // Use a valid domain below, as received when
        // you created the token
        'domain': 'YYYYYYYYYYYYYYYYYYYYYYYYYY'
    });

let result_promise =  toilmore.optimize("./my_image.jpg", "webp0");
result_promise.then((result) => {
    // `result` will be null when the optimization effort fails.
    if (result instanceof Readable) {
        let optimized_img_path = "./my_image.webp";
        let w = fs.createWriteStream(optimized_img_path);
        result.pipe(w);
        console.log("Successfully image optimized! Find it at: ", optimized_img_path);
    } else {
        console.log("The image could not be optimized due to: ", result.rejection_notice);
    }
});

  • Lux API

If you plan to use the Lux API, create a file e.g lux.js, paste the code below changing the api_token, domain, and of course the path of the image you want to optimize.


import { LUX_API, Toilmore } from '@shimmercat/toilmore-sdk';
import fs from "fs";
import { Readable } from "stream";

let adjustments = {
    "shifter": {
        "steps": [
            {
                "scale-to": {
                    "width": 90
                }
            }
        ]
    },
    "encoder": {
        "quality-measure": "fsim-c",
        "qual-threshold": 0.90
    }
}

let toilmore = new Toilmore(
    {
        'api_endpoint': LUX_API,

        'api_token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // <-- Use a valid API token here.

        'domain': 'YYYYYYYYYYYYYYYYYYYYYYYYYY' // <-- Use a registered domain here, e.g the one you received when you started the trial period.
    });


let result_promise =  toilmore.optimize(
    "./my_image.jpg",
    "webp0",
    adjustments
);
result_promise.then((result) => {
    // `result` will be null when the optimization effort fails.
    if (result instanceof Readable) {
        let optimized_img_path = "./my_image.webp";
        let w = fs.createWriteStream(optimized_img_path);
        result.pipe(w);
        console.log("Successfully image optimized! Find it at: ", optimized_img_path);
    } else {
        console.log("The image could not be optimized due to: ", result.rejection_notice);
    }
});

Before running the js-file, check the documentation for examples on how to configure it according to your needs, and then simply run it with:

$ node <file_name>.js 

The latest version of the SDK is published on the npm directory.

Video tutorial

The video tutorial below takes you through the quick process of setting up ShimmerCat Node SDK:

Want help to optimize your product images with ShimmerCat?

Please fill in the form and we will provide you a report. We know the tech team already has a lot of other things to focus on

Get Image Report