Image Compress API
Re-encodes an image in its own format at a lower quality to shrink the file, keeping its dimensions.
- Endpoint
POST /v1/image-compress- Price
- $0.003 per call
- Family
- Images
01When to use it
- Compressing safely needs a real codec and a hardened fetch: an agent that pulls the URL itself walks past every SSRF control, and the output must be byte-reproducible, which an ad-hoc re-encode is not.
When not to use it
- The input is over 25 MB (26,214,400 bytes): image-compress answers too_large (413).
- The destination needs more than 60 s to answer: the call ends with upstream_timeout (424).
- The destination is on a private or local network: the network guard refuses it with blocked_target (403).
02Parameters
| Field | Type | Required | Description |
|---|---|---|---|
| url | string | yes | Absolute http(s) URL of a JPEG, PNG, WebP or GIF image, up to 2048 characters; redirects are followed and the output keeps the source format and dimensions. |
| quality | integer | no | Encoder quality from 1 to 100, lower gives a smaller file; drives JPEG and WebP re-compression and PNG palette quantisation, and is ignored for GIF. Default: 75. |
03Limits
| Limit | Value |
|---|---|
| Size cap | 25 MB (26,214,400 bytes) |
| Timeout | 60 s |
04Example
The published example of image-compress, verbatim: the request body and the response it returns.
POST /v1/image-compress
content-type: application/json
payment-signature: <base64 x402 payload>
{
"url": "https://grist.tools/samples/images/compress-scene.jpg",
"quality": 60
}200 OK
payment-response: <base64 settlement receipt>
{
"source_url": "https://grist.tools/samples/images/compress-scene.jpg",
"format": "jpeg",
"width": 96,
"height": 64,
"source_bytes": 1836,
"bytes": 758,
"saved_bytes": 1078,
"ratio": 0.4129,
"image_base64": "/9j/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZG..."
}Sample file: images/compress-scene.jpg. 96x64 JPEG at quality 95 of a soft gradient with two flat discs, built from raw pixels with the pinned JPEG encoder.
In the sample itself, the segments before the first scan run DQT, DQT, SOF0, DHT, DHT, DHT, DHT and SOS, and the baseline frame header declares 96 by 64 pixels, 8-bit precision and 3 components. In the returned file: the start-of-image marker is followed directly by a 67-byte quantisation table segment (FF DB), with no JFIF APP0 segment in between: table 0, 8-bit precision, first quantiser value 13.
| Response field | Type | In the example |
|---|---|---|
| source_url | string | "https://grist.tools/samples/images/compress-scene.jpg" |
| format | string | "jpeg" |
| width | number | 96 |
| height | number | 64 |
| source_bytes | number | 1836 |
| bytes | number | 758 |
| saved_bytes | number | 1078 |
| ratio | number | 0.4129 |
| image_base64 | string | a string of 67 characters |
05Call it from code
// image-compress: $0.003 per call, paid in USD Coin on eip155:8453 via x402
// Install: npm install @x402/fetch@2 @x402/evm@2 viem@2
// Save as client.mjs (ES module, Node 18+), export EVM_PRIVATE_KEY with the paying wallet's key in your shell, then run: node client.mjs
// The wrapper reads the 402 (payment-required), signs and retries with payment-signature.
import { wrapFetchWithPayment, x402Client, decodePaymentResponseHeader } from '@x402/fetch'
import { ExactEvmScheme } from '@x402/evm/exact/client'
import { privateKeyToAccount } from 'viem/accounts'
const account = privateKeyToAccount(process.env.EVM_PRIVATE_KEY)
const client = new x402Client().register('eip155:8453', new ExactEvmScheme(account))
const fetchWithPayment = wrapFetchWithPayment(fetch, client)
const body = {
"url": "https://grist.tools/samples/images/compress-scene.jpg",
"quality": 60
}
const res = await fetchWithPayment('https://grist.tools/v1/image-compress', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body),
})
console.log(res.status, await res.json())
const settle = res.headers.get('payment-response')
if (settle) console.log(decodePaymentResponseHeader(settle).transaction)# image-compress: $0.003 per call, paid in USD Coin on eip155:8453 via x402
# Install: pip install "x402[requests,evm]>=2.24,<3"
# The session reads the 402 (payment-required), signs and retries with payment-signature.
import os
from eth_account import Account
from x402 import x402ClientSync
from x402.http import decode_payment_response_header
from x402.http.clients.requests import x402_requests
from x402.mechanisms.evm.exact import register_exact_evm_client
from x402.mechanisms.evm.signers import EthAccountSigner
account = Account.from_key(os.environ["EVM_PRIVATE_KEY"])
client = x402ClientSync()
register_exact_evm_client(client, EthAccountSigner(account), networks="eip155:8453")
session = x402_requests(client)
payload = {
"url": "https://grist.tools/samples/images/compress-scene.jpg",
"quality": 60
}
res = session.post("https://grist.tools/v1/image-compress", json=payload)
print(res.status_code, res.json())
settle = res.headers.get("payment-response")
if settle:
print(decode_payment_response_header(settle).transaction)# image-compress: $0.003 per call, paid in USD Coin on eip155:8453 via x402
# 1. Unpaid call: HTTP 402, the requirements in the payment-required header (base64 JSON) and in the body.
curl -i -X POST 'https://grist.tools/v1/image-compress' -H 'content-type: application/json' -d '{"url":"https://grist.tools/samples/images/compress-scene.jpg","quality":60}'
# 2. Same call with the signed payment (an EIP-3009 authorization, EIP-712 signed: it cannot be
# typed by hand). x-payment is accepted as the v1 alternative. HTTP 200 carries payment-response.
curl -i -X POST 'https://grist.tools/v1/image-compress' -H 'content-type: application/json' -H 'payment-signature: <base64 x402 payload>' -d '{"url":"https://grist.tools/samples/images/compress-scene.jpg","quality":60}'06Errors
| Code | HTTP | For this service |
|---|---|---|
| invalid_input | 400 | The body does not match the schema; its fields are url and quality. |
| blocked_target | 403 | |
| unreachable_target | 424 | |
| upstream_timeout | 424 | |
| unsupported_content_type | 415 | |
| too_large | 413 | |
| unprocessable | 422 | |
| internal | 500 |
Input that is rejected before the tool runs (malformed JSON, schema mismatch, body over the size limit) is never charged.
When each code is raised, and what it means for payment: /docs/image-compress.
07Questions
- How much does a image-compress call cost?
- $0.003 per call, paid in USDC on Base via x402, with no account and no API key.
- What does image-compress return?
- A JSON object with 9 top-level fields: source_url, format, width, height, source_bytes, bytes, saved_bytes, ratio and image_base64. The example on this page is a real call over images/compress-scene.jpg.
- What happens when a image-compress call fails?
- The tool answers with a typed JSON error from the errors table. Payment is settled only after the tool has produced its result; a call that fails inside the tool is never settled. A 503 upstream_unavailable can follow a settled call; the x402 payments guide covers it, 429, 402 and an uncertain 500.