# Compress PDF

Source: https://smartpdf.ru/en/developers/docs/operations/compress

Reduces the size of a PDF. The result is never larger than the original file.

- Request: `POST https://api.smartpdf.ru/v1/pdf/compress`, JSON body, key in the `X-API-Key` header

- MCP tool: `compress_pdf` (a public file_url link instead of file_id)

- Input: PDF

- Result: PDF

- Price unit: 10 MB

## Parameters

| Parameter | Type | Description |
|---|---|---|
| `file_id` (required) | `string` | file_id of the uploaded file (POST /v1/upload); valid for an hour. |
| `level` | `string` | low: no quality loss; medium: images at 150 dpi; high: 100 dpi, maximum compression. Values: `"low"`, `"medium"`, `"high"`. Default: `"medium"`. |
| `target_mb` | `number` | Compress to a size: the limit in MB (1 MB = 1,000,000 bytes), from 0.1 to 500. The gentlest compression that fits the file within the limit is used (images no lower than 100 dpi); level is then ignored. If the file doesn’t fit, you get the smallest result; its size is output_file_size in the task status. |
| `webhook_url` | `string` | An https URL: when processing finishes, SmartPDF sends a POST request with the result and a signature to it — use it instead of polling the status (see the “Webhooks” section). |

## Request

Step 2 of the full cycle: the file is already uploaded; status and download work as in the quick start (https://smartpdf.ru/en/developers/docs/quickstart.md).

### cURL

```bash
BODY=$(jq -n --arg file_id "$FILE_ID" '{file_id: $file_id, level: "medium"}')
curl -sS --fail-with-body -H "X-API-Key: $SMARTPDF_API_KEY" -H "Content-Type: application/json" \
  -d "$BODY" https://api.smartpdf.ru/v1/pdf/compress
```

### Python

```python
task_id = call("POST", "/pdf/compress", json={"file_id": file_id, "level": "medium"})["task_id"]
```

### Node.js

```javascript
const { task_id } = await call("/pdf/compress", { method: "POST", json: { file_id: fileId, level: "medium" } });
```

### PHP

```php
$taskId = call('POST', '/pdf/compress', ['file_id' => $fileId, 'level' => 'medium'])['task_id'];
```

### Go

```go
payload, _ := json.Marshal(map[string]any{"file_id": fileID, "level": "medium"})
call("POST", "/pdf/compress", bytes.NewReader(payload), "application/json", &task)
```

### Java

```java
String body = JSON.writeValueAsString(Map.of("file_id", fileId, "level", "medium"));
String taskId = call(HttpRequest.newBuilder(URI.create(API + "/pdf/compress"))
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body))).get("task_id").asText();
```

### C#

```csharp
var task = await Read(await http.PostAsJsonAsync($"{Api}/pdf/compress", new { file_id = fileId, level = "medium" }));
```

## Response

`202 Accepted`: the operation is paid for and queued:

```json
{
  "task_id": "3f2c5b0e-8a41-4d7e-9c1f-5d0b8f2e7a64"
}
```

Next: GET /v1/tasks/{task_id} (https://smartpdf.ru/en/developers/docs/tasks.md) or a webhook (https://smartpdf.ru/en/developers/docs/webhooks.md).
