Macro Calculator API: The Complete Guide

Need to calculate optimal macronutrient ratios in your nutrition or fitness application? This guide covers everything you need to know about macro calculation via API, including different diet approaches, goal-based splits, and implementation examples.

What are Macronutrients?

Macronutrients are the three main nutrients that provide calories: protein (4 cal/g), carbohydrates (4 cal/g), and fat (9 cal/g). The balance of these macros affects body composition, energy levels, and overall health.

While total calories determine weight change, macro ratios influence whether you lose fat or muscle, how satisfied you feel, and your athletic performance.

Macro Ratios Explained

Different goals and preferences call for different macro splits:

Balanced (40/30/30)

40% carbs, 30% protein, 30% fat. A versatile starting point suitable for most people maintaining weight or general fitness.

Low-Carb (25/35/40)

25% carbs, 35% protein, 40% fat. Effective for fat loss, blood sugar management, and those who feel better on fewer carbs.

High-Protein (35/40/25)

35% carbs, 40% protein, 25% fat. Ideal for muscle building, preserving muscle during fat loss, or high-activity athletes.

Ketogenic (5/25/70)

5% carbs, 25% protein, 70% fat. Very low-carb approach for those following a ketogenic diet.

Protein Priority: Regardless of diet approach, aim for at least 1.6-2.2g of protein per kg of body weight when training.

Using the Macro Calculator API

TinyFn provides a simple endpoint to calculate macros:

API Request
GET https://api.tinyfn.io/v1/health/macros?calories=2000&goal=muscle_gain&weight=70
Headers: X-API-Key: your-api-key
Response
{
  "calories": 2000,
  "macros": {
    "protein": {
      "grams": 175,
      "calories": 700,
      "percentage": 35
    },
    "carbs": {
      "grams": 200,
      "calories": 800,
      "percentage": 40
    },
    "fat": {
      "grams": 56,
      "calories": 500,
      "percentage": 25
    }
  },
  "ratio": "40/35/25",
  "goal": "muscle_gain",
  "protein_per_kg": 2.5
}

Parameters

Parameter Type Description
calories number Target daily calories
goal string Goal: fat_loss, maintain, muscle_gain, keto
weight number Weight in kg (for protein calculations)
ratio string Custom ratio like "40/30/30" (carbs/protein/fat)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/health/macros?calories=2000&goal=muscle_gain&weight=70',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(`Protein: ${data.macros.protein.grams}g`); // Protein: 175g

Python

import requests

response = requests.get(
    'https://api.tinyfn.io/v1/health/macros',
    params={'calories': 2000, 'goal': 'muscle_gain', 'weight': 70},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
macros = data['macros']
print(f"P: {macros['protein']['grams']}g, C: {macros['carbs']['grams']}g, F: {macros['fat']['grams']}g")

cURL

curl "https://api.tinyfn.io/v1/health/macros?calories=2000&goal=muscle_gain&weight=70" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Meal Planning Apps: Generate meals that fit macro targets
  • Nutrition Tracking: Set and monitor macro goals
  • Fitness Coaching: Create personalized nutrition plans
  • Recipe Builders: Suggest ingredients to hit macro targets
  • Diet Challenges: Structure macro-based diet programs

Best Practices

  1. Prioritize protein: Set protein first based on body weight, then divide remaining calories
  2. Allow flexibility: Give users a 5-10% range rather than exact targets
  3. Consider preferences: Some people thrive on higher fat, others on higher carbs
  4. Adjust for activity: Athletes may need more carbs on training days

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-health": {
      "url": "https://api.tinyfn.io/mcp/health/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all health tools available via MCP in our Health MCP Tools for AI Agents guide.

Try the Macro Calculator API

Get your free API key and start calculating macros in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key