Need to truncate text to a specific length? This guide covers everything you need to know about text truncation via API, including word-boundary awareness, ellipsis options, and best practices.
What is Text Truncation?
Text truncation shortens a string to a specified length, typically adding an indicator (like "...") to show content was removed. Smart truncation cuts at word boundaries rather than mid-word for better readability.
Example: "This is a long sentence" truncated to 15 chars becomes "This is a..."
Truncation Options
Different truncation strategies for different needs:
- Character-based: Cut at exact character count
- Word-based: Cut at word boundaries
- Sentence-based: Cut at sentence boundaries
- Custom suffix: Use "..." or "[...]" or custom text
Using the Text Truncate API
TinyFn provides a simple endpoint to truncate text:
POST https://api.tinyfn.io/v1/text/truncate
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "This is a very long sentence that needs to be truncated.",
"max_length": 30,
"word_boundary": true
}
{
"original": "This is a very long sentence that needs to be truncated.",
"truncated": "This is a very long...",
"was_truncated": true,
"original_length": 56,
"truncated_length": 22
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The text to truncate (required) |
max_length |
integer | Maximum length including suffix (required) |
word_boundary |
boolean | Cut at word boundaries (default: true) |
suffix |
string | Suffix to append (default: "...") |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/truncate',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'This is a very long sentence that needs to be truncated.',
max_length: 30,
word_boundary: true
})
}
);
const result = await response.json();
console.log(result.truncated); // "This is a very long..."
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/truncate',
headers={'X-API-Key': 'your-api-key'},
json={
'text': 'This is a very long sentence that needs to be truncated.',
'max_length': 30,
'word_boundary': True
}
)
result = response.json()
print(result['truncated']) # "This is a very long..."
cURL
curl -X POST "https://api.tinyfn.io/v1/text/truncate" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text": "This is a very long sentence.", "max_length": 20}'
Common Use Cases
- Preview Text: Show article excerpts in listings
- Meta Descriptions: Truncate to SEO character limits
- UI Components: Fit text in fixed-width elements
- Notifications: Shorten messages for push notifications
- Social Sharing: Prepare text for character-limited platforms
Best Practices
- Use word boundaries: Don't cut words in the middle
- Account for suffix: Include "..." length in max_length
- Consider mobile: Different lengths for different viewports
- Preserve meaning: Ensure truncated text makes sense
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 Text Truncate API
Get your free API key and start truncating text in seconds.
Get Free API Key