Need to calculate the edit distance between strings? This guide covers everything you need to know about Levenshtein distance via API, including its applications in fuzzy matching and spell checking.
What is Levenshtein Distance?
Levenshtein distance (also called edit distance) measures the minimum number of single-character edits required to transform one string into another. It's named after Soviet mathematician Vladimir Levenshtein who developed the concept in 1965.
Example: The distance between "kitten" and "sitting" is 3 (k->s, e->i, +g).
Edit Operations
Three types of edits are counted:
- Insertion: Add a character
- Deletion: Remove a character
- Substitution: Replace a character with another
Using the Levenshtein Distance API
TinyFn provides a simple endpoint to calculate edit distance:
POST https://api.tinyfn.io/v1/text/levenshtein
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"string1": "kitten",
"string2": "sitting"
}
{
"distance": 3,
"string1_length": 6,
"string2_length": 7,
"similarity_ratio": 0.5714
}
Parameters
| Parameter | Type | Description |
|---|---|---|
string1 |
string | First string (required) |
string2 |
string | Second string (required) |
case_sensitive |
boolean | Case-sensitive comparison (default: true) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/levenshtein',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
string1: 'kitten',
string2: 'sitting'
})
}
);
const result = await response.json();
console.log(result.distance); // 3
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/levenshtein',
headers={'X-API-Key': 'your-api-key'},
json={'string1': 'kitten', 'string2': 'sitting'}
)
result = response.json()
print(result['distance']) # 3
cURL
curl -X POST "https://api.tinyfn.io/v1/text/levenshtein" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"string1": "kitten", "string2": "sitting"}'
Common Use Cases
- Spell Checking: Find closest dictionary words
- Fuzzy Search: Match similar search terms
- Data Deduplication: Find near-duplicate records
- DNA Sequencing: Compare genetic sequences
- Plagiarism Detection: Find similar text passages
Best Practices
- Normalize strings: Consider lowercase for better matching
- Set thresholds: Define acceptable distance for fuzzy matches
- Use ratio for comparison: Distance/max_length gives similarity ratio
- Consider performance: Long strings increase computation time
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-text": {
"url": "https://api.tinyfn.io/mcp/text/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all text analysis tools available via MCP in our Text Analysis MCP Tools for AI Agents guide.
Try the Levenshtein Distance API
Get your free API key and start calculating edit distance in seconds.
Get Free API Key