Need to clean up whitespace in text? This guide covers everything you need to know about whitespace removal via API, including handling spaces, tabs, newlines, and other invisible characters.
What is Whitespace?
Whitespace refers to characters that create empty space in text. This includes spaces, tabs, newlines, carriage returns, and other Unicode space characters. Excessive whitespace can cause issues in data processing and display.
Common whitespace characters: space ( ), tab (\t), newline (\n), carriage return (\r)
Whitespace Removal Modes
Different ways to handle whitespace:
- Trim: Remove leading and trailing whitespace
- Collapse: Replace multiple spaces with single space
- Remove all: Strip all whitespace characters
- Normalize: Trim + collapse for clean text
Using the Whitespace Remover API
TinyFn provides a simple endpoint to remove whitespace:
POST https://api.tinyfn.io/v1/text/whitespace-remove
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": " Hello World ",
"mode": "normalize"
}
{
"original": " Hello World ",
"cleaned": "Hello World",
"mode": "normalize",
"characters_removed": 7
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The text to clean (required) |
mode |
string | Mode: trim, collapse, remove_all, normalize (default: normalize) |
preserve_newlines |
boolean | Keep newline characters (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/whitespace-remove',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ text: ' Hello World ', mode: 'normalize' })
}
);
const result = await response.json();
console.log(result.cleaned); // "Hello World"
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/whitespace-remove',
headers={'X-API-Key': 'your-api-key'},
json={'text': ' Hello World ', 'mode': 'normalize'}
)
result = response.json()
print(result['cleaned']) # "Hello World"
cURL
curl -X POST "https://api.tinyfn.io/v1/text/whitespace-remove" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text": " Hello World ", "mode": "normalize"}'
Common Use Cases
- Form Input Cleaning: Normalize user input before storage
- Data Processing: Clean imported data from various sources
- Text Comparison: Normalize text before comparing
- Search Indexing: Clean text before indexing
- CSV/TSV Processing: Clean column values
Best Practices
- Choose the right mode: Don't remove all whitespace unless needed
- Preserve newlines when needed: For multi-line text formatting
- Handle Unicode spaces: Include non-breaking spaces and other Unicode
- Validate output: Ensure text remains readable after cleaning
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 Whitespace Remover API
Get your free API key and start cleaning text in seconds.
Get Free API Key