Bytes Format API: The Complete Guide to Human-Readable File Sizes

Need to display file sizes in a user-friendly format? This guide covers everything you need to know about converting bytes to human-readable strings via API, including SI vs binary units and implementation examples in multiple languages.

What is Bytes Formatting?

Bytes formatting converts raw byte counts into human-readable strings with appropriate units. For example, 1073741824 bytes becomes "1 GB" or "1 GiB" depending on the unit system used.

Users understand "4.5 MB" much better than "4718592 bytes".

SI vs Binary Units

There are two common systems for measuring digital storage:

SI Units (Decimal, Base-10)

1 KB = 1,000 bytes, 1 MB = 1,000 KB, 1 GB = 1,000 MB. Used by hard drive manufacturers and network speeds.

Binary Units (IEC, Base-2)

1 KiB = 1,024 bytes, 1 MiB = 1,024 KiB, 1 GiB = 1,024 MiB. Used by operating systems and RAM specifications.

Note: A "500 GB" hard drive is 500 x 10^9 bytes (SI), but shows as ~465 GiB in your OS (binary). This causes common confusion.

Using the Bytes Format API

TinyFn provides a simple endpoint to format byte values:

API Request
GET https://api.tinyfn.io/v1/format/bytes?bytes=1073741824
Headers: X-API-Key: your-api-key
Response
{
  "bytes": 1073741824,
  "formatted": "1 GB",
  "unit": "GB",
  "value": 1.07,
  "standard": "SI"
}

Parameters

Parameter Type Description
bytes integer The number of bytes to format
standard string Unit standard: SI (KB, MB, GB) or IEC (KiB, MiB, GiB) (default: SI)
precision integer Decimal places (default: 2)
space boolean Include space between number and unit (default: true)

Code Examples

JavaScript / Node.js

// Format bytes (SI units)
const response = await fetch(
  'https://api.tinyfn.io/v1/format/bytes?bytes=1073741824',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const { formatted } = await response.json();
console.log(formatted); // 1.07 GB

// Format with binary units (IEC)
const iecResponse = await fetch(
  'https://api.tinyfn.io/v1/format/bytes?bytes=1073741824&standard=IEC',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const { formatted: iecFormatted } = await iecResponse.json();
console.log(iecFormatted); // 1 GiB

Python

import requests

# Format bytes (SI units)
response = requests.get(
    'https://api.tinyfn.io/v1/format/bytes',
    params={'bytes': 1073741824},
    headers={'X-API-Key': 'your-api-key'}
)
print(response.json()['formatted'])  # 1.07 GB

# Format with binary units (IEC)
response = requests.get(
    'https://api.tinyfn.io/v1/format/bytes',
    params={'bytes': 1073741824, 'standard': 'IEC'},
    headers={'X-API-Key': 'your-api-key'}
)
print(response.json()['formatted'])  # 1 GiB

cURL

# Format bytes (SI)
curl "https://api.tinyfn.io/v1/format/bytes?bytes=1073741824" \
  -H "X-API-Key: your-api-key"

# Format bytes (IEC/binary)
curl "https://api.tinyfn.io/v1/format/bytes?bytes=1073741824&standard=IEC" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • File Managers: Display file and folder sizes
  • Upload Progress: Show uploaded/remaining bytes
  • Storage Quotas: Display used vs available space
  • Bandwidth Monitoring: Show data transfer amounts
  • Log Analysis: Format data sizes in reports

Best Practices

  1. Be consistent: Use either SI or IEC throughout your application
  2. Match context: Use SI for network speeds, IEC for RAM/storage
  3. Show precision appropriately: 1.5 GB is clearer than 1.536 GB
  4. Consider accessibility: Some users prefer full words (gigabytes)

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

See all formatting tools available via MCP in our Formatting MCP Tools for AI Agents guide.

Try the Bytes Format API

Get your free API key and start formatting file sizes in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key