Skip to content

How to Get Nutritional Data for Any Food Using an API

Published May 31, 2026

A food nutritional data API lets you retrieve calories, macros, and micronutrients for any ingredient or packaged product with a single HTTP request. To get calorie data from an API, you send a food name, barcode, or food ID to a REST endpoint and parse the JSON response into your app.

This guide shows exactly how to get calories by food name API lookup, read a nutrition API JSON response, and return protein, carbs, and fat for meal logging and diet apps.

What You Need Before You Start

  • A free developer account and API key from Calorie API
  • Any language that supports HTTP (curl, JavaScript, or Python)
  • Basic familiarity with JSON

Step 1: Search for a Food by Name

The fastest way to get nutrition facts from an API is a text search endpoint.

curl "https://api.calorieapi.com/api/v1/search/foods?q=greek+yogurt&limit=5" \
  -H "X-API-Key: YOUR_API_KEY"

The response includes matching foods with calories and macronutrients. Pick the best id from results for a full detail lookup.

Step 2: Get Full Nutrition Detail by Food ID

curl "https://api.calorieapi.com/api/v1/foods/12345" \
  -H "X-API-Key: YOUR_API_KEY"

A food API calories per 100g response typically includes standardized nutrient arrays so you can compare foods consistently and scale to any serving size.

Step 3: Parse the JSON in JavaScript

const res = await fetch(
  'https://api.calorieapi.com/api/v1/search/foods?q=banana',
  { headers: { 'X-API-Key': process.env.CALORIE_API_KEY } }
);
const data = await res.json();
const food = data.results[0];
console.log(`NULL: NULL kcal`);

Map protein_g, carbohydrates_g, and fat_g into your UI for macro tracking.

Step 4: Build a Meal Nutrition Total

For a meal nutrition API workflow, loop ingredients, fetch each item, multiply nutrients by quantity, and sum:

StepAction
1User adds ingredients + grams
2Search API resolves each food to an ID
3Fetch per-100g nutrients
4Scale: (grams / 100) * nutrient_value
5Sum calories, protein, carbs, fat

This pattern powers recipe sites, meal prep apps, and food ingredient nutrition API features.

Common Errors and Fixes

ErrorCauseFix
401 UnauthorizedMissing or invalid API keyAdd X-API-Key header
404 Not foundNo match for queryBroaden search term or try barcode
429 Too many requestsRate limit exceededCache results; backoff retries
Empty resultsQuery too specificUse match_mode=any if supported

Tips for Production

  • Cache frequent foods (chicken breast, rice, eggs) for 24 hours
  • Debounce live search inputs to reduce API calls
  • Store food_id after first lookup to skip repeat searches
  • Log failed lookups to improve your food alias dictionary

Get Started Free

Create your free API key and read the API documentation.

Frequently Asked Questions

How do I get calorie data from an API?

Send an HTTP GET request to a food search endpoint with the food name as the query parameter. The API returns JSON with calories, protein, carbs, fat, and other nutrients per serving or per 100g.

Can an API return protein, carbs, and fat together?

Yes. Most nutrition APIs return macronutrients in a single JSON object. Calorie API includes protein, carbohydrates, fat, fiber, and sugar fields in search and food detail responses.

What is the best endpoint to get nutrition facts?

Start with GET /search/foods for discovery, then GET /foods/{id} for complete nutrient detail including per-100g values and serving metadata.

How do I calculate meal nutrition from an API?

Resolve each ingredient to a food ID, fetch per-100g nutrients, scale by quantity, and sum totals across all ingredients in the meal.

Is there a free API for nutrition data?

Yes. Calorie API offers a free developer tier with monthly quota. See our guide on free food APIs for tier comparisons.

← Back to all articles

Start building with the Calorie API

Get a free API key and access 4M+ foods with search, barcode lookup, and full macro data.