Text Diff API

Compare two texts by lines, words, characters, sentences or JSON and get the changes, a similarity score and a unified diff patch.

POSThttps://apibox.io.vn/v1/text-diff

Send the original text in a (or old) and the new text in b (or new). The API returns the list of changes (each one added, removed or unchanged with its value), stats with added, removed and unchanged counts, a similarity percentage and identical when nothing changed.

Choose the granularity with mode: lines (default, counts lines and also returns a unified diff like git diff), words, chars, sentences, or json which compares two JSON documents structurally after pretty printing. ignore_case and ignore_whitespace relax the comparison. For other modes, unified=true adds the patch; a_name, b_name and context (0-50 lines, default 3) control its header and context.

Both texts together may contain up to 2,000,000 characters (request body up to 4 MB).

Parameters

NameInTypeRequiredDefault
abodystringyesOriginal text. Alias: old.
bbodystringyesNew text. Alias: new.
modebody|querystringnolinesComparison unit: lines, words, chars, sentences or json.
ignore_casebody|querybooleannofalseIgnore upper/lower case differences (lines, words, chars).
ignore_whitespacebody|querybooleannofalseIgnore leading/trailing whitespace in lines mode, and whitespace changes in words mode.
unifiedbody|querybooleannofalseAlso return a unified diff for modes other than lines (always included in lines mode).
contextbody|querynumberno3Context lines in the unified diff (0-50).
a_namebody|querystringnoaFile name of the original in the unified diff header.
b_namebody|querystringnobFile name of the new text in the unified diff header.

Example

curl -X POST "https://apibox.io.vn/v1/text-diff" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"a":"one\ntwo\nthree","b":"one\n2\nthree\nfour"}'

Response

{
  "mode": "lines",
  "identical": false,
  "similarity": 50,
  "stats": {
    "added": 2,
    "removed": 1,
    "unchanged": 2,
    "unit": "lines"
  },
  "changes": [
    {
      "type": "unchanged",
      "value": "one\n",
      "count": 1
    },
    {
      "type": "removed",
      "value": "two\n",
      "count": 1
    },
    {
      "type": "added",
      "value": "2\n",
      "count": 1
    },
    {
      "type": "unchanged",
      "value": "three\n",
      "count": 1
    },
    {
      "type": "added",
      "value": "four\n",
      "count": 1
    }
  ],
  "unified": "===================================================================\n--- a\n+++ b\n@@ -1,3 +1,4 @@\n one\n-two\n+2\n three\n+four\n"
}

Use cases

  • Show what changed between two versions of a document or contract
  • Compare API responses or JSON configs in tests
  • Generate unified diff patches from a backend
  • Measure how similar two texts are (plagiarism, deduplication)

FAQ

Can I get a git-style unified diff?

Yes. In the default lines mode the unified field always contains the patch; for other modes add unified=true.

How do I compare two JSON objects?

Use mode=json with both documents as JSON strings in a and b. They are pretty printed and compared line by line, so formatting differences are ignored.

How is similarity calculated?

Unchanged units divided by unchanged plus the larger of added or removed, as a percentage. Units are lines in lines/json mode and characters otherwise.

Related APIs

Start with 1,000 free requests every month. Get free API key →