Need to calculate how similar two strings are? This guide covers everything you need to know about string similarity via API, including different algorithms and practical applications.
What is String Similarity?
String similarity measures how alike two strings are, typically expressed as a percentage or ratio from 0 (completely different) to 1 (identical). Unlike exact comparison, similarity considers partial matches and near-matches.
Example: "hello" and "hallo" might have 80% similarity despite being different words.
Similarity Algorithms
Common algorithms for measuring similarity:
- Levenshtein-based: 1 - (edit_distance / max_length)
- Jaro-Winkler: Good for short strings and typos
- Cosine Similarity: Compares character frequency
- Dice Coefficient: Uses bigram overlap
Using the String Similarity API
TinyFn provides a simple endpoint to calculate similarity:
POST https://api.tinyfn.io/v1/text/similarity
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"string1": "hello world",
"string2": "hallo world",
"algorithm": "levenshtein"
}
{
"similarity": 0.909,
"similarity_percent": 90.9,
"algorithm": "levenshtein",
"is_similar": true
}
Parameters
| Parameter | Type | Description |
|---|---|---|
string1 |
string | First string (required) |
string2 |
string | Second string (required) |
algorithm |
string | Algorithm: levenshtein, jaro_winkler, cosine (default: levenshtein) |
threshold |
number | Similarity threshold for is_similar flag (default: 0.8) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/similarity',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
string1: 'hello world',
string2: 'hallo world'
})
}
);
const result = await response.json();
console.log(result.similarity_percent); // 90.9
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/similarity',
headers={'X-API-Key': 'your-api-key'},
json={'string1': 'hello world', 'string2': 'hallo world'}
)
result = response.json()
print(result['similarity_percent']) # 90.9
cURL
curl -X POST "https://api.tinyfn.io/v1/text/similarity" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"string1": "hello world", "string2": "hallo world"}'
Common Use Cases
- Fuzzy Search: Find approximate matches in search
- Data Deduplication: Identify near-duplicate records
- Typo Correction: Suggest corrections for misspellings
- Name Matching: Match names with variations
- Product Matching: Find similar products across catalogs
Best Practices
- Choose the right algorithm: Match algorithm to your use case
- Set appropriate thresholds: 80% is common for "similar"
- Normalize input: Lowercase and trim for better results
- Test with real data: Validate algorithm choice with your data
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 String Similarity API
Get your free API key and start calculating similarity in seconds.
Get Free API Key