# https://compresr.ai/docs/api-reference/compress-markdown

> Human-readable page: https://compresr.ai/docs/api-reference/compress-markdown

`POST /api/compress/markdown/`

> REST-only endpoint, not exposed in the Python or TypeScript SDKs. Call it directly over HTTP when you need structural compression of markdown documents.

Keeps headings, list bullets, and code fences intact while shortening prose. Unlike `/compress/question-specific/`, this endpoint does not take a `query`; it's structural, not query-conditioned.

## Request body

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `context` | string | yes | The markdown text to compress. Pass null or an empty string to get an empty result with no billing. |
| `compression_model_name` | string | yes | Compression model name. Use the markdown model exposed for your account. |
| `target_compression_ratio` | number | no | Compression strength. See [/docs/api-reference/models](/docs/api-reference/models). |

## Response

| Field | Type | Description |
| --- | --- | --- |
| `success` | boolean | true on success. |
| `data` | object |  |
| `data.original_context` | string | The input markdown, echoed back. |
| `data.compressed_context` | string | The compressed markdown, with headings and structural elements preserved. |
| `data.original_tokens` | integer |  |
| `data.compressed_tokens` | integer |  |
| `data.tokens_saved` | integer |  |
| `data.target_compression_ratio` | number \| null |  |
| `data.actual_compression_ratio` | number |  |
| `data.duration_ms` | integer |  |

## Status codes

| Status | Description |
| --- | --- |
| 200 | Compression succeeded. |
| 400 | Malformed JSON body. |
| 401 | Missing or invalid `X-API-Key`. |
| 422 | Field validation failure. |
| 429 | Rate limit hit. |
| 500 | Upstream error. |
| 503 | Upstream error. |

```python
import os
import httpx

response = httpx.post(
    "https://api.compresr.ai/api/compress/markdown/",
    headers={
        "X-API-Key": os.environ["COMPRESR_API_KEY"],
        "Content-Type": "application/json",
    },
    json={
        "context": "# Title\n\nLong prose paragraph...\n\n## Subheading\n\nMore prose...",
        "compression_model_name": "markdown_v1",
        "target_compression_ratio": 0.5,
    },
    timeout=60,
)
response.raise_for_status()
print(response.json()["data"]["compressed_context"])
```

**TypeScript**

```typescript
const response = await fetch(
  'https://api.compresr.ai/api/compress/markdown/',
  {
    method: 'POST',
    headers: {
      'X-API-Key': process.env.COMPRESR_API_KEY!,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      context: '# Title\n\nLong prose...\n\n## Subheading\n\nMore prose...',
      compression_model_name: 'markdown_v1',
      target_compression_ratio: 0.5,
    }),
  },
);

if (!response.ok) {
  throw new Error(`Compression failed: ${response.status}`);
}

const body = await response.json();
console.log(body.data.compressed_context);
```

**cURL**

```bash
curl -X POST https://api.compresr.ai/api/compress/markdown/ \
  -H "X-API-Key: $COMPRESR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "context": "# Title\n\nLong prose paragraph...\n\n## Subheading\n\nMore prose...",
    "compression_model_name": "markdown_v1",
    "target_compression_ratio": 0.5
  }'
```

  response={

```json
{
  "success": true,
  "data": {
    "original_context": "# Title\n\nLong prose paragraph...\n\n## Subheading\n\nMore prose...",
    "compressed_context": "# Title\n\nLong prose.\n\n## Subheading\n\nMore prose.",
    "original_tokens": 24,
    "compressed_tokens": 14,
    "tokens_saved": 10,
    "target_compression_ratio": 0.5,
    "actual_compression_ratio": 0.42,
    "duration_ms": 320
  }
}
```
