Compress an image. POST exactly one of { url } or { imageBase64 }, plus optional { format?: auto|png|jpeg|webp|avif (default auto = keep input format), quality?: 1-100 (default 75), lossy?: bool (default true), effort?: 1-10 (default 6) }. Returns compressed image bytes directly with X-2s-* headers: Original-Bytes, Compressed-Bytes, Saved-Percent, Output-Format, Source-Format, Source-Width, Source-Height, Process-Ms. Limits: 5MB URL fetch, ~3MB inline body, 4096 × 4096 input pixels. Animated GIF input becomes animated WebP when format=webp or auto.
/api/image/compressPAYMENT-SIGNATURE.The compress API is a pay-per-call image endpoint built for AI agents and autonomous software. Compress an image.
There is no signup and no API key. An agent (or any HTTP client) hits the endpoint, receives an x402 "402 Payment Required" challenge, signs a sub-cent USDC payment on Base or Solana, and retries — the data comes back on the paid request. That makes it a drop-in compress data source for an agent tool-use loop, an MCP host, or a backend that needs image data on demand without onboarding to yet another vendor portal.
| Name | Type | Description |
|---|---|---|
url | string | max 2048 chars · uri |
imageBase64 | string | max 4400000 chars |
format | string | one of: auto | png | jpeg | webp | avif |
quality | integer | min 1 · max 100 |
lossy | boolean | |
effort | integer | min 1 · max 10 |
# 1. Probe the endpoint with no auth — receive 402 with PaymentRequirements
curl -sS -X POST 'https://2s.io/api/image/compress' \
-H 'Content-Type: application/json' \
-d '{"url":"https://example.com","imageBase64":"example","format":"auto","quality":1,"lossy":false,"effort":1}'
# 2. Sign the EIP-3009 transferWithAuthorization for the advertised price +
# payTo from the 402 envelope, then retry with PAYMENT-SIGNATURE:
curl -sS -X POST 'https://2s.io/api/image/compress' \
-H 'Content-Type: application/json' \
-H 'PAYMENT-SIGNATURE: <base64-json-payload>' \
-d '{"url":"https://example.com","imageBase64":"example","format":"auto","quality":1,"lossy":false,"effort":1}'
# Or just use the canonical runner — it handles the whole loop:
# EVM_PRIVATE_KEY=0x... node --env-file=.env.local \
# --experimental-strip-types scripts/x402-pay.ts \
# 'https://2s.io/api/image/compress'import { TwoS } from '@2sio/sdk'
const client = new TwoS({
privateKey: process.env.EVM_PRIVATE_KEY as `0x${string}`,
})
const result = await client.image.compress({
"url": "https://example.com",
"imageBase64": "example",
"format": "auto",
"quality": 1,
"lossy": false,
"effort": 1
})
console.log('endpoint:', result.endpoint)
console.log('cost:', result.costUsd, 'USDC')
console.log('tx:', result.settlement?.txHash)
console.log('data:', result.data)import os
from twosio import TwoS
client = TwoS(private_key=os.environ["EVM_PRIVATE_KEY"])
result = client.image.compress(url="https://example.com", imageBase64="example", format="auto", quality=1, lossy=False, effort=1)
print("endpoint:", result.endpoint)
print("cost:", result.cost_usd, "USDC")
print("tx:", (result.settlement or {}).get("tx_hash"))
print("data:", result.data)// 1. Add @2sio/mcp to your MCP host config (Claude Desktop example below).
// EVM_PRIVATE_KEY funds x402 payments per call.
// claude_desktop_config.json
{
"mcpServers": {
"2sio": {
"command": "npx",
"args": ["-y", "@2sio/mcp"],
"env": { "EVM_PRIVATE_KEY": "0x..." }
}
}
}
// 2. Once the server is running, agents call this tool via standard MCP:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "image.compress",
"arguments": {
"url": "https://example.com",
"imageBase64": "example",
"format": "auto",
"quality": 1,
"lossy": false,
"effort": 1
}
}
}