Need to count characters in text programmatically? This guide covers everything you need to know about character counting via API, including handling Unicode, spaces, and special characters.
What is Character Counting?
Character counting determines the number of characters in a text string. This includes letters, numbers, punctuation, spaces, and special characters. Accurate character counting must properly handle Unicode characters, emojis, and multi-byte characters.
Example: The text "Hello!" contains 6 characters.
Why Use a Character Count API?
Using an API for character counting provides several benefits:
- Unicode support: Properly counts multi-byte characters
- Multiple metrics: With/without spaces, excluding newlines
- Consistency: Same counting logic across platforms
- No edge cases: Handles emojis and special chars correctly
Using the Character Count API
TinyFn provides a simple endpoint to count characters:
POST https://api.tinyfn.io/v1/text/character-count
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "Hello, World!"
}
{
"total": 13,
"without_spaces": 12,
"letters": 10,
"digits": 0,
"punctuation": 2,
"spaces": 1
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The text to analyze (required) |
exclude_newlines |
boolean | Exclude newline characters from count (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/character-count',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ text: 'Hello, World!' })
}
);
const result = await response.json();
console.log(result.total); // 13
console.log(result.without_spaces); // 12
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/character-count',
headers={'X-API-Key': 'your-api-key'},
json={'text': 'Hello, World!'}
)
result = response.json()
print(result['total']) # 13
cURL
curl -X POST "https://api.tinyfn.io/v1/text/character-count" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text": "Hello, World!"}'
Common Use Cases
- Social Media Tools: Enforce Twitter/X character limits
- SMS Applications: Calculate message segments
- SEO Tools: Check meta description lengths
- Form Validation: Enforce input field limits
- Content Editors: Display real-time character counts
Best Practices
- Handle Unicode: Use APIs that properly count multi-byte characters
- Consider context: SMS counts differently than web text
- Show both counts: Display with and without spaces
- Real-time updates: Count on keystrokes for better UX
Try the Character Count API
Get your free API key and start counting characters in seconds.
Get Free API Key