Rolando Andrade
Back to Content

Experimenting with the IMG Processing SDK

A Node.js tutorial for the IMG Processing SDK: install it with npm, create the client with your API key, upload an image, convert it to PNG and download it.

· 2 min read
Experimenting with the IMG Processing SDK

In the last article, we saw how to get started with the IMG Processing API over HTTP. IMG Processing is an API and a Node.js SDK for editing and analyzing images. The HTTP API works, but you still write every request yourself. The SDK removes that work.

Presenting IMG Processing Presenting IMG Processing - In a world of digital media, it is increasingly common to transmit information through images instead of words. rolandoandrade.me

Today, I want to show you how to use the node-sdk.

img-processing-sdk img-processing-sdk - SDK for image processing. Latest version: 1.0.0. Start using img-processing-sdk in your project by running `npm i img-processing-sdk`. www.npmjs.com

Install and connect

A Software Development Kit (SDK) is a ready-made library that makes the API easy to use from Node.js projects. You only need your API key. The SDK handles the hard parts for you, like sending HTTP requests, reading responses and parsing data.

Enough talking. Let’s see it in action. First, install the SDK.

npm install img-processing-sdk

The library has 3 main classes: IMGProcessingClient, ImageObject and IMGProcessingAPIError. First, let’s create the client with our API key:

import { IMGProcessingClient } from 'img-processing-sdk';

const client = new IMGProcessingClient({
    apiKey: process.env.IMG_PROCESSING_API_KEY,
});

Now we have an IMGProcessingClient object, the main class of the SDK. It wraps the HTTP requests to the IMG Processing API, so you call simple methods instead. You can see all its methods in the documentation:

IMGProcessingClient - IMG Processing Simple and easy-to-use way to interact with the IMG Processing API docs.img-processing.com

Upload and convert

With the client ready, you can call the API with the same methods as the specification. For example, let’s upload an image from our file system:

const image = await client.uploadImage({
    image: 'path/to/image.jpg',
    name: 'image.jpg',
});

After the upload, the method returns an ImageObject. It represents an image stored in the IMG Processing API. The object holds information about the image, like its URL, size and format:

{
    "id": "image_b3wfdnk9gjx442a6deayfh17",
    "name": "image.jpg",
    "url": null,
    "width": 700,
    "height": 320,
    "format": "jpeg",
    "size": 27696,
    "created_at": "2024-09-22T16:00:29.700Z"
}

Besides these read-only properties, the object has many methods. You can review them in the documentation:

ImageObject - IMG Processing Base class for all image objects docs.img-processing.com

For example, let’s convert the image to PNG:

    const pngImage = await image.convert({
      format: "png",
    });

Now we have the converted PNG image:

{
    "id": "image_jwlx4c3p2djgrdrwrja23i74",
    "name": "image.jpg",
    "url": null,
    "width": 700,
    "height": 320,
    "format": "png",
    "size": 62504,
    "created_at": "2024-09-22T16:04:14.078Z"
}

That is it. We converted the image from JPEG to PNG in one call. Notice the new id, the png format and the new size, 62504 bytes instead of 27696. If you want to keep the result, download the image and save it to the filesystem:

const blob = await pngImage.download();

Help me find bugs

IMG Processing is still a release candidate, a version that is almost final. There may be some random errors. I am still looking for them with a lot of automated testing.

If you find an error, create an issue in the GitHub repository:

GitHub - img-processing/node-sdk Contribute to img-processing/node-sdk development by creating an account on GitHub. github.com

In the next posts, I will tell you more about these changes. I will also share the wins and the problems I find along the way. In the meantime, keep experimenting and creating amazing things!


If you enjoy the content, please don’t hesitate to subscribe and leave a comment! I would love to connect with you and hear your thoughts on the topics I cover.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build