Node.js client for the Tinify API that intelligently compresses, resizes, converts, and stores images in AVIF, WebP, JPEG, and PNG formats.
—
Methods for retrieving processed images as files, buffers, or metadata, with both Promise and callback support.
Retrieve the processing result for accessing image data and metadata.
/**
* Get the processing result for the source
* @returns Result instance containing image data and metadata
*/
result(): Result;Usage Example:
const tinify = require("tinify");
tinify.key = "your-api-key";
const source = tinify.fromFile("image.jpg").resize({ method: "fit", width: 800 });
const result = source.result();
// Access metadata
const width = await result.width();
const height = await result.height();
const size = await result.size();
console.log(`Processed image: ${width}×${height}, ${size} bytes`);Save processed images directly to the filesystem with both Promise and callback support.
/**
* Save processed image to file (Promise version)
* @param path - Output file path
* @returns Promise that resolves when file is written
*/
toFile(path: string): Promise<void>;
/**
* Save processed image to file (callback version)
* @param path - Output file path
* @param callback - Callback function receiving error or null
*/
toFile(path: string, callback: (err: Error | null) => void): void;Usage Examples:
const tinify = require("tinify");
tinify.key = "your-api-key";
// Promise-based file output
try {
await tinify.fromFile("input.jpg")
.resize({ method: "cover", width: 400, height: 300 })
.toFile("output.jpg");
console.log("Image saved successfully");
} catch (error) {
console.error("Failed to save image:", error.message);
}
// Callback-based file output
tinify.fromFile("input.png")
.convert({ type: "image/webp" })
.toFile("output.webp", (err) => {
if (err) {
console.error("Failed to save image:", err.message);
} else {
console.log("Image saved successfully");
}
});
// From Source instance
const source = tinify.fromFile("large-image.png")
.resize({ method: "fit", width: 1200 });
await source.toFile("resized-image.png");
// From Result instance
const result = source.result();
await result.toFile("result-copy.png");Get processed image data as a Uint8Array buffer for in-memory operations.
/**
* Get processed image as buffer (Promise version)
* @returns Promise resolving to image data as Uint8Array
*/
toBuffer(): Promise<Uint8Array>;
/**
* Get processed image as buffer (callback version)
* @param callback - Callback function receiving error or image data
*/
toBuffer(callback: (err: Error | null, data?: Uint8Array) => void): void;Usage Examples:
const tinify = require("tinify");
import * as fs from "fs";
tinify.key = "your-api-key";
// Promise-based buffer output
const imageBuffer = await tinify.fromFile("input.jpg")
.resize({ method: "fit", width: 800 })
.toBuffer();
// Use buffer data
fs.writeFileSync("manual-output.jpg", imageBuffer);
// Callback-based buffer output
tinify.fromFile("input.png")
.convert({ type: "image/webp" })
.toBuffer((err, data) => {
if (err) {
console.error("Failed to get buffer:", err.message);
} else {
console.log(`Buffer size: ${data!.length} bytes`);
// Process buffer data...
}
});
// From Result instance
const source = tinify.fromFile("image.jpg");
const result = source.result();
const buffer = await result.toBuffer();Access processed image metadata including dimensions, file size, and format information.
/**
* Get image width in pixels
*/
width(): Promise<number>;
width(callback: (err: Error | null, width?: number) => void): void;
/**
* Get image height in pixels
*/
height(): Promise<number>;
height(callback: (err: Error | null, height?: number) => void): void;
/**
* Get image file size in bytes
*/
size(): Promise<number>;
size(callback: (err: Error | null, size?: number) => void): void;
/**
* Get image media type (MIME type)
*/
mediaType(): Promise<string | void>;
mediaType(callback: (err: Error | null, type?: string | void) => void): void;
/**
* Get image content type (alias for mediaType)
*/
contentType(): Promise<string | void>;
contentType(callback: (err: Error | null, type?: string | void) => void): void;
/**
* Get recommended file extension for the image format
*/
extension(): Promise<string | void>;
extension(callback: (err: Error | null, ext?: string | void) => void): void;Usage Examples:
const tinify = require("tinify");
tinify.key = "your-api-key";
const source = tinify.fromFile("image.jpg")
.resize({ method: "fit", width: 800 })
.convert({ type: ["image/webp", "image/jpg"] });
const result = source.result();
// Promise-based metadata access
const width = await result.width();
const height = await result.height();
const size = await result.size();
const mediaType = await result.mediaType();
const extension = await result.extension();
console.log({
dimensions: `${width}×${height}`,
fileSize: `${size} bytes`,
format: mediaType,
extension: extension
});
// Callback-based metadata access
result.width((err, width) => {
if (err) {
console.error("Failed to get width:", err.message);
} else {
console.log(`Image width: ${width} pixels`);
}
});
// Use extension for dynamic filename
const ext = await result.extension();
await source.toFile(`processed-image.${ext}`);For cloud storage operations, get the storage location URL.
/**
* Get storage location URL (for cloud storage results)
*/
location(): Promise<string>;
location(callback: (err: Error | null, location?: string) => void): void;Usage Example:
const tinify = require("tinify");
tinify.key = "your-api-key";
// Store to cloud and get location
const resultMeta = tinify.fromFile("image.jpg")
.resize({ method: "fit", width: 800 })
.store({
service: "s3",
aws_access_key_id: "ACCESS_KEY",
aws_secret_access_key: "SECRET_KEY",
region: "us-west-1",
path: "my-bucket/processed-images/image.jpg"
});
const location = await resultMeta.location();
console.log(`Image stored at: ${location}`);/**
* Base class for result metadata
*/
class ResultMeta {
width(): Promise<number>;
height(): Promise<number>;
location(): Promise<string>;
}
/**
* Full result class with image data
*/
class Result extends ResultMeta {
toFile(path: string): Promise<void>;
toBuffer(): Promise<Uint8Array>;
size(): Promise<number>;
mediaType(): Promise<string | void>;
contentType(): Promise<string | void>;
extension(): Promise<string | void>;
}Result and output methods can throw the following errors:
Error Handling Example:
const tinify = require("tinify");
tinify.key = "your-api-key";
try {
const source = tinify.fromFile("image.jpg");
const result = source.result();
const buffer = await result.toBuffer();
const metadata = {
width: await result.width(),
height: await result.height(),
size: await result.size(),
format: await result.mediaType()
};
console.log("Processing successful:", metadata);
} catch (error) {
if (error instanceof tinify.ClientError) {
console.error("Client error:", error.message);
} else if (error instanceof tinify.ConnectionError) {
console.error("Network error:", error.message);
}
}Install with Tessl CLI
npx tessl i tessl/npm-tinify