Length Converter API: The Complete Guide

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
MetermMetric
KilometerkmMetric
CentimetercmMetric
MillimetermmMetric
MilemiImperial
YardydImperial
FootftImperial
InchinImperial
Nautical MilenmiMaritime
Common Conversions: 1 mile = 1.60934 km, 1 foot = 0.3048 m, 1 inch = 2.54 cm

Using the Length Converter API

TinyFn provides a dedicated endpoint for length conversions:

API Request
GET https://api.tinyfn.io/v1/convert/length
Headers: X-API-Key: your-api-key
Response
{
  "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

  1. Auto-detect region: Default to metric or imperial based on user location
  2. Provide toggle: Let users switch between systems easily
  3. Round sensibly: Use appropriate precision for the context
  4. 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

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key