Need to identify what language text is written in? This guide covers everything you need to know about language detection via API, including supported languages, confidence scoring, and implementation examples.
What is Language Detection?
Language detection is the process of automatically identifying what language a piece of text is written in. This is essential for multilingual applications that need to route content appropriately or provide translations.
Modern language detection can identify over 100 languages with high accuracy, even from short text snippets.
How Language Detection Works
Language detection algorithms typically use these techniques:
- N-gram Analysis: Analyze character and word patterns unique to each language
- Unicode Ranges: Identify scripts (Latin, Cyrillic, CJK, etc.)
- Statistical Models: Machine learning models trained on multilingual text
- Dictionary Matching: Match words against language-specific dictionaries
Using the Language Detection API
TinyFn provides an endpoint to detect text language:
POST https://api.tinyfn.io/v1/detect/language
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "Bonjour, comment allez-vous aujourd'hui?"
}
{
"detected_language": {
"code": "fr",
"name": "French",
"native_name": "Francais",
"confidence": 0.98
},
"alternatives": [
{"code": "it", "name": "Italian", "confidence": 0.02}
],
"text_length": 41,
"reliable": true
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | Text to detect language for (required) |
hint |
string | Expected language code hint (optional) |
alternatives |
integer | Number of alternative languages to return (default: 3) |
Code Examples
JavaScript / Node.js
const response = await fetch('https://api.tinyfn.io/v1/detect/language', {
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'Guten Tag, wie geht es Ihnen?'
})
});
const data = await response.json();
console.log(`Language: ${data.detected_language.name}`);
console.log(`Confidence: ${data.detected_language.confidence * 100}%`);
// Output: Language: German, Confidence: 97%
Python
import requests
def detect_language(text):
response = requests.post(
'https://api.tinyfn.io/v1/detect/language',
json={'text': text},
headers={'X-API-Key': 'your-api-key'}
)
return response.json()
# Example usage
result = detect_language("Hola, como estas?")
print(f"Language: {result['detected_language']['name']}")
print(f"Code: {result['detected_language']['code']}")
# Output: Language: Spanish, Code: es
cURL
curl -X POST "https://api.tinyfn.io/v1/detect/language" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text": "Ciao, come stai?"}'
Common Use Cases
- Content Routing: Route user content to appropriate moderators or translators
- Translation Services: Auto-detect source language before translation
- Search Enhancement: Apply language-specific stemming and analysis
- User Experience: Auto-select interface language
- Data Classification: Organize multilingual content
Best Practices
- Use enough text: Provide at least 20-50 characters for reliable detection
- Check confidence: Consider results unreliable below 0.7 confidence
- Handle mixed content: Some text may contain multiple languages
- Consider context: Use the hint parameter if you expect a specific language
Try the Language Detection API
Get your free API key and start detecting languages in seconds.
Get Free API Key