Character Count API: The Complete Guide

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
Note: Character limits are common in social media (Twitter/X: 280 chars), SMS (160 chars), and meta descriptions (155-160 chars).

Using the Character Count API

TinyFn provides a simple endpoint to count characters:

API Request
POST https://api.tinyfn.io/v1/text/character-count
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "text": "Hello, World!"
}
Response
{
  "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

  1. Handle Unicode: Use APIs that properly count multi-byte characters
  2. Consider context: SMS counts differently than web text
  3. Show both counts: Display with and without spaces
  4. 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

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key