Need to convert data sizes in your application? This guide covers everything you need to know about data size conversion via API, including binary vs decimal units, file sizes, and implementation examples in multiple languages.
Understanding Data Units
Digital information is measured in bits and bytes. A byte consists of 8 bits. Larger units include kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB). Understanding data sizes is essential for file management, storage planning, and bandwidth calculations.
Binary vs Decimal
There are two systems for measuring data:
| Type | Base | Example |
|---|---|---|
| Decimal (SI) | 1000 | 1 KB = 1,000 bytes |
| Binary (IEC) | 1024 | 1 KiB = 1,024 bytes |
Supported Units
- Byte (B): Base unit
- Kilobyte (KB/KiB): 1,000 / 1,024 bytes
- Megabyte (MB/MiB): 1,000,000 / 1,048,576 bytes
- Gigabyte (GB/GiB): 10^9 / 2^30 bytes
- Terabyte (TB/TiB): 10^12 / 2^40 bytes
- Petabyte (PB/PiB): 10^15 / 2^50 bytes
Using the Data Size Converter API
TinyFn provides a dedicated endpoint for data size conversions:
GET https://api.tinyfn.io/v1/convert/data
Headers: X-API-Key: your-api-key
{
"from_unit": "GB",
"to_unit": "MB",
"value": 5,
"result": 5000,
"base": "decimal"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
from |
string | Source unit code (e.g., "GB") |
to |
string | Target unit code (e.g., "MB") |
value |
number | Value to convert |
base |
string | "decimal" (1000) or "binary" (1024) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/convert/data?from=GB&to=MB&value=5&base=decimal',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.result); // 5000
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/convert/data',
params={'from': 'GB', 'to': 'MB', 'value': 5, 'base': 'decimal'},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['result']) # 5000
cURL
curl "https://api.tinyfn.io/v1/convert/data?from=GB&to=MB&value=5&base=decimal" \
-H "X-API-Key: your-api-key"
Common Use Cases
- File Management: Display file sizes in human-readable format
- Cloud Storage: Show storage usage and quotas
- Upload Progress: Calculate and display transfer progress
- Bandwidth Monitoring: Track data usage
- DevOps: Monitor server storage and memory
Best Practices
- Be consistent: Choose binary or decimal and stick with it
- Show appropriate units: Use GB for large files, KB for small
- Explain discrepancies: Note if storage differs from expected
- Use IEC notation: Use KiB, MiB when precision matters
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 Data Size Converter API
Get your free API key and start converting data sizes in seconds.
Get Free API Key