Extract Numbers API: The Complete Guide

Need to extract numbers from text? This guide covers everything you need to know about number extraction via API, including handling integers, decimals, negative numbers, and different formats.

What is Number Extraction?

Number extraction identifies and extracts all numeric values from text. This is useful for data processing, text analysis, and converting unstructured text into structured data. Proper extraction handles various number formats and edge cases.

Example: "The price is $29.99 for 3 items" extracts [29.99, 3]

Types of Numbers

Different number formats to consider:

  • Integers: 42, 1000, -5
  • Decimals: 3.14, 0.5, -2.7
  • Scientific notation: 1.5e10, 2E-5
  • Formatted numbers: 1,000,000 or 1.000.000
Tip: Consider whether you need to extract numbers with units (e.g., "10kg", "50%") or numbers within words (e.g., "version2").

Using the Extract Numbers API

TinyFn provides a simple endpoint to extract numbers:

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

{
  "text": "The temperature is 72.5 degrees and humidity is 45%",
  "include_decimals": true
}
Response
{
  "numbers": [72.5, 45],
  "count": 2,
  "sum": 117.5,
  "positions": [
    {"value": 72.5, "start": 19, "end": 23},
    {"value": 45, "start": 44, "end": 46}
  ]
}

Parameters

Parameter Type Description
text string The text to extract numbers from (required)
include_decimals boolean Extract decimal numbers (default: true)
include_negative boolean Extract negative numbers (default: true)
unique_only boolean Return only unique numbers (default: false)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/text/extract-numbers',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'your-api-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      text: 'The price is $29.99 for 3 items',
      include_decimals: true
    })
  }
);
const result = await response.json();
console.log(result.numbers); // [29.99, 3]

Python

import requests

response = requests.post(
    'https://api.tinyfn.io/v1/text/extract-numbers',
    headers={'X-API-Key': 'your-api-key'},
    json={'text': 'The price is $29.99 for 3 items', 'include_decimals': True}
)
result = response.json()
print(result['numbers'])  # [29.99, 3]

cURL

curl -X POST "https://api.tinyfn.io/v1/text/extract-numbers" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"text": "The price is $29.99 for 3 items"}'

Common Use Cases

  • Price Extraction: Extract prices from product descriptions
  • Data Parsing: Extract metrics from reports
  • Log Analysis: Pull numbers from log files
  • Form Processing: Extract numeric data from text inputs
  • Analytics: Calculate statistics from text content

Best Practices

  1. Consider context: "Version 2.0" might need different handling than "$2.00"
  2. Handle locale: Some regions use comma as decimal separator
  3. Validate results: Extracted numbers may need range validation
  4. Preserve positions: Use position data for text highlighting

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 Extract Numbers API

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

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key