Scale Food Nutrition From Per 100g to Grams Serving API
Published June 17, 2026
Every major food nutrition API stores macros per 100g. Users log 150 g or 2.5 oz. Your app must scale food nutrition per 100g to grams serving accurately. Calorie API offers both client-side math and a portion calc endpoint.
The Formula
scaled_value = (grams / 100) * per_100g_value
Example: 200g chicken, 31g protein per 100g:
protein = (200 / 100) * 31 = 62g
Apply to calories, carbs, fat, fiber, and sodium.
Option A: Client-Side Scale
After GET /foods/{id}:
function scaleNutrients(food, grams) {
const ratio = grams / 100;
return {
calories: food.calories * ratio,
proteinG: food.protein_g * ratio,
carbsG: food.carbohydrates_g * ratio,
fatG: food.fat_g * ratio,
};
}
Best when you already fetched detail and want instant UI updates as user drags a slider.
Option B: Portion Calc API
curl "https://api.calorieapi.com/api/v1/public/calc/portion?food_id=12345&grams=200"
Returns scaled macros server-side. Best when you want one source of truth and fewer client bugs.
Ounces Input
Convert before scaling:
const grams = ounces * 28.3495;
Keep grams as canonical storage in your database.
Edge Cases
| Case | Handling |
|---|---|
| grams = 0 | Reject validation |
| grams > 5000 | Cap or warn (portion API max) |
| null nutrient | Show N/A, not zero |
| Liquids | Still use grams if user weighs cup |
Used By Every Vertical
Frequently Asked Questions
How do I scale per-100g nutrition to a gram serving?
Multiply each nutrient by (grams / 100). Calorie API also offers GET /public/calc/portion with food_id and grams for server-side scaling.
Why do food APIs use per 100g values?
Per 100g standardizes comparison across foods and simplifies portion math for any gram amount.
Should I scale on the client or server?
Client scaling is fine after fetching food detail. Use portion calc API when you want centralized math and fewer client-side edge case bugs.
How do I handle ounces in a food logging app?
Convert ounces to grams (1 oz = 28.3495 g), then apply the per-100g scaling formula or call portion calc with grams.
Does Calorie API portion calc accept any food ID?
Yes. Use food IDs from search or barcode results. Grams must be between 0 and 5000 per API validation.
