xtract.bot
POST /api/diff-text

Diff two text inputs at line, word, or character granularity. Returns the unified diff plus per-segment add/remove arrays for custom rendering.

Computes the difference between two text inputs and returns both a unified-diff string (the format `git diff` produces) and a structured per-segment array (with `added`, `removed`, `unchanged` flags) so you can render the diff yourself. Granularity options: - `line` (default): treats each newline-terminated string as a single unit. Best for code or structured text. - `word`: tokenizes on whitespace. Best for prose. - `char`: per-character. Best for short strings or when you want fine-grained highlighting. Stats are included: lines added, lines removed, similarity ratio, longest unchanged run.

Inputs

NameTypeDefaultDescription
before*stringOriginal (pre-change) text.
after*stringModified (post-change) text.
modeenum (unified | split | stats)"unified"Output flavor.
granularityenum (line | word | char)"line"Granularity for split/stats; ignored for unified.
contextnumber (0…100)3Lines of unchanged context per hunk (unified only).

Response

Modes: json, text. Cache: yes (24h TTL).

Code samples

Built from the small example.


curl -X POST https://api.xtract.bot/api/diff-text \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "X-Account-Id: $XTRACT_ACCOUNT_ID" \
  -H "X-Api-Key: $XTRACT_API_KEY" \
  -d '{
  "before": "alpha\nbeta\ngamma\n",
  "after": "alpha\nBETA\ngamma\n",
  "mode": "unified",
  "context": 3
}'