NewConnect Claude, Gemini, ChatGPT, and other AI agents to API Ninjas via our MCP server →

How to Get Nutrition Data in Python

Nutrition APIUpdated October 19, 2026
Create a free API key and it will be pre-filled into every code sample on this page. No credit card required.
Getting nutrition data in Python takes one GET request to the Nutrition API — and the query is plain English, like 1 large apple or 200g chicken breast. No food-ID lookups, no database dumps. This guide covers the request, the response fields, and building a small meal tracker on top. Every example works with a free API key.

Prerequisites

You need three things:

  • Python 3.8 or newer.
  • The requests library.
  • A free API Ninjas API key — issued instantly, no credit card. If you already have an account, it's on your profile page.
shell
pip install requests

Step 1: Look up your first food

The endpoint is /v1/nutrition and takes one parameter: query, a natural-language description of the food:

nutrition.py
import requests response = requests.get( 'https://api.api-ninjas.com/v1/nutrition', params={'query': '1 large apple'}, headers={'X-Api-Key': 'YOUR_API_KEY'}, timeout=10, ) response.raise_for_status() items = response.json() for item in items: print(f"{item['name']}: {item['carbohydrates_total_g']}g carbs, {item['sugar_g']}g sugar") # apple: 25.6g carbs, 18.8g sugar

The request on the wire and the response (free-plan fields shown):

api.api-ninjas.com
GET/v1/nutrition?query=1 large apple
200 OKapplication/json
[ { "name": "apple", "serving_size_g": 182, "fat_total_g": 0.3, "fat_saturated_g": 0.1, "fat_trans_g": 0, "sodium_mg": 1, "cholesterol_mg": 0, "carbohydrates_total_g": 25.6, "fiber_g": 4.3, "sugar_g": 18.8 } ]

Step 2: Read the response

The response is always a list, one entry per recognized food — because a single query can name several. Each entry carries the parsed serving_size_g, carbohydrates, sugar, fiber and fat in grams (carbohydrates_total_g, sugar_g, fiber_g, fat_total_g, fat_saturated_g), and minerals in milligrams (sodium_mg, cholesterol_mg). Note the quantity handling in the sample above: “1 large apple” became a 182-gram serving, and every value is scaled to it.

Paid plans also return calories, protein_g and a full set of micronutrients, plus food images; see the Nutrition API docs for every field.

Wrapped as a reusable client with the key in an environment variable:

nutrition_client.py
import os import requests API_URL = 'https://api.api-ninjas.com/v1/nutrition' session = requests.Session() session.headers['X-Api-Key'] = os.environ.get('API_NINJAS_KEY', 'YOUR_API_KEY') def get_nutrition(query: str) -> list: """Return nutrition data for a natural-language food query.""" response = session.get(API_URL, params={'query': query}, timeout=10) response.raise_for_status() return response.json() for item in get_nutrition('200g chicken breast'): print(f"{item['name']}: {item['fat_total_g']}g fat, {item['sodium_mg']}mg sodium")

Step 3: Query in plain English

The query parser is the reason this API is pleasant to build on: it understands cups, grams, slices, and multiple foods joined with “and” — so user input can go straight through:

queries.py
queries = [ '1 cup of rice', '200g chicken breast', 'a slice of cheese pizza', '1 banana and 2 tablespoons of peanut butter', ] for q in queries: items = get_nutrition(q) carbs = sum(item['carbohydrates_total_g'] or 0 for item in items) print(f'{q:45} {carbs:>6.1f}g carbs ({len(items)} item(s))')

Step 4: Build a meal tracker

A meal is just a multi-food query, so totals are a sum over the returned list. This helper aggregates carbs, sugar, fiber, fat and sodium:

meal.py
TRACKED = ['carbohydrates_total_g', 'sugar_g', 'fiber_g', 'fat_total_g', 'sodium_mg'] def meal_totals(query: str) -> dict: """Sum carbs, sugar, fiber, fat and sodium across every food in one query.""" totals = dict.fromkeys(TRACKED, 0.0) for item in get_nutrition(query): for key in TRACKED: totals[key] += item.get(key) or 0 return totals meal = meal_totals('2 eggs, 1 slice of toast, and 1 cup of orange juice') print(f"Carbs: {meal['carbohydrates_total_g']:.1f} g") # 40.6 g print(f"Sugar: {meal['sugar_g']:.1f} g") # 22.4 g print(f"Fiber: {meal['fiber_g']:.1f} g") # 1.3 g print(f"Fat: {meal['fat_total_g']:.1f} g") # 10.6 g print(f"Sodium: {meal['sodium_mg']:.0f} mg") # 266 mg

Log the totals to a CSV or SQLite table with a date column and you have a personal food diary in an afternoon.

Step 5: Handle errors

When no food is recognized, the API returns an empty list rather than an HTTP error. Check for it separately from the usual auth and rate-limit failures:

errors.py
from requests.exceptions import HTTPError, RequestException items = get_nutrition('flurbles') # unknown food if not items: print('No foods recognized — rephrase the query') try: items = get_nutrition('1 cup of rice') except HTTPError as error: status = error.response.status_code if status == 401: print('Missing or invalid API key') elif status == 429: print('Rate limit exceeded — slow down and retry') else: print(f'HTTP {status}: {error.response.text}') except RequestException as error: print(f'Network error: {error}')

Where to go from here

For a fitness app, pair this with the Calories Burned API (the expenditure side of the ledger) and the Exercises API; for meal ideas, the Recipe API searches by ingredient. The complete command-line tracker is below.

meal_tracker.py

The complete script from this guide: pass a meal description on the command line, get a per-food and total carbs, sugar, fiber and fat breakdown.

Frequently asked questions

What kinds of queries does the API accept?

Natural language with quantities: "1 cup of rice", "200g chicken breast", "2 eggs and 1 slice of toast". The parser extracts each food and its amount automatically.

Why is the response a list instead of a single object?

One query can contain several foods — "a banana and 2 tablespoons of peanut butter" returns two entries. Sum across the list for meal totals.

What units does the API return?

Carbohydrates, sugar, fiber and fat in grams, and sodium and cholesterol in milligrams. Serving size is in grams. Paid plans add calories, protein and further micronutrients.

Does the API cover branded foods?

Yes. /v1/nutrition covers everyday foods described in plain English, and paid plans add /v1/nutritionbranded for packaged products by barcode (UPC), name or brand, including ingredients and allergens. See the Nutrition API docs.

What happens when a food is not recognized?

The API returns an empty list rather than an error. Check for it and prompt for a rephrase — usually adding a quantity or simplifying the food name fixes it.