Make pictures smaller in file size and choose the best settings
POST /v1/images/optimize
About this tool · Open it in the Studio
Guides
What it does
Makes the file size of your pictures smaller while they still look good. You can pick the file type, the quality, a target size, a maximum width or height, and whether to keep the hidden details such as camera and date. It gives you more control than automatic shrinking.
How to call it
Send a POST request to /v1/images/optimize with your API key in the X-Api-Key header. The parameters below go in the request body (or in the address of a GET request). Files, when the tool takes them, are sent as multipart form data. Every answer carries a Request-Id header: quote it if you contact support.
Parameters
| Parameter | Type | Default | What it does |
|---|---|---|---|
imagesRequired | files | — | Image file(s) to re-encode. |
formatOptional | string | — | Output format. "jpg" is accepted as an alias of jpeg. Omitted = keep the source format. jpegpngwebpavifgiftiff |
qualityOptional | integer | — | Encoder quality. 1 to 100 |
targetSizeKBOptional | integer | — | Try to reach this size (KB) via quality search (7 iterations over quality 20-90). min 1 |
colorSpaceOptional | string | srgb | Output color space. srgbgrayscalecmyk |
keepMetadataOptional | boolean | — | Keep EXIF/ICC metadata. |
progressiveOptional | boolean | — | Progressive encoding (jpeg/png). |
effortOptional | integer | — | Encoder effort. The valid range depends on the output format: webp 0-6, avif 0-9, gif 1-10, png 1-10. Ignored for jpeg/tiff. |
chromaSubsamplingOptional | string | — | JPEG chroma subsampling. 4:4:44:2:0 |
losslessOptional | boolean | — | Lossless encoding (webp/avif). |
output_bundleOptional | string | — | zip = return the result files of this request as ONE zip download (`bundle.url`) instead of one signed URL per file. It applies when the request produces two or more files (several input files, or an operation with several outputs); with a single result file the normal answer is returned. The files in `results` then carry `bundleEntry` (their place in the zip) instead of `url`. Not available together with `async: true`. zip |
validate_onlyOptional | boolean | false | true = check the request without running it. The answer is `{ validated: true, valid, errors[], files, estimate, limits }` instead of the endpoint's usual answer: `errors` lists every parameter or file the real call would reject, `estimate` is what it would count against your plan, and `limits` are the limits that apply to this endpoint for your key. Nothing is processed, no files are produced and no files are counted against the plan; the call itself counts as one request. The tool’s own checks run on it too, up to the point where processing would start; a problem that only shows while processing (a crop outside the image) is not found. Files given as a web address are not downloaded. |
presetIdOptional | string | — | Apply a saved preset (create one with POST /v1/presets). Every value stored in the preset is used unless this request sets that field itself. The preset must have been saved for this same endpoint; requires an individual customer API key. |
maxWidthOptional | integer | — | Shrink the picture to at most this width before encoding (never enlarges; the proportions are kept). 1 to 100000 |
maxHeightOptional | integer | — | Shrink the picture to at most this height before encoding (never enlarges; the proportions are kept). 1 to 100000 |
paletteOptional | boolean | — | PNG only: write an indexed-colour (palette) PNG. Much smaller for flat artwork; `quality` steers how carefully colours are matched. |
paletteColoursOptional | integer | — | PNG only: palette size (2-256). Implies palette=true. 2 to 256 |
bitdepthOptional | integer | — | PNG only: bits per channel: 1, 2 or 4 (needs, and implies, a palette), 8, or 16 (not with a palette). 1 to 16 |
animatedOptional | boolean | true | false = keep only the first frame of an animated GIF/WebP; true (default) keeps the animation where the output format can hold it. |
mozjpegOptional | boolean | true | JPEG only: false = use the standard libjpeg encoder instead of mozjpeg (faster, larger files). Default true. |
smartSubsampleOptional | boolean | — | WebP only: higher-quality chroma subsampling (sharper colour edges, slightly larger files). |
responseOptional | string | url | How the result comes back. url (default) = the usual JSON answer with a signed URL per file. binary = the file itself as the HTTP body with Content-Type and Content-Disposition (a request with several result files returns one zip). base64 = the JSON answer with each result carrying `data` (base64) and `mimeType` instead of `url`, up to 20 MB in total. Answers without a result file are returned as JSON either way. Not available together with `async: true`. urlbinarybase64 |
filenameOptional | string | — | The name for the result file (without extension; the extension follows the format). Used as the download name: Content-Disposition for response=binary, `downloadName` in the JSON answer otherwise. Path separators, control and bidirectional characters and Windows-reserved characters are removed; names are capped at 150 bytes. Several result files get -1, -2 ... added. |
filename_templateOptional | string | — | A name built per result file from placeholders: {name} (the input file name without extension), {index} (1, 2, ...), {date} (YYYY-MM-DD), {time} (HH-MM-SS, UTC), {format} (the result extension) and {operation}. Example: "{name}-{index}-{date}". Sanitised like filename; result files that would get the same name are numbered -2, -3 ... Not together with filename. |
appOptional | string | — | The name of the app that makes this call, for example "My shop importer". It is shown in your History next to the result, so you can tell which app did what. The official apps fill it in themselves (Davix H2I plugin, Davix H2I n8n node, Davix H2I Studio, Davix H2I Dashboard); when you leave it out, the official SDKs and plugins are recognised from their User-Agent. Up to 60 characters; control characters and angle brackets are removed. The header `X-Davix-App` does the same. It is only a label: it never changes what the call does. |
source_urlOptional | string | — | Fetch the input from this public http(s) address instead of uploading a file in "images". Send either the file or source_url, not both. Only public addresses on port 80/443 are fetched (never loopback, private, link-local or cloud-metadata addresses), redirects are re-checked, and the download is size- and time-limited; the fetched file is then judged exactly like an upload (content sniffing, format and size limits). |
source_urlsOptional | array | — | A list of public http(s) addresses to fetch as inputs, as a JSON array, for example ["https://example.com/a.pdf","https://example.com/b.pdf"]. They are added to any files you upload: uploaded files come first, then the URLs, in the order sent. Together they cannot exceed the files-per-request limit of your plan. Only public addresses on port 80/443 are fetched (never loopback, private, link-local or cloud-metadata addresses), redirects are re-checked, and the download is size- and time-limited; every fetched file is then judged exactly like an upload (content sniffing, format and size limits). |
Idempotency-KeyOptional | string | — | Optional idempotency key (8-128 characters). Reuse it for a retry of the SAME request; a different request with the same key -> 409 idempotency_key_conflict. |
asyncOptional | boolean | — | Opt-in asynchronous execution. With async=true and an individual customer API key the endpoint answers 202 {job_id, status_url} and the result becomes available from GET /v1/jobs/{id}. Omitted/false = the normal synchronous response. A request that carries several files in one field is not run as a job: it is processed immediately, answered with one result per file, and a `warnings` entry says that async was not applied. |
Use it from your code
cURL
curl -X POST "https://app.h2i.davixlabs.io/v1/images/optimize" \
-H "X-Api-Key: $DAVIX_H2I_API_KEY" \
-F 'images=@photo.jpg' \
-F 'format=jpeg' \
-F 'quality=1' \
-F 'targetSizeKB=1'Node.js
const { DavixH2I } = require('@davixlabs/h2i-sdk');
const h2i = new DavixH2I({ apiKey: process.env.DAVIX_H2I_API_KEY, baseUrl: process.env.DAVIX_H2I_URL });
const result = await h2i.images.optimize({
"images": 'photo.jpg',
"format": "jpeg",
"quality": 1,
"targetSizeKB": 1
});
console.log(result.data, result.requestId);PHP
<?php
require 'vendor/autoload.php';
$h2i = new \Davix\H2I\Client(['apiKey' => getenv('DAVIX_H2I_API_KEY'), 'baseUrl' => getenv('DAVIX_H2I_URL')]);
$result = $h2i->images()->optimize([
'images' => 'photo.jpg',
'format' => 'jpeg',
'quality' => 1,
'targetSizeKB' => 1,
]);
echo $result->requestId;