Need to convert weight or mass units in your application? This guide covers everything you need to know about weight conversion via API, including metric and imperial units, conversion accuracy, and implementation examples in multiple languages.
Weight vs Mass
Technically, mass (measured in kilograms) and weight (measured in newtons) are different. However, in everyday usage and most applications, they're used interchangeably. The API handles common "weight" units that are technically measures of mass.
The metric system uses kilograms and grams, while the imperial system uses pounds and ounces. Converting between them is essential for international shipping, e-commerce, health apps, and cooking.
Supported Weight Units
| Unit | Code | System |
|---|---|---|
| Kilogram | kg | Metric |
| Gram | g | Metric |
| Milligram | mg | Metric |
| Metric Ton | t | Metric |
| Pound | lb | Imperial |
| Ounce | oz | Imperial |
| Stone | st | Imperial (UK) |
| US Ton | ton | Imperial |
Using the Weight Converter API
TinyFn provides a dedicated endpoint for weight conversions:
GET https://api.tinyfn.io/v1/convert/weight
Headers: X-API-Key: your-api-key
{
"from_unit": "kg",
"to_unit": "lb",
"value": 75,
"result": 165.347,
"formula": "75 × 2.20462"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
from |
string | Source unit code (e.g., "kg") |
to |
string | Target unit code (e.g., "lb") |
value |
number | Value to convert |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/convert/weight?from=kg&to=lb&value=75',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.result); // 165.347
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/convert/weight',
params={'from': 'kg', 'to': 'lb', 'value': 75},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['result']) # 165.347
cURL
curl "https://api.tinyfn.io/v1/convert/weight?from=kg&to=lb&value=75" \
-H "X-API-Key: your-api-key"
Common Use Cases
- E-commerce: Display product weights in local units
- Shipping: Calculate shipping costs based on weight
- Health Apps: Track weight in user-preferred units
- Recipe Apps: Convert ingredient weights
- Fitness: Track lifting weights and body weight
Best Practices
- Know your audience: US users expect pounds, most others expect kg
- Handle precision: Use appropriate decimal places
- Store in metric: Store weights in kg internally, convert for display
- Validate ranges: Check for reasonable weight values
Try the Weight Converter API
Get your free API key and start converting weights in seconds.
Get Free API Key