# `diff_documents` — Redline & Audit Two Document Versions

> Part of [Document Redline & Table Diff Edge API](https://doc-redline.apikeg.com/llms.txt) (`com.apikeg/doc-redline`)

Runs two-level Myers O(ND) section/paragraph and inline word-and-punctuation diffing across two document versions, returning sectionBreadcrumb-tagged hunks, CriticMarkup ({--old--}{++new++}), inline HTML (<del>/<ins>), dedicated numericAndDateChanges alerts, and optional native .docx Track Changes.

**Why call instead of DIY:** Eliminates 95% of unchanged context tokens on multi-section specs/RFPs/SOWs, catches every altered number/currency/duration/date deterministically, and packages valid ECMA-376 .docx Track Changes ZIP archives on the edge.

- **REST Endpoint:** `POST https://doc-redline.apikeg.com/v1/diff-documents`
- **MCP Server:** `https://doc-redline.apikeg.com/mcp` (tool: `diff_documents`)
- **Pricing:** $0.002 USD per document redline comparison (x402 instant micropayment)
- **Free Dry-Run:** `POST https://doc-redline.apikeg.com/v1/diff-documents?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "original": {
      "type": "string",
      "maxLength": 900000,
      "description": "Baseline/original document text or Markdown (e.g. RFP, SOW, specification, SLA, or policy v1)."
    },
    "revised": {
      "type": "string",
      "maxLength": 900000,
      "description": "Revised/updated document text or Markdown to compare deterministically against original."
    },
    "changedHunksOnly": {
      "type": "boolean",
      "default": true,
      "description": "When true (default), omits 100% identical sections from hunks, criticMarkup, and htmlRedline so multi-page documents shrink by 90-98% in tokens."
    },
    "contextParagraphs": {
      "type": "integer",
      "default": 0,
      "minimum": 0,
      "maximum": 5,
      "description": "Number of surrounding unchanged paragraphs to include around each changed hunk (0-5)."
    },
    "ignoreWhitespace": {
      "type": "boolean",
      "default": true,
      "description": "Normalize repeated inline whitespace when aligning paragraphs while preserving exact text inside word-level diffs."
    },
    "includeDocxTrackChanges": {
      "type": "boolean",
      "default": false,
      "description": "When true, generates a base64 OpenXML .docx archive containing native Microsoft Word Track Changes (<w:ins> and <w:del>/<w:delText>) for Accept/Reject review."
    },
    "docxAuthor": {
      "type": "string",
      "default": "API Keg Redline Engine",
      "maxLength": 120,
      "description": "Author attribution recorded on OpenXML <w:ins> and <w:del> Track Changes tags when includeDocxTrackChanges is true."
    },
    "docxTitle": {
      "type": "string",
      "maxLength": 200,
      "description": "Optional document title stored in .docx Dublin Core package metadata."
    }
  },
  "required": [
    "original",
    "revised"
  ],
  "additionalProperties": false
}
```

## Examples
### Audit SOW revision with numeric duration & fee edits
```bash
curl -X POST "https://doc-redline.apikeg.com/v1/diff-documents" \
  -H "Content-Type: application/json" \
  -d '{"original":"# Statement of Work\n\n## Section 4.2: Payment Terms\nCustomer shall pay all undisputed invoices within 30 days of receipt at $150/hour.","revised":"# Statement of Work\n\n## Section 4.2: Payment Terms\nCustomer shall pay all undisputed invoices within 45 days of receipt at $175/hour.","changedHunksOnly":true,"contextParagraphs":0,"ignoreWhitespace":true,"includeDocxTrackChanges":true,"docxAuthor":"Procurement Ops Bot"}'
```
Expected output: 1 changed hunk under 'Statement of Work > Section 4.2: Payment Terms', 2 numericAndDateChanges ('30 days' -> '45 days', '$150' -> '$175'), and a valid base64 .docx with <w:del> and <w:ins>.
