Skip to content

Optimize Image

Premium · 5 credits

Compress, resize, and convert images between PNG, JPEG, and WebP. Auto mode tries all formats and picks the smallest.

Upload an image as a multipart form with optional optimization settings.

FieldTypeRequiredDescription
filefileYesImage file to optimize (up to 10 MB)
optionsJSON stringNoOptimization options as a JSON-encoded string in the form field

Heads up: The options field is a single JSON string, not separate form fields per option. Serialize your options object to JSON and pass the whole thing as one form value. See the multipart example below.

The options JSON string accepts the same fields as the JSON route: output_format, quality, max_width, strip_metadata, and inline.


Send a base64-encoded image in a JSON body for when you already have the image in memory.

FieldTypeRequiredDefaultDescription
contentstringYesBase64-encoded image
output_formatstringNoautoauto, png, jpeg, webp
qualityintegerNo85Compression quality (1—100)
max_widthintegerNo0Max width in pixels. 0 = no resize. Aspect ratio preserved
strip_metadatabooleanNofalseRemove EXIF and other metadata
inlinebooleanNofalseReturn base64 content instead of a presigned URL

Both routes return the same shape.

FieldTypeDescription
urlstringPresigned download URL (when inline is false)
contentstringBase64-encoded optimized image (when inline is true)
expires_atstringURL expiry timestamp (when url is present)
original_sizeintegerOriginal file size in bytes
optimized_sizeintegerOptimized file size in bytes
reduction_percentnumberSize reduction as a percentage
output_formatstringFormat of the optimized image
widthintegerOutput width in pixels
heightintegerOutput height in pixels

When output_format is auto (the default), the service compresses your image as PNG, JPEG, and WebP, then returns whichever produces the smallest file. You get the best compression without guessing which format suits a given image.

By default, optimized images are uploaded to storage and you receive a presigned URL with an expiry timestamp. Set inline: true to skip storage and get the base64-encoded result directly in the response body — useful when you need the bytes immediately or storage is unavailable.


Terminal window
curl -X POST https://morso.dev/api/optimize-image \
-H "Authorization: Bearer YOUR_API_KEY" \
-F 'options={"output_format":"webp","quality":80}'
{
"url": "https://storage.morso.dev/optimized/a1b2c3d4.webp?token=xyz",
"expires_at": "2026-05-05T12:00:00Z",
"original_size": 524288,
"optimized_size": 104857,
"reduction_percent": 80.0,
"output_format": "webp",
"width": 1920,
"height": 1080
}
Terminal window
curl -X POST https://morso.dev/api/optimize-image/json \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"content": "iVBORw0KGgoAAAANSUhEUgAA...",
"output_format": "jpeg",
"quality": 75,
"inline": true
}'
{
"content": "/9j/4AAQSkZJRgABAQAAAQABAAD...",
"original_size": 245000,
"optimized_size": 61250,
"reduction_percent": 75.0,
"output_format": "jpeg",
"width": 1200,
"height": 800
}
Terminal window
curl -X POST https://morso.dev/api/optimize-image/json \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"content": "iVBORw0KGgoAAAANSUhEUgAA...",
"max_width": 800,
"strip_metadata": true
}'
{
"url": "https://storage.morso.dev/optimized/e5f6g7h8.webp?token=abc",
"expires_at": "2026-05-05T12:00:00Z",
"original_size": 1048576,
"optimized_size": 153600,
"reduction_percent": 85.4,
"output_format": "webp",
"width": 800,
"height": 533
}
  • 503 — Storage unavailable and inline not set. Workaround: use inline: true to bypass storage entirely.

See Errors for the standard error shape.