Need to convert measurement units in your application? This guide covers everything you need to know about unit conversion via API, including supported unit types, conversion accuracy, and implementation examples in multiple languages.
What is Unit Conversion?
Unit conversion is the process of changing a measurement from one unit to another while preserving the same quantity. For example, converting 1 kilometer to 0.621371 miles or 100 Celsius to 212 Fahrenheit.
Unit conversion is essential for international applications, scientific software, engineering tools, and any application dealing with measurements from different systems.
Supported Unit Types
The API supports comprehensive conversion across multiple categories:
- Length: meters, kilometers, miles, feet, inches, yards, nautical miles
- Weight: kilograms, grams, pounds, ounces, tons, stones
- Volume: liters, milliliters, gallons, quarts, cups, fluid ounces
- Temperature: Celsius, Fahrenheit, Kelvin
- Area: square meters, acres, hectares, square feet
- Speed: km/h, mph, m/s, knots
- Data: bytes, kilobytes, megabytes, gigabytes, terabytes
Using the Unit Converter API
TinyFn provides a unified endpoint for all unit conversions:
GET https://api.tinyfn.io/v1/convert/unit
Headers: X-API-Key: your-api-key
{
"from_unit": "km",
"to_unit": "mi",
"value": 100,
"result": 62.1371,
"category": "length"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
from |
string | Source unit code (e.g., "km") |
to |
string | Target unit code (e.g., "mi") |
value |
number | Value to convert |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/convert/unit?from=km&to=mi&value=100',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.result); // 62.1371
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/convert/unit',
params={'from': 'km', 'to': 'mi', 'value': 100},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['result']) # 62.1371
cURL
curl "https://api.tinyfn.io/v1/convert/unit?from=km&to=mi&value=100" \
-H "X-API-Key: your-api-key"
Common Use Cases
- International E-commerce: Show product dimensions in local units
- Fitness Apps: Convert between metric and imperial systems
- Cooking Apps: Convert recipe measurements
- Scientific Software: Handle unit conversions in calculations
- Navigation Apps: Display distances in preferred units
Best Practices
- Detect locale: Default to the user's preferred unit system
- Allow toggling: Let users switch between unit systems
- Show both units: Display original and converted values when relevant
- Handle precision: Round to appropriate decimal places for each unit type
Try the Unit Converter API
Get your free API key and start converting units in seconds.
Get Free API Key