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.
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
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
a | body | string | yes | Original text. Alias: old. | |
b | body | string | yes | New text. Alias: new. | |
mode | body|query | string | no | lines | Comparison unit: lines, words, chars, sentences or json. |
ignore_case | body|query | boolean | no | false | Ignore upper/lower case differences (lines, words, chars). |
ignore_whitespace | body|query | boolean | no | false | Ignore leading/trailing whitespace in lines mode, and whitespace changes in words mode. |
unified | body|query | boolean | no | false | Also return a unified diff for modes other than lines (always included in lines mode). |
context | body|query | number | no | 3 | Context lines in the unified diff (0-50). |
a_name | body|query | string | no | a | File name of the original in the unified diff header. |
b_name | body|query | string | no | b | File 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"}'const res = await fetch('https://apibox.io.vn/v1/text-diff', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"a": "one\ntwo\nthree",
"b": "one\n2\nthree\nfour"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/text-diff",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"a": "one\ntwo\nthree", "b": "one\n2\nthree\nfour"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/text-diff');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"a":"one\\ntwo\\nthree","b":"one\\n2\\nthree\\nfour"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));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
Slugify
Turn titles into clean URL slugs: accents removed (Vietnamese đ included), custom separator, optional case keeping and maximum length.
123Text Statistics
Count words, characters, sentences, paragraphs and lines, estimate reading and speaking time, and list the most frequent words of a text.
AaCase Converter
Convert text to lower, UPPER, Title, Sentence, camelCase, PascalCase, snake_case, kebab-case, CONSTANT_CASE or dot.case in one call.