HTML to Text API: The Complete Guide

Need to extract plain text from HTML in your application? This guide covers everything you need to know about HTML to text conversion via API, including formatting options, tag handling, and implementation examples.

Why Convert HTML to Text?

HTML contains markup that's meant for browsers, not humans or text processing. Converting HTML to plain text extracts the readable content, making it suitable for emails, search indexing, accessibility, and text analysis.

Conversion Options

HTML to text conversion offers several options:

Tag Handling

Some tags should be converted to text equivalents: links can include URLs, lists can use bullets, and headings can use formatting.

Whitespace

HTML collapses whitespace, but plain text needs proper spacing. Line breaks and paragraphs must be preserved appropriately.

Entity Decoding

HTML entities like & must be decoded to their text equivalents (&).

Note: Our API intelligently handles common HTML patterns while producing clean, readable text output.

Using the HTML to Text API

TinyFn provides a simple endpoint to convert HTML to text:

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

{
  "html": "<h1>Hello World</h1><p>This is a <strong>test</strong> paragraph.</p>"
}
Response
{
  "text": "Hello World\n\nThis is a test paragraph.",
  "word_count": 6,
  "char_count": 39
}

Parameters

Parameter Type Description
html string HTML content to convert (required)
preserve_links boolean Include URLs in output (default: false)
preserve_newlines boolean Preserve line breaks (default: true)

Code Examples

JavaScript / Node.js

const html = '<h1>Title</h1><p>Content here.</p>';
const response = await fetch('https://api.tinyfn.io/v1/convert/html-to-text', {
  method: 'POST',
  headers: {
    'X-API-Key': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ html })
});
const result = await response.json();
console.log(result.text); // "Title\n\nContent here."

Python

import requests

html = '<h1>Title</h1><p>Content here.</p>'
response = requests.post(
    'https://api.tinyfn.io/v1/convert/html-to-text',
    json={'html': html},
    headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['text'])  # "Title\n\nContent here."

cURL

curl -X POST "https://api.tinyfn.io/v1/convert/html-to-text" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Title</h1><p>Content here.</p>"}'

Common Use Cases

  • Email Plain Text: Generate text version of HTML emails
  • Search Indexing: Extract searchable text from web pages
  • Text Analysis: Prepare HTML content for NLP processing
  • Accessibility: Provide text alternatives for screen readers
  • Content Preview: Generate text snippets from HTML articles

Best Practices

  1. Handle encoding: Ensure HTML is properly encoded (UTF-8)
  2. Consider context: Choose whether to preserve links based on use case
  3. Test edge cases: Verify handling of tables, lists, and special tags
  4. Trim output: Remove excess whitespace from the result

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-convert": {
      "url": "https://api.tinyfn.io/mcp/convert/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all conversion tools available via MCP in our Conversion MCP Tools for AI Agents guide.

Try the HTML to Text API

Get your free API key and start extracting text in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key