API reference · Optimisation

Compress image

POSThttps://api.imagemcpserver.com/playground/compress
💳 1 credit🛠️ MCP tool: compress_imageimagemcp.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.

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. Defaults to 70; values outside the range are clamped.
targetFormat"webp" | "jpeg" | "png"Output format. Defaults to "webp".

Examples

The same call in five forms. 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/playground/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%".
result.formatstringFormat actually written.
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%",
    "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.