Overview
The divisor depends on `standard`: `binary` (the default) divides by 1024 and labels units B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB and YiB; any other value, including `decimal`, divides by 1000 and labels them B, KB, MB, GB, TB, PB, EB, ZB and YB. For every unit the API computes `bytes / divisor^n` and rounds it to `precision` decimal places with `toFixed`, then parses it back to a number, so trailing zeros are dropped: 1073741824 bytes gives `value: 1`, not `1.00`.
When `unit` is not set, the API walks from the largest unit down and picks the first whose value is 1 or more. Values under 1 byte stay in `B`. When `unit` is set, it is matched case-insensitively against the label list of the chosen standard; a label that does not belong to that standard (for example `MB` with `standard=binary`) is not found, and the auto-selected unit is used instead without an error. `formatted` is `value` and `unit` joined by a space.
`precision` defaults to 2. Because rounding happens per unit, small results in large units round to 0, which is why a 1 GiB input shows `TiB: 0` in `conversions`. The `conversions` object is keyed by the labels of the active standard and is returned as null on the Free plan.
Live Test File Size Formatter API →
Endpoint
One host, one path per API. The block below shows this call in four languages; every one of them is the same HTTP request. Making requests covers the timeouts, retries and parameter rules that apply to all of them. The SDKs wrap the same call in a typed client.
curl "https://api.apiverve.com/v1/filesizeformatter?bytes=1073741824&unit=GB&precision=2&standard=binary" \
-H "x-api-key: your_api_key_here"const res = await fetch('https://api.apiverve.com/v1/filesizeformatter?bytes=1073741824&unit=GB&precision=2&standard=binary', {
headers: { 'x-api-key': 'your_api_key_here' },
});
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
const { data } = await res.json();
console.log(data);import requests
res = requests.get(
"https://api.apiverve.com/v1/filesizeformatter?bytes=1073741824&unit=GB&precision=2&standard=binary",
headers={"x-api-key": "your_api_key_here"},
timeout=15,
)
res.raise_for_status()
print(res.json()["data"])package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, _ := http.NewRequest("GET", "https://api.apiverve.com/v1/filesizeformatter?bytes=1073741824&unit=GB&precision=2&standard=binary", nil)
req.Header.Set("x-api-key", "your_api_key_here")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
out, _ := io.ReadAll(res.Body)
fmt.Println(string(out))
}Replace your_api_key_here with the key from your dashboard. When the inputs arrive as a list rather than one at a time, batch requests run up to 200 of them through this same API in a single call.
Authentication
Send your key in the x-api-key header. That is the only auth step — there is no token exchange and no per-endpoint scope to configure. Authentication covers creating, rotating and revoking keys.
A 401 means the key is missing, invalid or expired. A 403 means the key is valid but not permitted here — blocked by a key restriction or an IP allow-list. Running out of credits is a 429.
Parameters
Sent in the query string. Premium parameters are accepted on every plan but only take effect on plans that include them.
| Parameter | Type | Description |
|---|---|---|
bytesRequired | integer | Number of bytes to format range 0–∞ |
unitOptional | string | Specific target unit (optional, auto-selects best unit if not specified)autoBKBMBGBTBPBEB+2 |
precisionOptional | integer | Decimal places (0-10) default 2 · range 0–10 |
standardOptional | string | Standard to use: binary (1024) or decimal (1000)binarydecimaldefault binary |
Response
Every API returns the same three top-level keys, so one response handler covers your whole integration: status, error and data. Only data changes shape. Response format covers the envelope, the other output formats and how premium fields are withheld.
{
"status": "ok",
"error": null,
"data": {
"bytes": 1073741824,
"formatted": "1 GiB",
"value": 1,
"unit": "GiB",
"standard": "binary",
"precision": 2,
"conversions": {
"B": 1073741824,
"KiB": 1048576,
"MiB": 1024,
"GiB": 1,
"TiB": 0,
"PiB": 0,
"EiB": 0,
"ZiB": 0,
"YiB": 0
}
}
}
Response fields
Paths are relative to data. Premium fields are absent rather than zeroed on plans that do not include them, so check for presence instead of comparing to 0.
| Field | Type | Example | Description |
|---|---|---|---|
bytes | number | 1073741824 | Original byte value in numeric format |
formatted | string | "1 GiB" | Formatted file size with unit as human-readable string |
value | number | 1 | Numeric value in the selected or auto-determined unit |
unit | string | "GiB" | File size unit used in the conversion (e.g., GiB, MB) |
standard | string | "binary" | Standard used for conversion: binary or decimal |
precision | number | 2 | Decimal places applied to the formatted value |
conversionsPremium | object | {...} | Complete conversion table with values across all units |
BPremium | number | 1073741824 | The size expressed in bytes |
KiBPremium | number | 1048576 | The size expressed in kibibytes (1024 bytes) |
MiBPremium | number | 1024 | The size expressed in mebibytes |
GiBPremium | number | 1 | The size expressed in gibibytes |
TiBPremium | number | 0 | The size expressed in tebibytes |
PiBPremium | number | 0 | The size expressed in pebibytes |
EiBPremium | number | 0 | The size expressed in exbibytes |
ZiBPremium | number | 0 | The size expressed in zebibytes |
YiBPremium | number | 0 | The size expressed in yobibytes |
Errors
Read the HTTP status first, then error for the specific reason. The body names the parameter that has to change. Error handling covers the full status list and which of them are worth retrying.
| Status | Meaning | What to do |
|---|---|---|
400 | Input was rejected | Read error; it names the parameter. |
401 | Key missing or invalid | Check the header name and the key value. |
403 | Key valid, but not permitted | A key restriction or IP allow-list; see key scoping. |
429 | Rate limited, or out of credits | Read error to tell them apart; see rate limits. |
Use cases
- File Management
- Display file sizes in user-friendly formats in file managers and storage applications
- Storage Analytics
- Format storage usage data in dashboards and analytics reports
- Data Transfer
- Show download and upload sizes in appropriate units during file transfers
- API Responses
- Convert byte sizes to readable formats in API responses and data exports
Other ways to use File Size Formatter
Set up File Size Formatter on APIVerve, or reach the same source a different way. Your APIVerve account and credits work on all of them — one key, one balance.
Related
More in Data Conversion: