Skip to content

Text Comparison ​

The /diff endpoint compares two texts and returns an ordered list of changes, line by line or word by word.

How it works?

The comparison relies on the Longest Common Subsequence (LCS) algorithm: it finds the largest set of segments shared by both texts in the same order, and everything outside that set is reported as added or removed. In line mode the texts are split on newlines, keeping empty lines; in word mode they are split on whitespace runs and empty segments are dropped.

Parameters ​

The request body must be JSON:

ParameterRequiredDescription
aYesOriginal text
bYesModified text, compared against a
modeNoComparison granularity: line or word. Default: line

Available Modes ​

ModeDescription
lineSplits both texts on newlines and compares line by line
wordSplits both texts on whitespace and compares word by word

Response Fields ​

FieldTypeDescription
modestringGranularity used: line or word
addednumberNumber of segments present only in b
removednumberNumber of segments present only in a
changesobject[]Ordered changes rebuilding b from a

changes entries:

FieldTypeDescription
typestringequal, add or del
valuestringThe line or word this change applies to

Code Examples ​

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "a": "line1\nline2",
    "b": "line1\nline2 edited"
  }' \
  "https://api.sylvain.sh/v6/diff"

Try It ​

Error Handling ​

If parameters are missing or invalid, the API will return an error:

Error MessageDescription
Please provide a first text (a={text})The a parameter is missing or is not a string
Please provide a second text (b={text})The b parameter is missing or is not a string
Texts must be 10000 characters or fewera or b exceeds the maximum length
Mode must be one of: line, wordThe mode value is not line or word
Texts must contain 2000 lines or fewerA text has more lines than the limit in line mode
Texts must contain 2000 words or fewerA text has more words than the limit in word mode