How to Validate JPG and PNG Images using Free APIs in Node.js

Cloudmersive
2 min readMay 23, 2024

--

JPG and PNG are the most common image formats across the internet. Like any file format, a lot can be wrong with the file internally.

Thankfully, using a couple of free APIs, we can easily validate JPG and PNG images in our Node.js applications. We can structure our API calls with complementary code examples provided below, and we can authorize both API calls using the same free API key (this will allow us to make a limit of 800 API calls per month with zero additional commitments).

Calling each API will return a detailed response object identifying any errors or warnings found within the image. This way, we can ensure JPG and PNG files rigorously conform with expected formatting standards before being rendered/processed by any libraries downstream from our pipeline.

First, let’s run the following NPM command to install the client SDK:

npm install cloudmersive-convert-api-client --save

If we’d prefer, we can also just add the Node client to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Next, we can use the below code examples to call our JPG and PNG validation functions respectively. Within each code block, we can replace the ‘YOUR API KEY’ string with our own API key string to authorize our requests.

We can use the below code to validate our JPG files:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentPngValidation(inputFile, callback);

And we can use the below code to validate our PNG files:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentJpgValidation(inputFile, callback);

That’s all there is to it! Now we have two low-code, easy-to-use validation solutions for JPG and PNG files in our Node.js applications.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet