TinyJPG API Documentation
Integrate our powerful image compression technology directly into your applications with a simple REST API.
Get Your API KeyContents
Client Libraries
Overview
The TinyJPG API allows you to integrate our powerful image compression technology directly into your applications. This REST API provides both synchronous and asynchronous compression modes for maximum flexibility.
Fast Processing
Optimize images in milliseconds with our high-performance API
High Compression
Reduce file sizes by up to 80% while maintaining quality
Simple Integration
Easy to integrate with any platform or programming language
Authentication
All API requests require authentication using your API key. You can find your API key in the dashboard after signing up for a paid plan.
AUTHENTICATION HEADER
Authorization: Bearer YOUR_API_KEY
Replace YOUR_API_KEY
with your actual API key.
Compress Image Endpoint
ENDPOINT
REQUEST PARAMETERS
Parameter | Type | Required | Description |
---|---|---|---|
file |
File | Required | The image file to compress (JPEG or PNG format) |
quality |
Integer | Optional | Compression quality from 1-100 (default: 75) |
callback_url |
String | Optional | URL to notify when asynchronous compression is complete |
metadata |
String | Optional | Preserve metadata: 'none', 'copyright', 'all' (default: 'none') |
EXAMPLE REQUEST
curl -X POST \
https://api.tinyjpg.com/v1/compress \
-H 'Authorization: Bearer YOUR_API_KEY' \
-F 'file=@/path/to/your/image.jpg' \
-F 'quality=80'
EXAMPLE RESPONSE
{
"status": "success",
"original_size": 1253420,
"compressed_size": 315240,
"savings_percent": 74.8,
"url": "https://api.tinyjpg.com/v1/output/abc123def456.jpg"
}
Asynchronous Processing
For large files or batch processing, use the asynchronous mode by providing a callback_url
. The API will return immediately and notify your server when processing is complete.
// Example callback POST body
{
"status": "success",
"original_size": 2487654,
"compressed_size": 498765,
"savings_percent": 80.2,
"url": "https://api.tinyjpg.com/v1/output/def789ghi012.jpg"
}
Error Handling
The API returns standard HTTP status codes and a JSON response body with error details:
{
"status": "error",
"code": "invalid_api_key",
"message": "The provided API key is invalid or expired."
}
Status Code | Error Code | Description |
---|---|---|
400 | invalid_parameters |
Missing required parameters or invalid format |
401 | invalid_api_key |
Invalid or expired API key |
403 | quota_exceeded |
Monthly API request quota exceeded |
415 | unsupported_file_type |
File is not a supported image format |
422 | file_too_large |
File exceeds the maximum allowed size for your plan |
429 | rate_limit_exceeded |
Too many requests in a short period of time |
500 | server_error |
Server encountered an error processing request |
Rate Limits
To ensure service stability and fair usage, rate limits apply based on your subscription plan:
Plan | Request Limit | File Size Limit |
---|---|---|
Developer | 100 requests/minute | 5MB |
Business | 300 requests/minute | 10MB |
Enterprise | 1000 requests/minute | 25MB |
Rate limit information is included in the response headers:
X-RateLimit-Limit
: Maximum requests per minuteX-RateLimit-Remaining
: Remaining requests in the current windowX-RateLimit-Reset
: Seconds until the rate limit resets
Need Higher Limits?
Enterprise customers can request custom rate limits tailored to their specific needs.
Contact SalesCode Examples
// Using fetch API
const apiKey = 'YOUR_API_KEY';
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('quality', 80);
fetch('https://api.tinyjpg.com/v1/compress', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`
},
body: formData
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
document.getElementById('result').src = data.url;
})
.catch(error => {
console.error('Error:', error);
});
// Using cURL with PHP
$apiKey = 'YOUR_API_KEY';
$filePath = '/path/to/your/image.jpg';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.tinyjpg.com/v1/compress",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . $apiKey
],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile($filePath),
'quality' => 80
]
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "Error: " . $err;
} else {
$result = json_decode($response, true);
echo "Success! Compressed image URL: " . $result['url'];
}
# Using requests library in Python
import requests
api_key = 'YOUR_API_KEY'
file_path = '/path/to/your/image.jpg'
headers = {
'Authorization': f'Bearer {api_key}'
}
files = {
'file': open(file_path, 'rb')
}
data = {
'quality': 80
}
response = requests.post(
'https://api.tinyjpg.com/v1/compress',
headers=headers,
files=files,
data=data
)
if response.status_code == 200:
result = response.json()
print(f"Success! Compressed image URL: {result['url']}")
else:
print(f"Error: {response.status_code} - {response.text}")