API reference · Optimisation

Compress image — POST /v1/compress

Updated

POSThttps://api.imagemcpserver.com/v1/compress
💳 1 credit🛠️ MCP tool: compress_image⌘ imagemcp.js compress

Re-encodes an image at the quality and format you choose and returns the result as base64 along with before/after sizes. Two uses dominate: trimming assets before they ship to a browser, and shrinking images before they are pushed back into an agent's context window.

Overview

Shrink an image's file size with quality control, and report exactly how much was saved.

Flat 1 credit per call. No model is involved, so it is fast and cheap.

Pricing, examples and when to use it: Image compression API.

Request

Headers

FieldTypeDescription
AuthorizationrequiredstringBearer <IMAGEMCP_API_KEY>. The header x-api-key: <key> is accepted as an alternative.
Content-Typerequiredstringapplication/json

Body parameters

FieldTypeDescription
imagerequiredstringInput image as an https URL or a base64 data URI. imageBase64 is accepted as an alias for the same value.
qualitynumberEncoder quality 1–100 (default 70, clamped). For PNG it switches on lossy palette quantisation — and a quality is always applied — so PNG output here is never lossless.
targetFormat"webp" | "jpeg" | "png"Output format. Defaults to "webp"; jpg is accepted as jpeg. Any other value is encoded as WebP.

Examples

The same call in every form we support. The MCP tab is what an MCP client sends under the hood; the CLI tab is what an agent with the agent skill runs.

compress_image
curl -X POST "https://api.imagemcpserver.com/v1/compress" \
  -H "Authorization: Bearer $IMAGEMCP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "image": "https://example.com/large-photo.png",
  "quality": 70,
  "targetFormat": "webp"
}'

Response

Response fields

FieldTypeDescription
result.imagestringCompressed image as a base64 data URI.
result.originalSizestringInput size, e.g. "4302.4 KB".
result.compressedSizestringOutput size in the same units.
result.savedPercentagestringHow much was saved, e.g. "91.2%".
result.formatstringThe requested format, upper-cased. Over REST any value other than jpeg, jpg, png or webp is encoded as WebP but still labelled as requested — read the MIME prefix of result.image for the real type.
200 OK
{
  "success": true,
  "message": "Image compressed successfully",
  "deductedCredits": 1,
  "userCredits": 2418,
  "result": {
    "image": "data:image/webp;base64,UklGRl4…",
    "originalSize": "4302.4 KB",
    "compressedSize": "380.2 KB",
    "savedPercentage": "91.2%",
    "quality": "70%",
    "format": "WEBP",
    "latency": "0.42s",
    "cost": "$0.0000",
    "deductedCredits": 1,
    "userCredits": 2418
  }
}

When it fails

Errors return success: false with a message. See errors & troubleshooting for the status codes and whether a retry is worth it.

FAQ

Is the compressed image returned as a URL?

No — it comes back as a base64 data URI in result.image, together with the original and compressed sizes.

What quality should I use?

70 is the default and holds up well for web delivery. Drop toward 40–50 when you are shrinking images to fit an agent's context window and fine detail does not matter.