API Documentation
Integrate MegaOptim's image optimization into your applications with our REST API. Submit images, poll for results, and download optimized files in seconds.
Base URL: https://api.megaoptim.com
How It Works
Submit
Send your image via URL or file upload to the optimize endpoint.
POST /v1/optimize
Poll
Use the returned process_id to long-poll for the result.
POST /v1/optimize/{id}/result
Download
Fetch the optimized image from the URL in the response.
GET /image/{id}/{file}
Authentication
All optimization and result endpoints require an API key passed via the X-API-KEY header.
X-API-KEY: your_api_key_here
Get your API key from your dashboard.
Utility Endpoints
These endpoints require no authentication and are useful for health checks and monitoring.
/
— Health Check
Basic health check. No rate limit.
{
"status": 1
}
/status
— Server Status
Returns server version info. Rate limit: 300 requests/minute.
{
"status": "ok",
"code": 200,
"software": {
"php": "8.4.x",
"app": "12.x.x"
}
}
/ping
— Ping
Simple ping check. Rate limit: 300 requests/minute.
{
"status": "ok",
"code": 200
}
/v1/optimize
Schedule one or more images for optimization. Returns a process_id to poll for results.
Requires authentication. Rate limit: 120 requests/minute.
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
type |
string | Yes | Input source: url, file, urls, or files |
url |
string | type=url | URL of the image to optimize |
url1...url5 |
string | type=urls | URLs of images (url1 required, up to 5) |
file |
file | type=file | Image file upload |
file1...file5 |
file | type=files | Image file uploads (file1 required, up to 5) |
compression |
string | No | Compression level (see below). Default: medium |
webp |
int | No | 1 to generate additional WebP version alongside optimized original |
keep_exif |
int | No | 1 to preserve EXIF metadata. Default: 0 (strip) |
cmyktorgb |
int | No | 1 to convert CMYK color space to RGB. Default: 0 |
max_width |
int | No | Resize to max width (pixels), preserving aspect ratio |
max_height |
int | No | Resize to max height (pixels), preserving aspect ratio |
http_user |
string | No | HTTP Basic Auth username (for protected URLs) |
http_pass |
string | No | HTTP Basic Auth password (required if http_user is set) |
callback_url |
string | No | URL to receive async result callback |
Compression Levels
| Value | Aliases | Description |
|---|---|---|
high | ultra | Aggressive compression (smaller size, lower quality) |
medium | intelligent | Balanced compression (default) |
lossless | — | Maximum quality, lossless optimization |
Supported formats: JPEG, PNG, GIF, WebP, AVIF
Example: Optimize by URL
curl -X POST https://api.megaoptim.com/v1/optimize \
-H "X-API-KEY: your_api_key" \
-F "type=url" \
-F "url=https://example.com/photo.jpg" \
-F "compression=medium"
Example: Upload & Convert to WebP
curl -X POST https://api.megaoptim.com/v1/convert \
-H "X-API-KEY: your_api_key" \
-F "type=file" \
-F "file=@/path/to/image.jpg" \
-F "compression=high" \
-F "format=webp"
Success Response (202)
{
"status": "processing",
"code": 202,
"process_id": 12345
}
Error Response (400)
{
"status": "error",
"code": 400,
"errors": [
"The type field is required and must be one of: url, file, urls, files."
]
}
/v1/convert
Convert one or more images to a different format (WebP or AVIF). Returns a process_id to poll for results.
Requires authentication. Rate limit: 120 requests/minute.
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
type |
string | Yes | Input source: url, file, urls, or files |
format |
string | Yes | Target format: webp or avif |
url |
string | type=url | URL of the image to convert |
url1...url5 |
string | type=urls | URLs of images (url1 required, up to 5) |
file |
file | type=file | Image file upload |
file1...file5 |
file | type=files | Image file uploads (file1 required, up to 5) |
compression |
string | No | Compression level: high, medium (default), or lossless |
keep_exif |
int | No | 1 to preserve EXIF metadata. Default: 0 (strip) |
Example: Convert to AVIF
curl -X POST https://api.megaoptim.com/v1/convert \
-H "X-API-KEY: your_api_key" \
-F "type=file" \
-F "file=@/path/to/image.jpg" \
-F "format=avif"
Success Response (202)
{
"status": "processing",
"code": 202,
"process_id": 12345
}
Poll for results using POST /v1/convert/{process_id}/result (same format as the optimize result endpoint).
/v1/optimize/{process_id}/result
Long-polls for the optimization result. The server holds the connection open until the result is ready or the timeout is reached.
Requires authentication. Rate limit: 350 requests/minute.
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
process_id | int | Yes (URL) | The process_id returned from the optimize endpoint |
timeout | int | No | Max seconds to wait (30-120). Default: 80 |
Success Response (200)
{
"status": "ok",
"code": 200,
"result": [
{
"success": true,
"file_name": "photo.jpg",
"original_size": 524288,
"optimized_size": 314572,
"saved_bytes": 209716,
"saved_percent": 40.0,
"url": "https://cdn.example.com/abc123/photo.jpg",
"type": "compress",
"format": "jpg"
}
],
"id": 12345,
"user": {
"name": "John Doe",
"email": "[email protected]",
"tokens": 4500
}
}
Result Object Fields
| Field | Type | Description |
|---|---|---|
success |
bool | Whether this file was optimized successfully |
file_name |
string | Original file name |
original_size |
int | Original file size in bytes |
optimized_size |
int | Optimized file size in bytes (0 if no savings) |
saved_bytes |
int | Bytes saved (0 if no savings) |
saved_percent |
float | Percentage saved (0 if no savings) |
url |
string|null | URL to download the optimized image (null if no savings) |
type |
string | Operation type: compress, convert, or compress_convert |
format |
string | Output format (e.g., jpg, png, webp, avif) |
webp |
object|null | WebP version details (present only when webp=1 was requested) |
WebP Object (when present)
| Field | Type | Description |
|---|---|---|
url | string | URL to download the WebP version |
saved_bytes | int | Bytes saved vs original |
optimized_size | int | WebP file size in bytes |
saved_percent | float | Percentage saved vs original |
User Object
| Field | Type | Description |
|---|---|---|
name | string | Account name |
email | string | Account email |
tokens | int | Remaining API tokens (-1 = unlimited subscription) |
Error Responses
Not Found (404)
{
"status": "error",
"errors": ["Job not found."],
"code": 404
}
Unauthorized (401)
{
"status": "error",
"errors": ["Access denied. This optimization request does not belong to your account."],
"code": 401
}
Timeout (504)
{
"status": "error",
"errors": ["Request timeout! The max timeout of 80 seconds was reached."],
"code": 504
}
Failed (500)
{
"status": "error",
"code": 500,
"errors": ["Optimization failure. Possible internal server error. Please contact support for more information."]
}
/image/{public_id}/{file_name}
Download an optimized image file. The full URL is provided in the result object's url field.
No authentication required. Returns the image with the appropriate Content-Type header.
Supported output types: JPEG, PNG, GIF, WebP, AVIF, SVG. Returns 404 if the file is not found.
/v1/users/info
Get account information for the provided API key.
Requires authentication. Rate limit: 120 requests/minute. Works even with 0 tokens.
Example Request
curl -X POST https://api.megaoptim.com/v1/users/info \
-H "X-API-KEY: your_api_key"
Success Response (200)
{
"status": "ok",
"code": 200,
"result": {
"name": "John Doe",
"email": "[email protected]",
"tokens": 4500,
"verified": 1
}
}
Response Fields
| Field | Type | Description |
|---|---|---|
name | string | Account name |
email | string | Account email |
tokens | int | Remaining API tokens (-1 = unlimited subscription) |
verified | int | 1 if email is verified, 0 otherwise |
Error Response (401)
{
"status": "error",
"code": 401,
"errors": ["Your API Key is invalid. Make sure you use the correct API key obtained from https://megaoptim.com/"]
}
Error Codes
| Code | Meaning |
|---|---|
200 | OK |
202 | Accepted (optimization queued) |
400 | Bad Request (validation errors) |
401 | Unauthorized (invalid key or access denied) |
404 | Not Found |
465 | Missing API Key (no X-API-KEY header provided) |
466 | Insufficient Tokens (account has no remaining tokens) |
500 | Internal Server Error |
504 | Gateway Timeout (result polling exceeded timeout) |
Authentication Error Examples
Missing API Key (465)
{
"status": "error",
"code": 465,
"errors": ["Missing API key. Make sure you copy the correct API key from https://megaoptim.com/dashboard or if you do not have one, sign up at https://megaoptim.com"]
}
Invalid API Key (404)
{
"status": "error",
"code": 404,
"errors": ["Invalid API key. You can always signup on our site https://megaoptim.com and choose your plan."]
}
Insufficient Tokens (466)
{
"status": "error",
"code": 466,
"errors": ["No tokens left. Please top up your account at https://megaoptim.com/dashboard in order to continue."]
}
Rate Limits
| Endpoint | Limit |
|---|---|
GET /status, GET /ping | 300 requests/minute |
POST /v1/optimize, POST /v1/convert | 120 requests/minute |
POST /v1/optimize/{id}/result, POST /v1/convert/{id}/result | 350 requests/minute |
POST /v1/users/info | 120 requests/minute |
Ready to Optimize?
Sign up for a free account and get 500 images per month. No credit card required.
Get Your API Key