Need to convert energy units in your application? This guide covers everything you need to know about energy conversion via API, including electrical, thermal, and nutritional energy units with implementation examples.
Understanding Energy Units
Energy is the capacity to do work. The SI unit is the joule (J), but different fields use different units: kilowatt-hours (kWh) for electricity, calories for nutrition, BTU for heating/cooling, and electron volts (eV) in physics.
Energy conversion is essential for utility applications, fitness/nutrition apps, HVAC systems, and scientific calculations.
Supported Energy Units
| Unit | Code | Common Use |
|---|---|---|
| Joule | J | SI unit, physics |
| Kilojoule | kJ | Nutrition (intl) |
| Calorie | cal | Small scale |
| Kilocalorie | kcal | Nutrition (US) |
| Kilowatt-hour | kWh | Electricity |
| Watt-hour | Wh | Batteries |
| BTU | BTU | HVAC (US) |
| Electron Volt | eV | Physics |
Using the Energy Converter API
TinyFn provides a dedicated endpoint for energy conversions:
GET https://api.tinyfn.io/v1/convert/energy
Headers: X-API-Key: your-api-key
{
"from_unit": "kcal",
"to_unit": "kJ",
"value": 2000,
"result": 8368,
"formula": "2000 × 4.184"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
from |
string | Source unit code (e.g., "kcal") |
to |
string | Target unit code (e.g., "kJ") |
value |
number | Value to convert |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/convert/energy?from=kcal&to=kJ&value=2000',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.result); // 8368
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/convert/energy',
params={'from': 'kcal', 'to': 'kJ', 'value': 2000},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['result']) # 8368
cURL
curl "https://api.tinyfn.io/v1/convert/energy?from=kcal&to=kJ&value=2000" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Nutrition Apps: Convert between kcal and kJ
- Utility Monitoring: Track electricity usage in kWh
- HVAC Applications: Calculate heating/cooling in BTU
- Fitness Trackers: Display calories burned
- Solar/Battery: Calculate stored energy in Wh
Best Practices
- Know your context: Use kcal for food (US), kJ internationally
- Be precise: Note that 1 Calorie (big C) = 1 kcal
- Show both units: Display kcal/kJ for nutrition labels
- Match industry: Use BTU for HVAC, kWh for electricity
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-convert": {
"url": "https://api.tinyfn.io/mcp/convert/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all conversion tools available via MCP in our Conversion MCP Tools for AI Agents guide.
Try the Energy Converter API
Get your free API key and start converting energy units in seconds.
Get Free API Key