# `diff_tables` — Compare Two Structured CSV or JSON Tables by Primary Key

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

Compares two JSON object arrays or RFC 4180 CSV strings keyed by a single or composite primaryKey, returning exact summary counts, added rows, removed rows, and cell-level modified diffs with exact decimal numericDelta.

**Why call instead of DIY:** Avoids row-order false positives in line-based diffs and floating-point drift on price/inventory catalogs; supports composite keys like ['sku', 'region'] in <10ms.

- **REST Endpoint:** `POST https://doc-redline.apikeg.com/v1/diff-tables`
- **MCP Server:** `https://doc-redline.apikeg.com/mcp` (tool: `diff_tables`)
- **Pricing:** $0.002 USD per structured table or catalog diff (x402 instant micropayment)
- **Free Dry-Run:** `POST https://doc-redline.apikeg.com/v1/diff-tables?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "before": {
      "description": "Table dataset passed either as a JSON array of row objects (e.g. [{\"sku\":\"A1\",\"region\":\"US\",\"price\":49.99}]) or an RFC 4180 CSV string with a header row."
    },
    "after": {
      "description": "Table dataset passed either as a JSON array of row objects (e.g. [{\"sku\":\"A1\",\"region\":\"US\",\"price\":49.99}]) or an RFC 4180 CSV string with a header row."
    },
    "primaryKey": {
      "description": "Single primary-key column name (e.g. \"sku\") or composite primary-key column list (e.g. [\"sku\", \"region\"])."
    },
    "ignoreFields": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 128
      },
      "maxItems": 64,
      "description": "Optional column names (such as \"updated_at\" or \"etag\") to exclude from cell comparison."
    },
    "csvDelimiter": {
      "type": "string",
      "default": ",",
      "minLength": 1,
      "maxLength": 1,
      "description": "Single-character column delimiter used when before or after is a CSV string (default ',')."
    },
    "coerceCsvTypes": {
      "type": "boolean",
      "default": true,
      "description": "Automatically parse unquoted numeric and boolean literals in CSV cells while preserving leading-zero identifiers."
    },
    "numericTolerance": {
      "type": "number",
      "default": 0,
      "minimum": 0,
      "maximum": 1000000,
      "description": "Optional non-negative tolerance; numeric cell differences with |delta| <= numericTolerance are treated as unchanged."
    }
  },
  "required": [
    "before",
    "after",
    "primaryKey"
  ],
  "additionalProperties": false
}
```

## Examples
### Diff regional SKU price catalog by composite key
```bash
curl -X POST "https://doc-redline.apikeg.com/v1/diff-tables" \
  -H "Content-Type: application/json" \
  -d '{"primaryKey":["sku","region"],"csvDelimiter":",","coerceCsvTypes":true,"numericTolerance":0,"before":"sku,region,price,stock\nPRO-100,US,49.99,120\nPRO-100,EU,54.99,80\nLEGACY-1,US,19.00,10","after":"sku,region,price,stock\nPRO-100,US,59.99,95\nPRO-100,EU,54.99,80\nNEW-200,US,99.00,250"}'
```
Expected output: unchangedCount=1, addedCount=1 (NEW-200::US), removedCount=1 (LEGACY-1::US), modifiedCount=1 (PRO-100::US price +10, stock -25).
