Need to convert length or distance units in your application? This guide covers everything you need to know about length conversion via API, including metric and imperial units, conversion factors, and implementation examples in multiple languages.
Understanding Length Units
Length is one of the fundamental physical quantities. The world primarily uses two measurement systems: the metric system (meters, kilometers) used by most countries, and the imperial system (feet, miles) used mainly in the United States.
Converting between these systems is essential for international applications, e-commerce, travel, and scientific computing.
Supported Length Units
| Unit | Code | System |
|---|---|---|
| Meter | m | Metric |
| Kilometer | km | Metric |
| Centimeter | cm | Metric |
| Millimeter | mm | Metric |
| Mile | mi | Imperial |
| Yard | yd | Imperial |
| Foot | ft | Imperial |
| Inch | in | Imperial |
| Nautical Mile | nmi | Maritime |
Using the Length Converter API
TinyFn provides a dedicated endpoint for length conversions:
GET https://api.tinyfn.io/v1/convert/length
Headers: X-API-Key: your-api-key
{
"from_unit": "mi",
"to_unit": "km",
"value": 26.2,
"result": 42.1649,
"formula": "26.2 × 1.60934"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
from |
string | Source unit code (e.g., "mi") |
to |
string | Target unit code (e.g., "km") |
value |
number | Value to convert |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/convert/length?from=mi&to=km&value=26.2',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.result); // 42.1649
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/convert/length',
params={'from': 'mi', 'to': 'km', 'value': 26.2},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['result']) # 42.1649
cURL
curl "https://api.tinyfn.io/v1/convert/length?from=mi&to=km&value=26.2" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Navigation Apps: Display distances in user-preferred units
- E-commerce: Show product dimensions in local units
- Fitness Tracking: Convert run/walk distances
- Real Estate: Convert property measurements
- Shipping: Calculate package dimensions
Best Practices
- Auto-detect region: Default to metric or imperial based on user location
- Provide toggle: Let users switch between systems easily
- Round sensibly: Use appropriate precision for the context
- Label units: Always display the unit alongside the value
Try the Length Converter API
Get your free API key and start converting lengths in seconds.
Get Free API Key