Image compression API
Re-encode smaller in the same format. Put a quality from 1 to 100 in "option".
| Endpoint | POST https://astralbatch.com/api/v1/image-compress |
| Auth | Bearer token, free |
| Sends | input: base64 |
| Returns | result: base64 |
| Price | Free. 1,000 calls a day, 60 a minute. |
When to call it
Image weight is the usual reason a page is slow, and the usual reason it stays slow is that compressing by hand does not survive contact with a content team. Putting it in the upload path applies the same setting to every file, forever.
For a one-off, the browser version needs no token and never sends your data anywhere: it runs on your own machine.
Example
curl
curl -X POST https://astralbatch.com/api/v1/image-compress \
-H "Authorization: Bearer ast_YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"input": "/9j/4AAQSkZJRgABAQAA...", "option": "70"}'response
{
"tool": "image-compress",
"result": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoL...",
"chars": 41260,
"ms": 132
}In your language
JavaScript
const res = await fetch("https://astralbatch.com/api/v1/image-compress", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.ASTRAL_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({"input": "/9j/4AAQSkZJRgABAQAA...", "option": "70"}),
});
if (!res.ok) throw new Error((await res.json()).detail);
const { result } = await res.json();Python
import os, requests
res = requests.post(
"https://astralbatch.com/api/v1/image-compress",
headers={"Authorization": f"Bearer {os.environ['ASTRAL_TOKEN']}"},
json={"input": "/9j/4AAQSkZJRgABAQAA...", "option": "70"},
timeout=30,
)
res.raise_for_status()
result = res.json()["result"]Parameters
| Field | Type | Notes |
|---|---|---|
| input | string | Required. The file, base64 encoded. A data: URL is accepted too. Up to 10,000,000 characters per call. |
| option | string | See the example above. |
Errors
Errors are RFC 7807 problem documents with a stable type you can branch on.
| missing-token | 401 | No Authorization header. |
| bad-request | 400 | input missing or the wrong type. |
| tool-failed | 422 | The input could not be processed. detail says why. |
| quota-exceeded | 429 | Past 1,000 calls today. |
Questions
- What quality should I use?
- Between 70 and 80 is usually invisible on a photograph and saves a great deal. Below 50 shows on flat colours and gradients. Test with your own images rather than trusting a number.
- Does the format change?
- No. A JPEG comes back a JPEG. JPEG uses mozjpeg, PNG is quantised to a palette at maximum effort, and WebP and AVIF re-encode at the quality you asked for.
- Why did my PNG barely shrink?
- PNG is lossless, so there is no quality dial: what it can drop is palette size. A photograph stored as PNG is better converted to WebP or AVIF than compressed as PNG.
Other endpoints
A token takes thirty seconds
Free, no card, 1,000 calls a day, and the same token works on astraltext.com, astralpdf.com, astraljson.com and astralbatch.com.
Get a free token