Image Compression API

Try on PicWish

Compress images through the API by quality, target file size, or output dimensions. It supports image URLs and local file uploads for batch compression, ecommerce image optimization, and website asset optimization, with async and sync request modes.

The result image URL is valid for 1 hour. Please download and store it promptly.

Authentication

Every API request must include your API Key in the X-API-KEY request header. Send it with each request exactly as shown in the examples and parameter descriptions.

X-API-KEY: YOUR_API_KEY

Create an image compression task

POST /api/tasks/visual/imgcompress

Body Parameters

image_url string optional

Source image URL. If this parameter is present, the other image source parameter must be empty.

one required
image_file file optional

Source image file as binary data. If this parameter is present, the other image source parameter must be empty.

For image upload requirements, see Guidelines and Limits #7.

sync integer optional

Whether to wait for the result and return it immediately. Use 0 to return a task_id asynchronously, then fetch the result later with task_id. Use 1 to wait and return the result synchronously. Results are kept for up to 1 hour.

format string optional

Output format. Supports jpg, png, and webp. If the original format is jpg, png, or webp, the original format is used by default; otherwise the service recommends an output format.

return_type integer optional

Result return format. 1 returns an image URL; 2 returns a base64-encoded string. Default is 1.

quality integer optional

Compression quality. Range 1-100. If this parameter is present, kbyte must be empty.

one optional
kbyte integer optional

Target compressed image size in KB. Range 0-51200. If this parameter is present, quality must be empty. PNG output does not support target-size compression.

width integer optional

Output image width. Range 0-8000. If height is empty, the height is calculated proportionally.

height integer optional

Output image height. Range 0-8000. If width is empty, the width is calculated proportionally.

Return Parameters

status number

HTTP response status code. 200 means success, and non-200 means failure. See Status Code Definitions.

message string

API response message. Usually success for a successful request.

data.task_id string

Async image compression task ID. Returned after the task is created, and used to query the compression result later.

status number

HTTP response status code. 200 means success, and non-200 means failure. See Status Code Definitions.

message string

API response message. Usually success for a successful request.

data.task_id string

Image compression task ID.

data.created_at string

Timestamp of task creation.

data.processed_at string

Timestamp when the task started to be processed.

data.completed_at string

Timestamp of task completion.

data.image string

Compressed result image URL or base64 data. The URL is valid for 1 hour.

data.progress number

Task progress. 100 means processing is complete.

data.state number

Task status code. 1 means succeeded, greater than 1 means processing, and less than 0 means failed. -7 means invalid image file. See Status Code Definitions.

Query image compression result

GET /api/tasks/visual/imgcompress/{task_id}

Path Parameters

task_id string required

Image compression task ID returned after creating an async task. Use it to query the processing result.

Return Parameters

status number

HTTP response status code. 200 means success, and non-200 means failure. See Status Code Definitions.

message string

API response message. Usually success for a successful request.

data.task_id string

Image compression task ID. If the task fails, contact support with this task_id.

data.created_at string

Timestamp of task creation.

data.processed_at string

Timestamp when the task started to be processed.

data.completed_at string

Timestamp of task completion.

data.image string

Compressed result image URL or base64 data. The URL is valid for 1 hour.

data.progress number

Task progress. 100 means processing is complete.

data.state number

Task status code. 1 means succeeded, greater than 1 means processing, and less than 0 means failed. -7 means invalid image file. See Status Code Definitions.

Guidelines and Limits

  1. The result image URL is valid for 1 hour. Please download and store it promptly.

  2. HTTP status 200 indicates that the HTTP request succeeded, not that image compression succeeded. Determine the task result with data.state. See Status Code Definitions.

  3. When passing URLs as parameters, follow URL encoding standards to prevent parameter parsing confusion.

  4. Use either image_url or image_file. If one image source parameter is present, the other image source parameter must be empty.

  5. quality and kbyte cannot be sent together. If kbyte is used to specify a target size, PNG output does not support target-size compression.

  6. Each successful request consumes 1 credit when the longest side is up to 4096px, or 2 credits when the longest side is greater than 4096px and up to 8000px.

  7. Uploaded images must meet the following format, resolution, and file size limits.

    FormatResolutionSize
    jpg, jpeg, bmp, png, webp, tiff, bitmap, gif, lzw, raw, rgbUp to 8000 x 8000Up to 20MB